Tr ATime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3615 Accepted Submission (s): 2695
Problem Descriptiona is a square, tr a represents the trace of a (which is the and of the main diagonal), and now requires TR (a^k)%9973.
The first line of the input data is a T, which indicates that there is a T group of data.
The first row of each group of data has n (2 <= n <= 10) and K (2 <= K < 10^9) two data. Next there are n rows, each row has n data, and the range of each data is [0,9], which represents the contents of square a.
Output corresponds to each set of data and outputs TR (a^k)%9973.
Sample Input
22 21 00 13 999999991 2 34 5 67 8 9
Sample Output
22686
Authorxhd
Sourcehdu 2007-1 Programming Contest
Recommendlinle | We have carefully selected several similar problems for you:2256 2604 2254 3117 2276
#include <stdio.h> #include <string.h>struct matrix{int m[11][11];}; int N;matrix multi (Matrix A,matrix b) {matrix temp;for (int i=0;i<n;++i) {for (int j=0;j<n;++j) {temp.m[i][j]=0;for ( int k=0;k<n;++k) temp.m[i][j]= (temp.m[i][j]+a.m[i][k]*b.m[k][j])%9973;}} return temp;} Matrix Fast (Matrix P,int k) {Matrix Ans;memset (ans.m,0,sizeof (ANS.M)); for (int i=0;i<n;++i) Ans.m[i][i]=1;while (k) { if (k&1) Ans=multi (ans,p);p =multi (p,p); k>>=1;} return ans;} int solution (matrix a) {int ans=0;for (int i=0;i<n;++i) {ans= (ans+a.m[i][i])%9973;} return ans;} int main () {int t,k;scanf ("%d", &t), while (t--) {matrix a;scanf ("%d%d", &n,&k), and for (int. I=0;i<n;++i) {for ( int j=0;j<n;++j) scanf ("%d", &a.m[i][j]);} Matrix C=fast (a,k); printf ("%d\n", Solution (c));} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDOJ-1575-TR A "Matrix fast Power"