Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1575
If a 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 row of data is a T, indicating that there is a T group of data.
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.
A bare Question of the Rapid power of a matrix.
Question:
# Include <iostream> # include <stdio. h ># include <cstring> # define mod 9973 using namespace STD; const int max = 11; struct matrix {int V [Max] [Max] ;}; int N, K, m; matrix mtmul (matrix A, matrix B) // evaluate the matrix A * B {int I, j, k; matrix C; for (I = 0; I <N; I ++) for (j = 0; j <n; j ++) {C. V [I] [J] = 0; For (k = 0; k <n; k ++) C. V [I] [J] = (. V [I] [k] * B. V [k] [J] + C. V [I] [J]) % MOD;} return C;} matrix mtpow (matrix A, int K) // evaluate the matrix A ^ K {If (k = 0) {memset (. v, 0, sizeof (. v); For (INT I = 0; I <n; I ++). V [I] [I] = 1; return a;} If (k = 1) return a; matrix C = mtpow (A, K/2 ); if (K % 2 = 0) return mtmul (C, C); else return mtmul (C, C), a);} int solv (matrix) {int ans = 0; For (INT I = 0; I <n; I ++) ans + =. V [I] [I] % MOD; return ans;} void out (matrix A) {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) printf ("% d",. V [I] [J]); cout <Endl ;}} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & K); matrix A; For (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) scanf ("% d", &. V [I] [J]); matrix ans; ans = mtpow (A, K); // out (ANS); cout <solv (ANS) % mod <Endl ;}}