2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9
2 2686
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int mod=9973;
int n;
struct node {
int mp[20][20];
} init,res;
struct node Mult(struct node x, struct node y)// 矩阵相乘
{
struct node tmp;
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++) {
tmp.mp[i][j]=0;
for(k=0;k<n;k++)
tmp.mp[i][j]=(tmp.mp[i][j]+x.mp[i][k]*y.mp[k][j])%mod;
}
return tmp;
};
struct node expo(struct node x, int k)//矩阵快速幂
{
struct node tmp;
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++) {
if(i==j)
tmp.mp[i][j]=1;
else
tmp.mp[i][j]=0;
}
while(k) {
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
};
int main()
{
int T,i,j,k;
int ans;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&n,&k);
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d",&init.mp[i][j]);
res=expo(init,k);
ans=0;
for(i=0; i<n; i++)
ans=(ans+res.mp[i][i])%mod;
printf("%d\n",ans);
}
return 0;
}
原文:http://blog.csdn.net/u013486414/article/details/44063785