题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575
算是模板吧
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <queue> #include <vector> #include <map> using namespace std; typedef long long LL; #define met(a, b) memset(a, b, sizeof(a)) #define INF 0x3f3f3f3f #define N 15 #define MOD 9973 struct node { int a[N][N]; }; int n, k; node mul(node p, node q) { node temp; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { temp.a[i][j] = 0; for(int k=0; k<n; k++) temp.a[i][j] = (temp.a[i][j]+p.a[i][k]*q.a[k][j])%MOD; } } return temp; } node Pow(node A, int m) { node temp; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { temp.a[i][j] = 0; } temp.a[i][i] = 1; } while(m) { if(m % 2) temp = mul(temp, A); m/=2; A = mul(A, A); } return temp; } int slove(node A) { node ans = Pow(A, k); int sum = 0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i==j) sum = (sum + ans.a[i][j]) % MOD; } } return sum; } int main() { int T; scanf("%d", &T); while(T--) { node A; scanf("%d %d", &n, &k); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) scanf("%d", &A.a[i][j]); } int ans = slove(A); printf("%d ", ans); } return 0; }