51nod1113 Question: Matrix Fast Powers
The fast power of a matrix is exactly the same as the ordinary fast power idea, which is to replace the number with a matrix. When I wrote this question, my mind was drawn, and I forgot the definition of the unit matrix.
Identity Matrix: 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1.
Finally, the code is attached, the structure is useless, and it is better understood (in fact, it will not be used).
1 #include<iostream> // It took three days to write it out, but the result was just a mistake in the concept of the unit matrix, otherwise it was done the first time 2 #include<cstdio> 3 #include<cmath> 4 #include< algorithm> 5 #include<cstring> 6 using namespace std; 7 typedef long long ll; 8 const ll mod=1e9+ 7 ; 9 ll c[ 150 ][ 150 ],m,s[ 150 ][ 150 ],a[ 150 ][ 150 ]; 10 int n; 11 void chen(ll (*p)[150],ll (*q)[150]) 12 { 13 ll zj[150][150]; 14 memset(zj,0,sizeof(zj)); 15 /*for(int i=0;i<n;i++) 16 { 17 for(int j=0;j<n;j++) 18 { 19 cout<<q[i][j]; 20 } 21 cout<<endl; 22 }*/ 23 int i,j,k; 24 for(i=0;i<n;i++) 25 for(j=0;j<n;j++) 26 { 27 for(k=0;k<n;k++) 28 { 29 zj[i][j]+=(((p[i][k])%mod)*((q[k][j])%mod))%mod; 30 zj[i][j]%=mod; 31 // cout<<p[i*n+k]<<" "<<p[k*n+k]<<endl; 32 } 33 } 34 for(i=0;i<n;i++) 35 { 36 for(j=0;j<n;j++) 37 { 38 p[i][j]=zj[i][j]; 39 //cout<<zj[i][j]<<" "; 40 } 41 //cout<<endl; 42 } 43 } 44 int main() 45 { 46 scanf("%d%lld",&n,&m); 47 int i,j; 48 for(i=0;i<n;i++) 49 for(j=0;j<n;j++) 50 { 51 cin>>c[i][j]; 52 if(i==j)s[i][j]=1; 53 else s[i][j]=0; 54 } 55 while(m>0) 56 { 57 //cout<<0000000000<<endl; 58 if(m&1) chen(s,c); 59 chen(c,c); 60 m>>=1; 61 } 62 for(i=0;i<n;i++) 63 { 64 for(j=0;j<n;j++) 65 { 66 printf("%lld",s[i][j]); 67 if(j!=n-1) printf(" "); 68 } 69 printf("\n"); 70 } 71 return 0; 72 }