-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathR-Walk.cpp
More file actions
129 lines (113 loc) · 2.22 KB
/
Copy pathR-Walk.cpp
File metadata and controls
129 lines (113 loc) · 2.22 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x.size())
#define FOR(x,y,z) for(int x = y; x <= z; x++)
#define int long long
const int N = 1e5 + 2, inf = 1e18;
const int MOD = 1e9 + 7;
int SZ;
set<int> g[102];
int add(int a, int b)
{
int res = a + b;
if(res >= MOD)
return res - MOD;
return res;
}
int mult(int a, int b)
{
long long res = a;
res *= b;
if(res >= MOD)
return res % MOD;
return res;
}
struct matrix
{
int arr[102][102];
void reset()
{
memset(arr, 0, sizeof(arr));
}
void makeiden()
{
reset();
for(int i=0;i<SZ;i++)
{
arr[i][i] = 1;
}
}
matrix operator + (const matrix &o) const
{
matrix res;
for(int i=0;i<SZ;i++)
{
for(int j=0;j<SZ;j++)
{
res.arr[i][j] = add(arr[i][j], o.arr[i][j]);
}
}
return res;
}
matrix operator * (const matrix &o) const
{
matrix res;
for(int i=0;i<SZ;i++)
{
for(int j=0;j<SZ;j++)
{
res.arr[i][j] = 0;
for(int k=0;k<SZ;k++)
{
res.arr[i][j] = add(res.arr[i][j] , mult(arr[i][k] , o.arr[k][j]));
}
}
}
return res;
}
};
matrix power(matrix a, int b)
{
matrix res;
res.makeiden();
while(b)
{
if(b & 1)
{
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, k;
cin >> n >> k;
SZ = n;
//Remember : nodes are kept 0-indexed
FOR(i,0,n-1) FOR(j,0,n-1) {
int yes;
cin >> yes;
if(yes) {
g[i].insert(j);
}
}
matrix M;
FOR(start,0,n-1) {
FOR(to,0,n-1) {
if(g[start].count(to) > 0)
M.arr[start][to] = 1;
else
M.arr[start][to] = 0;
}
}
M = power(M, k);
int ans = 0;
FOR(start,0,n-1) FOR(to,0,n-1) {
ans = (ans + M.arr[start][to]) % MOD;
}
cout << ans << '\n';
}