poj 3070 矩阵快速幂模板
题意:求fibonacci数列第n项
1 #include "iostream" 2 #include "vector" 3 #include "cstring" 4 using namespace std; 5 6 typedef unsigned long int ULL; 7 typedef vector<ULL> vec; 8 typedef vector<vec> mat; 9 const ULL P=10000; 10 int n,m; 11 12 mat mul(mat &A,mat &B) //return A*B 13 { 14 mat C(A.size(),vec(B[0].size())); 15 for (int i=0;i<(int)A.size();i++) 16 { 17 for (int k=0;k<(int)B.size();k++) 18 { 19 for (int j=0;j<(int)B[0].size();j++) 20 { 21 C[i][j]=(C[i][j]+A[i][k]*B[k][j])%P; 22 } 23 } 24 } 25 return C; 26 } 27 28 mat m_pow(mat A,int m) //return A^m 29 { 30 mat B(A.size(),vec(A.size())); 31 for (int i=0;i<(int)A.size();i++) 32 B[i][i]=1; 33 while (m>0) 34 { 35 if (m&1) B=mul(B,A); 36 A=mul(A,A); 37 m>>=1; 38 } 39 return B; 40 } 41 42 int main() 43 { 44 while (cin>>n) 45 { 46 if (n==-1) break; 47 mat A(2,vec(2)); 48 49 A[0][0]=1; A[0][1]=1; 50 A[1][0]=1; A[1][1]=0; 51 52 A=m_pow(A,n); 53 cout<<A[1][0]<<endl; 54 } 55 }
posted on 2014-10-30 12:33 Pentium.Labs 阅读(159) 评论(0) 编辑 收藏 举报