Matrix Power Series
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
typedef long long ll;
const int N = 100;
int mod;
int n,M,K;
struct Matrix {
ll mat[N][N];
Matrix operator * (const Matrix m)const {
Matrix tmp;
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]+=mat[i][k]*m.mat[k][j]%mod;
tmp.mat[i][j]%=mod;
}
}
}
return tmp;
}
};
Matrix Pow(Matrix &m,int k){
Matrix ans;
memset (ans.mat,0,sizeof (ans.mat));
for (int i=0;i<n;i++)ans.mat[i][i]=1;
while (k){
if (k&1)ans=ans*m;
k>>=1;
m=m*m;
}
return ans;
}
int main ()
{
while (scanf ("%d%d%d",&n,&K,&M)!=EOF){
Matrix ans;
memset (ans.mat,0,sizeof (ans.mat));
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
scanf ("%d",&ans.mat[i][j]);
}
}
for (int i=0;i<n;i++)ans.mat[i][i+n]=1;
for (int i=0;i<n;i++)ans.mat[i+n][i+n]=1;
n=n*2;
mod = M;
ans=Pow(ans,K+1);
for (int i=0;i<n/2;i++)
ans.mat[i][i+n/2]=(ans.mat[i][i+n/2]-1+mod)%mod;
for (int i=0;i<n/2;i++){
for (int j=n/2;j<n;j++){
if (j==n/2)printf ("%lld",ans.mat[i][j]);
else printf (" %lld",ans.mat[i][j]);
}
printf ("\n");
}
}
return 0;
}