标签:
1 const int SIZ=100; 2 int MOD=100; 3 4 struct mat 5 { 6 int n,m; 7 int ar[SIZ][SIZ]; 8 mat() 9 { 10 memset(ar,0,sizeof(ar)); 11 n=m=SIZ; 12 }; 13 }; 14 15 //矩阵乘法 16 mat operator *(mat a,mat b) 17 { 18 mat c; 19 c=mat(); 20 c.n=a.n; 21 c.m=b.m; 22 for(int i=1;i<=a.n;i++) 23 for(int j=1;j<=b.m;j++) 24 for(int k=1;k<=a.m;k++) 25 { 26 c.ar[i][j]+=(a.ar[i][k]*b.ar[k][j])%MOD; 27 c.ar[i][j]%=MOD; 28 } 29 return c; 30 } 31 32 //矩阵加法 33 mat operator +(mat a,mat b) 34 { 35 mat c; 36 c=mat(); 37 c.n=a.n; 38 c.m=a.m; 39 for(int i=1;i<=a.n;i++) 40 for(int j=1;j<a.m;j++) 41 c.ar[i][j]=a.ar[i][j]+b.ar[i][j]; 42 return c; 43 } 44 45 //矩阵快速幂 46 mat operator ^(mat a,int k) 47 { 48 mat c; 49 c=mat(); 50 c.n=a.n; 51 c.m=a.m; 52 for(int i=1;i<=a.n;i++) 53 c.ar[i][i]=1; 54 while(k) 55 { 56 if(k&1) 57 c=c*a; 58 a=a*a; 59 k/=2; 60 } 61 return c; 62 }
标签:
原文地址:http://www.cnblogs.com/wsruning/p/4673001.html