#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
#define MOD 9973
LL n,k;
struct Mat{
LL mat[12][12];
};
//初始化单位矩阵
Mat init(){
Mat E;
for(LL i = 0; i < n; i++){
for(LL j = 0; j < n; j++){
if(i == j)
E.mat[i][i] = 1;
else
E.mat[i][j] = 0;
}
}
return E;
}
//重载乘法
Mat operator *(Mat a,Mat b){
Mat c;
memset(c.mat,0,sizeof(Mat));
for(LL i = 0; i < n; i++){
for(LL j = 0; j < n; j++){
for(LL k = 0; k < n; k++){
if(a.mat[i][k] && b.mat[k][j]){
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
}
}
}
}
return c;
}
//重载加法
Mat operator +(Mat a,Mat b){
Mat c;
memset(c.mat,0,sizeof(Mat));
for(LL i = 0; i < n; i++){
for(LL j = 0; j < n; j++){
c.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % MOD;
}
}
return c;
}
//重载幂次方
Mat operator ^(Mat A,LL x){
if(x == 1)return A;
Mat c;
c = init();
for(; x ; x >>= 1){
if(x&1){
c = c*A;
}
A = A*A;
}
return c;
}
int main(){
int t;
scanf("%d\n",&t);
while(t--){
scanf("%I64d%I64d",&n,&k) ;
int i,j;
Mat a;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%I64d",&a.mat[i][j]);
}
}
Mat ans = a^k;
LL sum = 0;
for(i=0;i<n;i++){
sum += ans.mat[i][i];
}
printf("%I64d\n",sum % MOD);
}
return 0;
}
【矩阵快速幂】hdu 1575
最新推荐文章于 2018-10-25 20:29:02 发布