-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3233.cpp
45 lines (43 loc) · 1.27 KB
/
3233.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define fromto(from,to,i) for(int (i)=(from);(i)<=(to);++(i))
using namespace std;
int n,M;
int A[3][3][31][31],B[3][3][31][31],temp[3][3][31][31];
void Apower(unsigned int __n)
{
if(__n%2)
memcpy(B,A,sizeof(A));
else fromto(1,2,i) fromto(1,n,j) B[i][i][j][j]=1;
while (__n >>= 1) {
memset(temp,0,sizeof(temp));
fromto(1,2,i) fromto(1,2,j) fromto(1,2,k) fromto(1,n,x) fromto(1,n,y) fromto(1,n,z)
temp[i][j][x][y]=(temp[i][j][x][y]+A[i][k][x][z]*A[k][j][z][y])%M;
//A*=A;
memcpy(A,temp,sizeof(temp));
if (__n % 2) {
memset(temp,0,sizeof(temp));
fromto(1,2,i) fromto(1,2,j) fromto(1,2,k) fromto(1,n,x) fromto(1,n,y) fromto(1,n,z)
temp[i][j][x][y]=(temp[i][j][x][y]+A[i][k][x][z]*B[k][j][z][y])%M;
//B*=A;
memcpy(B,temp,sizeof(temp));
}
}
}
int main()
{
int k;
scanf("%d%d%d",&n,&k,&M);
fromto(1,n,i) fromto(1,n,j) {
scanf("%d",&A[1][1][i][j]);
A[1][2][i][j]=A[1][1][i][j];
A[2][1][i][j]=0;
A[2][2][i][j]=(i==j);
}
Apower(k);
fromto(1,n,i) fromto(1,n,j)
printf(j==n?"%d\n":"%d ",B[1][2][i][j]);
//system("pause");
return 0;
}