#include<cstdio>
#include<cstring>
#define mod 10
using namespace std;
int n;
struct matrix{
int d[50][50];
};
matrix add(matrix a,matrix b){
matrix c;
memset(c.d,0,sizeof(c.d));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
c.d[i][j]=(a.d[i][j]+b.d[i][j])%mod;
return c;
}
matrix mutl(matrix a,matrix b){
matrix c;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
c.d[i][j]=0;
for(int k=0;k<n;k++)
c.d[i][j]=(c.d[i][j]+a.d[i][k]*b.d[k][j])%mod;
}
return c;
}
matrix qcal(matrix a,int k){
matrix res;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i==j) res.d[i][j]=1;
else res.d[i][j]=0;
while(k){
if(k&1)
res=mutl(res,a);
k>>=1;
a=mutl(a,a);
}
return res;
}
matrix solve(matrix a,int k){
if(k==1)
return a;
matrix t=solve(a,k/2);
matrix res=add(t,mutl(t,qcal(a,k/2)));
if(k&1)
res=add(res,qcal(a,k));
return res;
}
int main(){
int k;
while(scanf("%d%d",&n,&k)==2 && n){
matrix t;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%d",&t.d[i][j]);
t.d[i][j]%=10;
}
t=solve(t,k);
for(int i=0;i<n;i++){
printf("%d",t.d[i][0]);
for(int j=0;j<n;j++)
printf(" %d",t.d[i][j]);
printf("\n");
}
printf("\n");
}
}
uva 11149 矩阵快速幂 求矩阵1-n次幂的和
最新推荐文章于 2022-05-07 01:48:51 发布