标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3409 Accepted Submission(s): 2542
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 6 using namespace std; 7 8 #define MOD 9973 9 #define MAXN 15 10 11 typedef struct MAT 12 { 13 int d[MAXN][MAXN]; 14 int r, c; 15 MAT() 16 { 17 r = c = 0; 18 memset(d, 0, sizeof(d)); 19 } 20 }MAT; 21 22 MAT mul(MAT m1, MAT m2, int mod) 23 { 24 MAT ans = MAT(); 25 ans.r = m1.r; 26 ans.c = m2.c; 27 for(int i = 1; i <= m1.r; i++) 28 { 29 for(int j = 1; j <= m2.r; j++) 30 { 31 if(m1.d[i][j]) 32 { 33 for(int k = 1; k <= m2.c; k++) 34 { 35 ans.d[i][k] = (ans.d[i][k] + m1.d[i][j] * m2.d[j][k]) % mod; 36 } 37 } 38 } 39 } 40 return ans; 41 } 42 43 MAT quickmul(MAT m, int n, int mod) 44 { 45 MAT ans = MAT(); 46 for(int i = 1; i <= m.r; i++) 47 { 48 ans.d[i][i] = 1; 49 } 50 ans.r = m.r; 51 ans.c = m.c; 52 while(n) 53 { 54 if(n & 1) 55 { 56 ans = mul(m, ans, mod); 57 } 58 m = mul(m, m, mod); 59 n >>= 1; 60 } 61 return ans; 62 } 63 64 int main() 65 { 66 int T; 67 scanf("%d", &T); 68 while(T--) 69 { 70 int n, k; 71 scanf("%d %d", &n, &k); 72 MAT A = MAT(); 73 A.r = n, A.c = n; 74 for(int i = 1; i <= n; i++) 75 { 76 for(int j = 1; j <= n; j++) 77 { 78 scanf("%d", &A.d[i][j]); 79 } 80 } 81 A = quickmul(A, k, MOD); 82 int ans = 0; 83 for(int i = 1; i <= n; i++) 84 { 85 ans += A.d[i][i]; 86 } 87 printf("%d\n", ans % MOD); 88 } 89 return 0; 90 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4575728.html