矩阵幂模板题。AC自动机+矩阵的题没想法,过来刷刷矩阵的题,熟悉下矩阵。
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 2000005
#define INF 9973
typedef long long LL;
int n, m;
struct Mar{
LL s[15][15];
};
Mar mut(Mar a, Mar b){
Mar ans;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ans.s[i][j] = 0;
for(int k = 0; k < n; k++){
ans.s[i][j] += (a.s[i][k] * b.s[k][j]) % INF;
ans.s[i][j] %= INF;
}
}
}
return ans;
}
Mar Mpow(Mar a, int k){
Mar ret;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ret.s[i][j] = 0;
if(i == j)ret.s[i][j] = 1;
}
}
while(k){
if(k & 1)ret = mut(a, ret);
a = mut(a, a);
k >>= 1;
}
return ret;
}
int main(){
int t;
scanf("%d", &t);
Mar a;
while(t--){
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%I64d", &a.s[i][j]);
}
}
a = Mpow(a, m);
LL ans = 0;
for(int i = 0; i < n; i++){
ans += a.s[i][i];
ans %= INF;
}
cout << ans << endl;
}
return 0;
}