-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF. Teletransporte.cpp
84 lines (64 loc) · 1.14 KB
/
F. Teletransporte.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vll2d = vector<vll>;
const int MAXN(100);
const ll MOD(1'0'000);
ll N, L, S, T;
vll2d ways(MAXN, vll(MAXN));
vll2d prod(vll2d &a, vll2d &b) {
vll2d c(N, vll(N, 0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
}
}
}
return c;
}
vll2d pow (vll2d &a, ll p) {
vll2d res(N, vll(N));
for (int i = 0; i < N; i++) {
res[i][i] = 1;
}
while (p) {
if (p&1) res = prod(res, a);
a = prod(a,a);
p >>= 1;
}
return res;
}
void solve() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ways[i][j] = 0;
}
}
cin >> S >> T;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 4; j++) {
int k;
cin >> k;
k--;
ways[i][k]++;
}
}
auto w2 = pow(ways, L);
cout << w2[S-1][T-1] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (cin >> N >> L) {
solve();
}
}
/*
* AC
* https://www.beecrowd.com.br/judge/pt/problems/view/1713
* Graphs
* Matrix Exponiation
* */