题解:矩阵快速幂模板题。
#include <stdio.h>
#include <string.h>
const int N = 10;
struct Mat {
int g[N][N];
}res, ori;
int n, k;
Mat multiply(Mat x, Mat y) {
Mat temp;
memset(temp.g, 0, sizeof(temp.g));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int l = 0; l < n; l++)
temp.g[i][j] = (temp.g[i][j] + x.g[i][l] * y.g[l][j]) % 9973;
return temp;
}
void calc(int m) {
while (m) {
if (m & 1)
res = multiply(res, ori);
m >>= 1;
ori = multiply(ori, ori);
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(res.g, 0, sizeof(res.g));
memset(ori.g, 0, sizeof(ori.g));
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
res.g[i][i] = 1;
for (int j = 0; j < n; j++)
scanf("%d", &ori.g[i][j]);
}
calc(k);
int ans = 0;
for (int i = 0; i < n; i++)
ans = (ans + res.g[i][i]) % 9973;
printf("%d\n", ans);
}
return 0;
}