标签:
解题思路:int型的注意,+= (a * b) % 9973时小心溢出了
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 10
#define mod 9973
typedef long long ll;
struct Matrix{
ll mat[maxn][maxn];
}a, b, tmp;
int n, k;
Matrix matrixMul(Matrix aa, Matrix bb) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
tmp.mat[i][j] = 0;
for(int k = 0; k < n; k++)
tmp.mat[i][j] += (aa.mat[i][k] * bb.mat[k][j]) % mod;
}
return tmp;
}
void solve() {
while(k) {
if(k & 1)
b = matrixMul(b,a);
a = matrixMul(a,a);
k >>= 1;
}
}
int main() {
int test;
scanf("%d", &test);
while(test--) {
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
b.mat[i][j] = 0;
scanf("%I64d", &a.mat[i][j]);
}
for(int i = 0; i < n; i++)
b.mat[i][i] = 1;
solve();
ll ans = 0;
for(int i = 0; i < n; i++)
ans += b.mat[i][i];
printf("%I64d\n", ans % mod);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/45832299