【矩阵快速幂】 斐波那契数列求解。
来源:互联网 发布:不配说爱我网络 编辑:程序博客网 时间:2024/05/21 06:58
Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11123 Accepted: 7913
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0
9
999999999
1000000000
-1
Sample Output
0
34
626
6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
题意:给你一个求解第N个斐波那契数的公式。 让你求出Fn % 10000。
Solution:
都要比赛了,居然矩阵快速幂写得如此的不熟。。
思路:构造单位矩阵,使用矩阵快速幂。
PS
写矩阵乘法的时侯。
Matrix matrix_mul(Matrix A,Matrix B){ Matrix C; memset(C.m,0,sizeof(C.m)); for (int i=0;i<=1;i++) for (int k=0;k<=1;k++){ int r=A.m[i][k]; for (int j=0;j<=1;j++) C.m[i][j]=(C.m[i][j]+(r*B.m[k][j])%M)%M; } return C;}
这样的写法比下面的写法会快一些~
matrix multi(matrix a, matrix b){ matrix tmp; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { tmp.m[i][j] = 0; for(int k = 0; k < 2; ++k) tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD; } } return tmp;}
为什么呢。
大神说这是缓存读取数据比内存读取数据快得多的原因,
还不不懂。
www.jb51.net/aricle/36422.htm
这个地方有详细的解释~
接下来就是一个基本的矩阵快速幂,
注意事项都在程序里面写了,
注意多写写,增加熟练度。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct Matrix{ int m[2][2];}ans,base;int n;void test(){ for (int i=0;i<=1;i++){ for (int j=0;j<=1;j++) cout<<ans.m[i][j]<<' '; cout<<endl; }}/* *This way to improve the efficiency*/const int M=1e4;Matrix matrix_mul(Matrix A,Matrix B){ Matrix C; memset(C.m,0,sizeof(C.m)); for (int i=0;i<=1;i++) for (int k=0;k<=1;k++){ int r=A.m[i][k]; for (int j=0;j<=1;j++) C.m[i][j]=(C.m[i][j]+(r*B.m[k][j])%M)%M; } return C;}/* *The n_th power of matrix base. *initialize base and initialize ans as identify matix. *<pay attention here>*/ int power_matrix_mod(int n){ base.m[1][1]=0; base.m[0][0]=base.m[0][1]=base.m[1][0]=1; ans.m[0][0]=ans.m[1][1]=1; ans.m[0][1]=ans.m[1][0]=0; while(n){ if(n&1) ans=matrix_mul(ans,base); base=matrix_mul(base,base); n>>=1; } return ans.m[0][1];} int main(){ scanf("%d",&n); printf("%d ",power_matrix_mod(n)); return 0;}
- 【矩阵快速幂】 斐波那契数列求解。
- 1242 斐波那契数列的第N项 运用矩阵快速幂来求解斐波那契数列问题
- 矩阵快速幂求解任意初始值f1,f2及a,b的斐波那契数列
- hdu3117(斐波那契数列+矩阵快速幂)
- 矩阵快速幂 斐波那契数列
- EOJ 1499 【斐波那契数列】【矩阵快速幂】
- hdu4549 M斐波那契数列(矩阵快速幂)
- hihoCoder1143 DP 斐波那契数列矩阵快速幂
- nod51-1242斐波那契数列-矩阵快速幂
- 斐波那契数列矩阵快速幂
- 斐波那契数列 打表+矩阵快速幂
- 斐波那契数列(矩阵快速幂)
- *矩阵快速幂(斐波那契数列模板)
- 矩阵快速幂(斐波那契数列)
- HDU4549 M斐波那契数列【矩阵快速幂】
- 斐波那契数列 (矩阵快速幂)
- 斐波那契数列 (矩阵快速幂)
- 矩阵快速幂--斐波那契数列
- 设计模式--工厂模式
- eclipse建javaEE项目的常见错误(不定期更新)
- HDOJ 1285 确定比赛名次(拓扑排序)
- hdu 5965 扫雷 ccpc 2016 合肥站
- 总结最近一周遇到的开发问题.
- 【矩阵快速幂】 斐波那契数列求解。
- 学 Java 有必要看数据结构的书吗?如果是,那么哪本书比较好?
- 鼠标滚轮事件
- spring学习总结(五):IOC & DI 配置 Bean 之自动装配及bean之间的关系
- level遍历102. Binary Tree Level Order Traversal
- 如果有人让你推荐编程技术书,请叫他看这个列表
- 【bzoj 1179】[Apio2009]Atm(Tarjan+spfa)
- leetcode(E)two sum
- [hdu 5973 Game of Taking Stones] Wythoff Game+大数运算