Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1575
# Include <iostream> # include <string. h> # include <stdlib. h >#include <cstdio >#include <algorithm> # define mod 9973 using namespace std; struct matrix {int a [11] [11];} init, res; int n, k; matrix Mult (matrix x, matrix y) {matrix tmp; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {tmp. a [I] [j] = 0; for (int k = 0; k <n; k ++) tmp. a [I] [j] = (tmp. a [I] [j] + x. a [I] [k] * y. a [k] [j]) % mod ;}return tmp ;}matrix Pow (matrix x, int k) {matrix tmp; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) tmp. a [I] [j] = (I = j);} while (k) {if (k & 1) tmp = Mult (tmp, x ); k> = 1; x = Mult (x, x);} return tmp;} int main () {int T; cin> T; while (T --) {cin> n> k; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) scanf ("% d", & init. a [I] [j]);} res = Pow (init, k); int sum = 0; for (int I = 0; I <n; I ++) sum = (sum + res. a [I] [I]) % mod; cout <sum <endl;} return 0 ;}
HDU1575: Tr A (matrix quick power template)