传送门
题意:求给定n维方阵的k次幂后的对角线元素和
思路:模板题
代码如下:
#include
int N;//N个系数,N维矩阵
typedef long long ll;
using namespace std;
struct matrix
{
int m[20][20];
};
matrix ans, base, m;
matrix multi(matrix a, matrix b,ll mod,int N)
{
matrix tmp;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
tmp.m[i][j] = 0;
for (int k = 0; k < N; ++k)
tmp.m[i][j] = (tmp.m[i][j] + (a.m[i][k] * b.m[k][j])) % mod;
}
}
return tmp;
}
matrix fastm(matrix base, ll n,ll mod,int N)
{
int i = 0, j = 0;
matrix ans;
memset(ans.m, 0, sizeof(ans.m));
for (i = 0; i < N; i++)
ans.m[i][i] = 1;
while (n)
{
if (n & 1)
{
ans = multi(ans, base,mod,N);
}
base = multi(base, base,mod,N);
n >>= 1;
}
return ans;
}
int main()
{
ll k,mod=9973, sum=0 ;
matrix m, ans;
int t;cin>>t;
while(t--)
{
sum=0;
int n,k;
cin>>n>>k;
N=n;
for(int i=0;i>m.m[i][j];
}
ans = fastm(m,k,mod,n);
for (int i = 0; i < N; i++)
sum += ans.m[i][i];
printf("%d\n", sum%mod);
}
return 0;
}