题目链接:传送门

题目大意:

  求斐波那契数列第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. 剑指 offer 面试题31 连续子数组的最大和(动态规划)

    求连续子数组的最大和 题目描述 给定一个整形数组,有正数也有负数,数组中连续一个或多个组成一个子数组,求所有子数组的和的最大值,要求时间复杂度为O(n); 测试用例 给定数组 {1,-2,3,10,- ...

  2. hibernate配置log

    hibernate依赖jboss-logging,通过它选择对应的对应的日志包,选择的逻辑课查看具体代码org.jboss.logging.LoggerProviders. 先通过系统变量(org.j ...

  3. MAVEN ECLIPSE JAR工程

    在eclipse 空白处点击鼠标右键选择新建 project 选择maven project: 选择Create a simple project Group ID: Artifact ID:创建项目 ...

  4. Win10系列:JavaScript动画4

    上面介绍的动画效果是通过Windows动画库创建的,这里的旋转动画是通过设置页面元素的style对象的相关属性来创建,此动画的效果是将界面元素沿着指定的方向进行旋转.下面介绍style对象的几个常用属 ...

  5. JS&Jquery中的循环/遍历

    JavaScript中的遍历 for 遍历 var anArray = ['one','two']; for(var n = 0; n < anArray.length; n++) { //具体 ...

  6. ubuntu compile openjdk87

    0. use oracle JDK,not OpenJDK 1. 遇到错误Error:./gamma: relocation error: /usr/lib/jvm/java-7-openjdk-am ...

  7. asp.net中HttpModule扩展的浅析

    在asp.net中,我们可以使用HttpModule扩展自己的相关业务.可以在HttpApplication的19个管道事件中注入我们自己的业务逻辑代码. 闲话不说,代码先上. 一.新建网站项目 我们 ...

  8. C++类型转换的注意事项

    1.如果两个类型可以相互转换,就说他们是关联的. 2.隐式转换是指,由编译器自行转换,而不需要程序员介入的转换. 3.以下情况,编译器会发生隐式转换: 1)在大多数表达式中,比int类型小的整型值会被 ...

  9. git 继续前进篇

    * git 输入 git log (--all)命令后出现<END>标记? 按q退出历史记录列表即可 * 继续前一天的  继续推送到github  步骤看图 先 链接到 之前工作区 的 文 ...

  10. L323 英语有必要学语法吗

    The Agony and Ecstasy of Grammar “Underline a relative clause.” This challenge would give a lot of a ...