HDU 1575 Tr A (matrix fast power), hdu1575
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 5537 Accepted Submission (s): 4161
If Problem DescriptionA is A square matrix, Tr A indicates the trace of A (the sum of all items on the main diagonal). Tr (A ^ k) % 9973 is required.
The first line of Input data is a T, indicating that T groups of data exist.
The first row of each data group contains n (2 <= n <= 10) and k (2 <= k <10 ^ 9) data. Next there are n rows, each row has n data, and the range of each data is [], which indicates the content of square matrix.
Output corresponds to each group of data, and Output Tr (A ^ k) % 9973.
Sample Input22 21 00 13 999999991 2 34 5 67 8 9
Sample Output22686
Authorxhd
SourceHDU 2007-1 Programming Contest
Recommendlinle | We have carefully selected several similar problems for you: 1757 1588 2256 2604 2254 Matrix Quick power !. We need to create a new matrix to meet the following requirements: the diagonal line is 1, the remaining is 0, and then run the Matrix Quick power.
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int MAXN=101; 5 inline void read(int &n){char c='+';bool flag=0;n=0; 6 while(c<'0'||c>'9') c=='-'?flag=1,c=getchar():c=getchar(); 7 while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;} 8 struct matrix 9 {10 int m[11][11];matrix(){memset(m,0,sizeof(m));}11 };12 matrix ma;13 int limit;14 const int mod=9973;15 matrix mul(matrix a,matrix b)16 {17 matrix c;18 for(int k=0;k<limit;k++)19 for(int i=0;i<limit;i++)20 for(int j=0;j<limit;j++)21 c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;22 return c;23 }24 matrix fast_martix_pow(matrix ma,int p)25 {26 matrix bg;27 for(int i=0;i<limit;i++)28 for(int j=0;j<limit;j++)29 bg.m[i][j]=(i==j);30 while(p)31 {32 if(p&1) bg=mul(bg,ma);33 ma=mul(ma,ma);34 p>>=1;35 }36 return bg;37 }38 int main()39 {40 int T;read(T);41 while(T--)42 {43 read(limit);int n;read(n);44 for(int i=0;i<limit;i++)45 for(int j=0;j<limit;j++)46 read(ma.m[i][j]);47 matrix ans=fast_martix_pow(ma,n);48 int out=0;49 for(int i=0;i<limit;i++)50 out+=ans.m[i][i]%mod;51 printf("%d\n",out%mod);52 }53 return 0;54 }