-
Notifications
You must be signed in to change notification settings - Fork 1
/
05095.cpp
46 lines (40 loc) · 1.16 KB
/
05095.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
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > matmul(int n, int m, vector<vector<int> > a, vector<vector<int> > b) {
vector<vector<int> > ret(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++) {
ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % m;
}
}
}
return ret;
}
vector<vector<int> > matexp(int n, int m, int p, vector<vector<int> > a) {
if (p == 1) return a;
vector<vector<int> > half = matexp(n, m, p/2, a);
if (p & 1) return matmul(n, m, matmul(n, m, half, half), a);
else return matmul(n, m, half, half);
}
bool solve(void) {
int n, m, p; cin >> n >> m >> p;
if (n == 0 && m == 0 && p == 0) return false;
vector<vector<int> > v(n, vector<int>(n));
for (int i=0; i<n; i++) for (int j=0; j<n; j++) cin >> v[i][j];
vector<vector<int> > ans = matexp(n, m, p, v);
for (int i=0; i<n; i++) {
cout << ans[i][0];
for (int j=1; j<n; j++) cout << " " << ans[i][j];
cout << "\n";
}
cout << "\n";
return true;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
while (solve());
return 0;
}