标签:matrix mat pow clu name cci cout char argv
题意:给你一个n,输出Fibonacci (n)%10000的结果
思路:裸矩阵快速幂乘,直接套模板
代码:
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int N=2,M=2,P=2; const int MOD=10000; struct Matrix { ll m[N][N]; }; Matrix A={1,1, 1,0}; Matrix I={1,0, 0,1}; Matrix multi(Matrix a,Matrix b) { Matrix ans; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { ans.m[i][j]=0; for(int k=0;k<P;k++) { ans.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD; } ans.m[i][j]%=MOD; } } return ans; } Matrix power(Matrix a,int k) { Matrix ans=I,p=a; while(k) { if(k&1) { ans=multi(ans,p); } k>>=1; p=multi(p,p); } return ans; } int main(int argc, char const *argv[]) { int n; while(scanf("%d",&n)!=-1) { if(n==-1) break; if(n==0) { cout<<"0"<<endl; continue; } Matrix ans=power(A,n-1); printf("%lld\n",ans.m[0][0] ); } return 0; }
poj 3070 Fibonacci (矩阵快速幂乘/模板)
标签:matrix mat pow clu name cci cout char argv
原文地址:http://www.cnblogs.com/simplekinght/p/6659656.html