-
Notifications
You must be signed in to change notification settings - Fork 2
/
R - Walk.cpp
56 lines (49 loc) · 1.1 KB
/
R - Walk.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
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int md = 1e9+7;
int n;
struct M {
vector<vector<int>> t;
M () {
t.resize(n, vector<int>(n, 0));
}
M operator* (const M &b) const {
M c = M();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
(c.t[i][j] += t[i][k]*b.t[k][j]%md) %= md;
}
}
}
return c;
}
};
M exp(M &adj, int k) {
M res = M();
for (int i = 0; i < n; i++) res.t[i][i] = 1;
while (k > 0) {
if (k & 1)
res = res * adj;
adj = adj * adj;
k >>= 1;
}
return res;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int k; cin>>n>>k;
M adj = M();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin>>adj.t[i][j];
adj = exp(adj, k);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
ans = (ans + adj.t[i][j]) % md;
}
cout<<ans;
}