A - Tr A
很裸的矩阵乘法
对角线一共只有两条,试一下就好了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int mod = 9973;
int n;
struct jz{
int g[20][20];
void init(){
memset(g,0,sizeof(g));
}
void one(){
memset(g,0,sizeof(g));
for(int i = 1; i <= n; ++ i) g[i][i] = 1;
}
};
jz operator * (jz a, jz b){
jz c; c.init();
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
for(int k = 1; k <= n; ++ k)
c.g[i][j] += (a.g[i][k] * b.g[k][j]) % mod, c.g[i][j] %= mod;
return c;
}
jz ksm(jz x,int y){
jz z; z.one();
while(y){
if(y & 1) z = z * x;
y >>= 1;
x = x * x;
}
return z;
}
int k;
jz a;
int main(){
int T; scanf("%d",&T);
while(T --){
scanf("%d%d",&n,&k);
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
scanf("%d",&a.g[i][j]), a.g[i][j] %= mod;
a = ksm(a,k);
int ans = 0;
for(int i = 1; i <= n; ++ i)
ans += a.g[i][i], ans %= mod;
printf("%d\n",ans);
}
return 0;
}