矩阵快速幂详见上一篇
这题貌似更裸一点……
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
//Template mat
//Interface here
#define MULMOD
//Use mat(int row,int cow) to define a new EMPTY mat
//Use define MULMOD and set MOD to ENABLE MOD
#define MAXROWS 10
#define MAXCOLS 10
#ifdef MULMOD
int MOD;
#endif
//Interface End
struct Matrix
{
int Rows,Cols;
int data[MAXROWS][MAXCOLS];
void clear()
{
memset(data,0,sizeof(data));
}
Matrix (int n,int m)
:Rows(n),Cols(m)
{
clear();
}
Matrix (int n)
:Rows(n),Cols(n)
{
clear();
for (int i=0;i<n;i++)
data[i][i]=1;
}
int* operator[](const int n)
{
return data[n];
}
Matrix operator* (const Matrix& ano) const
{
Matrix result(Rows,ano.Cols);
for (int i=0;i<Rows;i++)
for (int j=0;j<ano.Cols;j++)
for (int k=0;k<Cols;k++)
{
#ifdef MULMOD
result[i][j] += data[i][k] * ano.data[k][j] % MOD;
result[i][j]%=MOD;
#else
result[i][j] += data[i][k] * ano.data[k][j];
#endif
}
return result;
}
};
Matrix QuickMatrixPow(Matrix to ,int k)
{
Matrix ans(to.Rows);
for (int i=0;i<10;i++)
ans[i][i]=1;
while (k)
{
if (k&1)
ans=ans*to;
k>>=1;
to = to*to;
}
return ans;
}
//Template mat end
int main()
{
cin.sync_with_stdio(false);
int Total;
cin>>Total;
for (int C=0;C<Total;C++)
{
int n,k;
cin>>n>>k;
Matrix A(n,n);
MOD=9973;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
cin>>A[i][j];
Matrix B(QuickMatrixPow(A,k));
int Result(0);
for (int i=0;i<n;i++)
{
Result+=B[i][i];
Result%=9973;
}
cout<<Result<<endl;
}
}