矩乘快速幂入门
题目已经把题解讲得很清楚了
1 #include<cstdio> 2 #include<cstring> 3 #include<cassert> 4 #include<algorithm> 5 using namespace std; 6 #define p 10000 7 #define N 10 8 #define ll long long 9 struct matrix{ 10 int n,m; 11 int mat[N][N]; 12 matrix(){ 13 memset(mat,0,sizeof(mat)); 14 } 15 void print(){ 16 printf("%d\n",mat[0][0]%p); 17 } 18 }m0,trs; 19 matrix operator *(matrix m1,matrix m2){ 20 assert(m1.m==m2.n); 21 matrix res; 22 res.n=m1.n; 23 res.m=m2.m; 24 for (int i=0;i<m1.n;i++) 25 for (int j=0;j<m2.m;j++) 26 for (int k=0;k<m1.m;k++) 27 res.mat[i][j]+=(ll)m1.mat[i][k]*m2.mat[k][j]%p; 28 return res; 29 } 30 matrix quickpow(matrix x,int y){ 31 matrix res; 32 res.n=res.m=2; 33 res.mat[1][1]=res.mat[0][0]=1; 34 while (y){ 35 if (y&1) res=res*x; 36 x=x*x; 37 y>>=1; 38 } 39 return res; 40 } 41 int main(){ 42 ll k; 43 scanf("%lld",&k); 44 m0.m=2;m0.n=1; 45 m0.mat[0][0]=m0.mat[0][1]=1; 46 trs.m=trs.n=2; 47 trs.mat[1][1]=trs.mat[1][0]=trs.mat[0][1]=1; 48 while (k!=-1){ 49 if (k==0) printf("0\n"); 50 else (m0*quickpow(trs,k-1)).print(); 51 scanf("%lld",&k); 52 } 53 return 0; 54 }
PS:
矩乘的简单理解:http://www.ruanyifeng.com/blog/2015/09/matrix-multiplication.html
矩阵拓展:08年国家集训队论文,
http://wenku.baidu.com/link?url=O5_d_kstn_0Ym7zrCiZa1EYPuKlun-K1OHeV42s0pt64JVGvJKSf2gFKyr7hAYEm9SBu52xdUhNkYemH6blJjaaK5fhbIfPjrQuqNAU90EG