POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】
题目链接:传送门
题目大意:
求斐波那契数列第n项F(n)。
(F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109)
思路:
用矩阵乘法加速递推。
算法竞赛进阶指南的模板:
#include <iostream>
#include <cstring> using namespace std;
const int MOD = ; void mul(int f[], int base[][]) {
int c[];
memset(c, , sizeof c);
for (int j = ; j < ; j++) {
for (int k = ; k < ; k++) {
c[j] = (c[j] + 1LL * f[k] * base[k][j]) % MOD;
}
}
memcpy(f, c, sizeof c);
}
void mulself(int base[][]) {
int c[][];
memset(c, , sizeof c);
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k < ; k++)
c[i][j] = (c[i][j] + 1LL*base[i][k]*base[k][j]) % MOD;
memcpy(a, c, sizeof c);
} int main()
{
int n;
while (cin >> n && n != -) {
int f[] = {, };
int base[][] = {{, }, {, }};
for (; n; n >>= ) {
if (n & ) mul(f, base);
mulself(base);
}
cout << f[] << endl;
}
return ;
}
蒟蒻的模板:
#include <cstdio>
#include <iostream>
#include <cstring> using namespace std;
typedef long long ll;
const int MOD = 1e4;
const int MAXN = ;
struct Matrix{
int mat[MAXN][MAXN];
Matrix operator * (Matrix const& b) const {
Matrix res;
memset(res.mat, , sizeof res.mat);
for (int i = ; i < MAXN; i++)
for (int j = ; j < MAXN; j++)
for (int k = ; k < MAXN; k++)
res.mat[i][j] = (res.mat[i][j] + this->mat[i][k] * b.mat[k][j])%MOD;
return res;
}
}base, F0, FN;
Matrix fpow(Matrix base, ll n) {
Matrix res;
memset(res.mat, , sizeof res.mat);
for (int i = ; i < MAXN; i++)
res.mat[i][i] = ;
while (n) {
if (n&) res = res*base;
base = base * base;
n >>= ;
}
return res;
}
void init()
{
base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ;
memset(F0.mat, , sizeof F0.mat);
F0.mat[][] = ; F0.mat[][] = ;
} int main()
{
int N;
while (cin >> N) {
if (N == -)
break;
init();
FN = fpow(base, N);
cout << FN.mat[][] << endl;
}
return ;
}
POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】的更多相关文章
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- CH 3401 - 石头游戏 - [矩阵快速幂加速递推]
题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...
- HDU 1757 矩阵快速幂加速递推
题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- CH3401 石头游戏(矩阵快速幂加速递推)
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
- 洛谷P1357 花园(状态压缩 + 矩阵快速幂加速递推)
题目链接:传送门 题目: 题目描述 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(<=N<=^).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻 ...
- [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
- 2019.2.25考试T1, 矩阵快速幂加速递推+单位根反演(容斥)
\(\color{#0066ff}{题解}\) 然后a,b,c通过矩阵加速即可 为什么1出现偶数次3没出现的贡献是上面画绿线的部分呢? 考虑暴力统计这部分贡献,答案为\(\begin{aligned} ...
随机推荐
- servlet/和/*匹配的区别
两者真正的区别是,两者的长度不同,根据最长路径匹配的优先级,/*比/更容易被选中,而/的真正含义是,缺省匹配.既所有的URL都无法被选中的时候,就一定会选中/,可见它的优先级是最低的,这就两者的区别.
- CString 转换为 wchar_t *
1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str); / ...
- Java Web(二) Servlet详解
什么是Servlet? Servlet是运行在Web服务器中的Java程序.Servlet通常通过HTTP(超文本传输协议)接收和响应来自Web客户端的请求.Java Web应用程序中所有的请求-响应 ...
- windows 常用dos命令
explorer目录 打开当前目录 explorer . 打开上级目录 explorer .. 打开任意目录 explorer dirname cls 命令 清屏屏幕,屏幕显示的所有字符信息都是存放在 ...
- Windows Server 2016与旧版本系统比较
一.性能和可扩性 特征描述 Windows Server 2012/2012 R2 标准版和数据中心 Windows Server 2016 标准版和数据中心 物理内存(主机)支持 每个物理服务器至多 ...
- Python的网络编程--思维导图
Python的网络编程--思维导图
- Driver 01 进程隐藏
大二时候的代码以及笔记,当时暂时记录在QQ上在,现在发出来分享一下. 为了写驱动装一大堆的软件插件啥的,还常常失败. 这里就顺带总结下SDK下载和WinDbg symbol路径设置正确WinDbg却总 ...
- DevExpress WinForms v18.2新版亮点(八)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- web项目与jsp有关的三个jar的依赖
<!-- jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId> ...
- kubenetes pv(nfs) pvc 搭建
1:nfs-server的搭建. install the NFS Server: sudo apt install nfs-kernel-server 2:配置server. vim /etc/exp ...