思路:矩阵快速幂
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=15;
const int mod=9973;
int n,k;
struct matrix
{
int A[maxn][maxn];
};
matrix operator * (const matrix &a,const matrix &b)
{
matrix c;
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
c.A[i][j]=0;
for(int k=0;k<n;++k)
{
c.A[i][j]+=a.A[i][k]*b.A[k][j];
c.A[i][j]%=mod;
}
}
}
return c;
}
matrix f_pow(matrix a,int p)
{
matrix ret;
memset(ret.A,0,sizeof(ret.A));
for(int i=0;i<n;++i)
{
ret.A[i][i]=1;
}
while(p)
{
if(p&1)ret=ret*a;
a=a*a;
p>>=1;
}
return ret;
}
int main()
{
//freopen("data.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
matrix 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]);
}
matrix b=f_pow(a,k);
int ans=0;
for(int i=0;i<n;++i)
{
ans+=b.A[i][i];
}
ans%=mod;
printf("%d\n",ans);
}
return 0;
}