大数斐波那契【矩阵快速幂】
2017-11-16 21:03
357 查看
#include <bits/stdc++.h> using namespace std; const int N = 4 ; long long int n; struct xx { int a ; } ori,res; xx mul(xx x,xx y) { xx temp; memset(temp.a,0,sizeof(temp.a)); for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { for(int k=0; k<N; k++) { temp.a[i][j]+=x.a[i][k]*y.a[k][j]; temp.a[i][j]=temp.a[i][j]%1000000007; } } } return temp; } void init() { ori.a[0][0] = 1, ori.a[0][1] = 1; ori.a[1][0] = 1, ori.a[1][1] = 0; for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { res.a[i][j]=i==j?1:0; } } } void calc(long long k) { while(k) { if(k%2==1) { res=mul(ori,res); k-=1; } else { ori=mul(ori,ori); k/=2; } } printf("%d\n", res.a[0][0]); } int main() { int t; cin>>t; while(t--) { cin>>n; init(); calc(n); } }
相关文章推荐
- Number Sequence (斐波那契的矩阵快速幂)
- 蓝桥杯历届试题----斐波那契(矩阵快速幂)
- 10299 Problem A: Modular Fibonacci(斐波那契的矩阵快速幂)
- 斐波那契数列的第N项(1≤n≤10^18 矩阵快速幂)
- poj 3070 Fibonacci 【矩阵快速幂 求第N个斐波那契数%1000】
- hdu5171 GTY's birthday gift 矩阵快速幂求斐波那契前n项和,矩阵快速幂模板
- 斐波那契的矩阵快速幂
- 快速矩阵幂求斐波那契数
- HDU 3117Fibonacci Numbers(求斐波那契前四位与后四位 数论知识+矩阵快速幂)
- 矩阵快速幂-GCD-斐波那契-HZAU2017现场赛-D
- [POJ](3070)Fibonacci ---矩阵快速幂与斐波那契
- 斐波那契数列 打表+矩阵快速幂
- ZOJ -- 2317(矩阵快速幂 + 大数简单处理)
- 2017.11.8. 矩阵快速幂求斐波那契数
- 矩阵乘法快速幂 codevs 1574 广义斐波那契数列
- 斐波那契 矩阵快速幂
- POJ 3070(矩阵快速幂,求斐波那契第n项)
- 矩阵乘法+快速幂求斐波那契
- 斐波那契数列数组递推,普通递归,记忆化搜索,矩阵快速幂,和公式法
- POJ3070 斐波那契矩阵快速幂