-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdpr.cpp
67 lines (55 loc) · 1.41 KB
/
dpr.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
59
60
61
62
63
64
65
66
67
// Ivan Carvalho
// Solution to https://dmoj.ca/problem/dpr
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 50;
const int MOD = 1e9 + 7;
struct matrix {
ll mat[MAXN][MAXN];
matrix(int idd = 0) {
for (int i = 0; i < MAXN; i++) {
for (int j = 0; j < MAXN; j++) {
mat[i][j] = (idd) ? (i == j) : (0);
}
}
}
matrix operator*(const matrix& other) const {
matrix ans;
for (int i = 0; i < MAXN; i++) {
for (int j = 0; j < MAXN; j++) {
for (int k = 0; k < MAXN; k++) {
ans.mat[i][j] += mat[i][k] * other.mat[k][j];
if (ans.mat[i][j] > MOD) ans.mat[i][j] %= MOD;
}
}
}
return ans;
}
};
matrix exponentiation(matrix base, ll K) {
matrix ans(1);
for (int i = 0; (1LL << i) <= K; i++) {
if ((1LL << i) & K) ans = ans * base;
base = base * base;
}
return ans;
}
int main() {
matrix base;
int N;
ll K;
cin >> N >> K;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) cin >> base.mat[i][j];
}
matrix ans = exponentiation(base, K);
ll paths = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
paths = (paths + ans.mat[i][j]) % MOD;
}
}
cout << paths << endl;
return 0;
}