Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5537 Accepted Submission(s): 4161 Problem Descrip ...
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5537 Accepted Submission(s):
4161
Input 數據的第一行是一個T,表示有T組數據。
每組數據的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)兩個數據。接下來有n行,每行有n個數據,每個數據的範圍是[0,9],表示方陣A的內容。
Output 對應每組數據,輸出Tr(A^k)%9973。
Sample Input 2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9
Sample Output 2 2686
Author xhd
Source HDU 2007-1 Programming Contest
Recommend linle | We have carefully selected several similar problems for you: 1757 1588 2256 2604 2254 矩陣快速冪的裸題!。 我們需要新建一個矩陣, 滿足:對角線為1,其餘為0 然後跑一下矩陣快速冪就好
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 }