矩阵裸题, 直接上代码.
code:
/*
adrui's submission
Language : C++
Result : Accepted
Love : ll
Favorite : Dragon Balls
Standing in the Hall of Fame
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define debug 0
#define LL long long
#define mod 9973
#define M(a, b) memset(a, b, sizeof(a))
const int maxn(15);
int n, k;
struct Matrix {
int mat[maxn][maxn];
void init() {
M(mat, 0);
for (int i = 0; i < n; i++)
mat[i][i] = 1;
}
};
Matrix operator * (Matrix a, Matrix t) { //矩阵乘法
Matrix c;
M(c.mat, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
c.mat[i][j] += (a.mat[i][k] * t.mat[k][j]) % mod;
c.mat[i][j] %= mod;
}
return c;
}
Matrix operator ^ (Matrix tmp, int b) { //快速幂
Matrix res;
res.init();
while (b) {
if (b & 1) res = res * tmp;
tmp = tmp * tmp;
b >>= 1;
}
return res;
}
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
Matrix tmp;
M(tmp.mat, 0); //构造底数矩阵
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &tmp.mat[i][j]);
Matrix res = tmp ^ k; //快速幂
int ans = 0;
for (int i = 0; i < n; i++)
ans = (ans + res.mat[i][i]) % mod; //对角线
printf("%d\n", ans); //ans
}
return 0;
}</span>