-
Notifications
You must be signed in to change notification settings - Fork 0
/
10830
60 lines (49 loc) · 1.2 KB
/
10830
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;
#define MOD 1000
void matrixMultiply(vector<vector<int>>& A, vector<vector<int>>& B, int N) {
vector<vector<int>> result(N, vector<int>(N, 0));
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
for (int k=0; k<N; k++) {
result[i][j]+=(A[i][k]*B[k][j])%MOD;
result[i][j]%=MOD;
}
}
}
A=result;
}
vector<vector<int>> power(vector<vector<int>>& mat, long long b, int N) {
vector<vector<int>>result(N, vector<int>(N, 0));
for (int i=0; i<N; i++) {
result[i][i] = 1;
}
while(b>0) {
if(b%2==1) {
matrixMultiply(result, mat, N);
}
matrixMultiply(mat, mat, N);
b/=2;
}
return result;
}
int main() {
int N;
long long b;
cin>>N>>b;
vector<vector<int>> mat(N, vector<int>(N));
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
cin>>mat[i][j];
}
}
vector<vector<int>> result=power(mat, b, N);
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
cout<<result[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}