题目链接:传送门

题目大意:

  求斐波那契数列第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(矩阵快速幂加速递推)【模板题】的更多相关文章

  1. 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 ...

  2. CH 3401 - 石头游戏 - [矩阵快速幂加速递推]

    题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...

  3. 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 ...

  4. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  5. CH3401 石头游戏(矩阵快速幂加速递推)

    题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...

  6. 洛谷P1357 花园(状态压缩 + 矩阵快速幂加速递推)

    题目链接:传送门 题目: 题目描述 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(<=N<=^).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻 ...

  7. [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...

  8. [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)

    Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...

  9. 2019.2.25考试T1, 矩阵快速幂加速递推+单位根反演(容斥)

    \(\color{#0066ff}{题解}\) 然后a,b,c通过矩阵加速即可 为什么1出现偶数次3没出现的贡献是上面画绿线的部分呢? 考虑暴力统计这部分贡献,答案为\(\begin{aligned} ...

随机推荐

  1. [转]每天一个linux命令(44):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  2. AAC ADTS格式分析

    转自: https://blog.csdn.net/jay100500/article/details/52955232 https://blog.csdn.net/andyhuabing/artic ...

  3. python 怎样使用单个反斜杠\

    path2 = "c:\\windows\\temp\\readme.txt" path2:用一个"\"取消第二个"\"的特殊转义作用,即为 ...

  4. 逆袭之旅DAY16.东软实训.Oracle.匿名块

    2018-07-1216:41:19 六.匿名块 .定义匿名块: declare 定义部分: ---可选部分 begin 执行部分: ---必选部分 exception 异常处理部分: ---可选部分 ...

  5. windows 路由的配置

    查看ip路由表 route print : netstat -r windows 下添加一条路由 route命令 route [-f][-p][command [distinataion] [MASK ...

  6. java⑨

    do-while,先执行一次,再判断! do{ 循环体 }while(循环条件); 经典案例: 1. 需求:    01.记录每次用户购买的商品金额! 之后进行 结账!    02.增加购买商品的数量 ...

  7. xpath & <tr><td><br>

    python : 3.6 lxml : 4.2.1 from lxml.html import etree test_html = ''' <!DOCTYPE html PUBLIC " ...

  8. react全家桶-服务端与客户端配置

    全家桶内装有: react - github react-router - github redux - github react-redux - github react-router-redux ...

  9. ORA-02049: 超时: 分布式事务处理等待锁

    java.sql.SQLSyntaxErrorException: ORA-02049: 超时: 分布式事务处理等待锁 ORA-06512: 在 "HECDEV.BGT_JOURNAL_BA ...

  10. CreateThread和_beginthread区别及使用

    CreateThread 是一个Win 32API 函数, _beginthread 是一个CRT(C Run-Time)函数, 他们都是实现多线城的创建的函数,而且他们拥有相同的使用方法,相同的参数 ...