显示代码纯文本
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=70;
int n,size;ll k,p,calc[N][N];
ll inc(ll x,ll y){x+=y;return x>=p?x-p:x;}
ll mul(ll x,ll y){
ll ans=0;x%=p;
for (;y;y>>=1,x=inc(x,x))
if (y&1) ans=inc(ans,x);
return ans;
}
struct matrix{
ll a[N][N];
void operator *= (const matrix &x){
for (int i=0;i<n;i++)
for (int j=0;j<size;j++){
calc[i][j]=0;
for (int k=0;k<size;k++)
calc[i][j]=inc(calc[i][j],mul(a[i][k],x.a[k][j]));
}
memcpy(a,calc,sizeof a);
}
}ans,x;
int main()
{
freopen("matrix_sum.in","r",stdin);
freopen("matrix_sum.out","w",stdout);
cin>>n>>k>>p;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++){
cin>>x.a[i][j];
x.a[i][j]%=p;
ans.a[i][j]=x.a[i][j];
}
size=n*2;
for (int i=0;i<n;i++)
calc[i+n][i+n]=x.a[i][i+n]=x.a[i+n][i+n]=1;
for (;k;k>>=1,x*=x)
if (k&1) ans*=x;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++) cout<<ans.a[i][j+n]<<' ';
cout<<endl;
}
return 0;
}