-
Notifications
You must be signed in to change notification settings - Fork 0
/
10830.cpp
58 lines (52 loc) · 1.1 KB
/
10830.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
46
47
48
49
50
51
52
53
54
55
56
57
58
#define MOD 1000
#include <iostream>
#include <cstring>
using namespace std;
int n;
long long b;
int res_matrix[5][5], sq_matrix[5][5];
void mult_sq(int m1[5][5],int m2[5][5]){
int tmp_res[5][5];
memset(tmp_res,0,sizeof(tmp_res));
for(int r=0;r<n;r++){
for(int c=0;c<n;c++){
for(int k=0;k<n;k++){
tmp_res[r][c] = (tmp_res[r][c] + ((m1[r][k] * m2[k][c]) % MOD)) % MOD;
}
}
}
for(int r=0;r<n;r++){
for(int c=0;c<n;c++){
m1[r][c] = tmp_res[r][c];
}
}
}
void solve(){
while(b > 0){
if(b % 2 == 1){
mult_sq(res_matrix,sq_matrix);
b -= 1;
}
mult_sq(sq_matrix,sq_matrix);
b /= 2;
}
for(int r=0;r<n;r++){
for(int c=0;c<n;c++){
cout << res_matrix[r][c] << ' ';
}
cout << '\n';
}
}
int main(){
cin >> n >> b;
for(int r=0;r<n;r++){
for(int c=0;c<n;c++){
cin >> sq_matrix[r][c];
}
}
for(int r=0;r<n;r++){
res_matrix[r][r] = 1;
}
solve();
return 0;
}