#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
constexpr int inf = 1 << 30;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, l, r) for (int i = (l); i < (r); ++i)
#define RVP(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(x) (x).begin(), (x).end()
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr int V = 5000000;
int main() {
int N, M;
cin >> N >> M;
vector<pair<short, short>> A(M);
REP(i, M) {
cin >> A[i].first >> A[i].second;
chmin(A[i].second, (short)N);
}
int S = 0, T = 0;
{
const auto as = A[0], at = A[1];
sort(ALL(A), [](auto a, auto b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
});
REP(i, M) {
if (A[i] == as) S = i;
if (A[i] == at) T = i;
}
}
vector<vector<pair<short, bool>>> lst(N);
REP(i, M) {
if ((not lst[A[i].first].empty()) and lst[A[i].first].back().first == A[i].second) {
lst[A[i].first].back().second = true;
continue;
} else {
lst[A[i].first].push_back({A[i].second, true});
}
int f = A[i].first % A[i].second;
while (f < N) {
lst[f].push_back({A[i].second, false});
f += A[i].second;
if (f == A[i].first) f += A[i].second;
}
}
vector<int> size_r(N + 1);
REP(i, N) size_r[i + 1] = size_r[i] + (int)lst[i].size();
static array<pair<int, int>, V> pair_lr;
static array<int, V> par;
vector<vector<int>> chd(N);
vector<unordered_map<short, int>> id_m(N);
REP(i, N) { REP(j, (int)lst[i].size()) id_m[i][lst[i][j].first] = size_r[i] + j; }
REP(i, N) {
for (const auto &[w, c] : lst[i]) {
const int id = id_m[i][w];
pair_lr[id] = {-1, -1};
par[id] = size_r[N] + i;
if (c) chd[i].push_back(id);
const int l = i - w;
if (l >= 0) {
const int id2 = id_m[l][w];
pair_lr[id].first = id2;
pair_lr[id2].second = id;
}
}
}
static array<int, V> dist;
fill(ALL(dist), inf);
dist[size_r[N] + A[S].first] = 0;
deque<int> deq;
deq.push_back(size_r[N] + A[S].first);
while (not deq.empty()) {
const int f = deq.front();
deq.pop_front();
if (f >= size_r[N]) {
for (const int t : chd[f - size_r[N]]) {
if (chmin(dist[t], dist[f])) {
deq.push_front(t);
}
}
} else {
const auto &[l, r] = pair_lr[f];
if (l != -1 and chmin(dist[l], dist[f] + 1)) {
deq.push_back(l);
}
if (r != -1 and chmin(dist[r], dist[f] + 1)) {
deq.push_back(r);
}
const int pa = par[f];
if (chmin(dist[pa], dist[f])) {
deq.push_front(pa);
}
}
}
auto ans = dist[size_r[N] + A[T].first];
if (ans == inf) ans = -1;
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
9 ms |
19796 KB |
Output is correct |
5 |
Correct |
10 ms |
19880 KB |
Output is correct |
6 |
Correct |
9 ms |
19796 KB |
Output is correct |
7 |
Correct |
10 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19808 KB |
Output is correct |
3 |
Correct |
10 ms |
19792 KB |
Output is correct |
4 |
Correct |
10 ms |
19804 KB |
Output is correct |
5 |
Correct |
9 ms |
19880 KB |
Output is correct |
6 |
Correct |
9 ms |
19796 KB |
Output is correct |
7 |
Correct |
9 ms |
19784 KB |
Output is correct |
8 |
Correct |
10 ms |
19796 KB |
Output is correct |
9 |
Correct |
10 ms |
19796 KB |
Output is correct |
10 |
Correct |
11 ms |
19904 KB |
Output is correct |
11 |
Correct |
11 ms |
20128 KB |
Output is correct |
12 |
Correct |
10 ms |
19896 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
13 ms |
20332 KB |
Output is correct |
15 |
Correct |
11 ms |
20232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
10 ms |
19796 KB |
Output is correct |
4 |
Correct |
10 ms |
19796 KB |
Output is correct |
5 |
Correct |
10 ms |
19796 KB |
Output is correct |
6 |
Correct |
11 ms |
19824 KB |
Output is correct |
7 |
Correct |
10 ms |
19796 KB |
Output is correct |
8 |
Correct |
10 ms |
19796 KB |
Output is correct |
9 |
Correct |
10 ms |
19796 KB |
Output is correct |
10 |
Correct |
10 ms |
19988 KB |
Output is correct |
11 |
Correct |
11 ms |
20180 KB |
Output is correct |
12 |
Correct |
11 ms |
19924 KB |
Output is correct |
13 |
Correct |
11 ms |
19924 KB |
Output is correct |
14 |
Correct |
12 ms |
20308 KB |
Output is correct |
15 |
Correct |
12 ms |
20308 KB |
Output is correct |
16 |
Correct |
10 ms |
20052 KB |
Output is correct |
17 |
Correct |
14 ms |
20820 KB |
Output is correct |
18 |
Correct |
12 ms |
20592 KB |
Output is correct |
19 |
Correct |
11 ms |
20564 KB |
Output is correct |
20 |
Correct |
11 ms |
20424 KB |
Output is correct |
21 |
Correct |
10 ms |
20052 KB |
Output is correct |
22 |
Correct |
12 ms |
20564 KB |
Output is correct |
23 |
Correct |
12 ms |
20564 KB |
Output is correct |
24 |
Correct |
13 ms |
20916 KB |
Output is correct |
25 |
Correct |
13 ms |
20756 KB |
Output is correct |
26 |
Correct |
13 ms |
20692 KB |
Output is correct |
27 |
Correct |
11 ms |
20692 KB |
Output is correct |
28 |
Correct |
16 ms |
21588 KB |
Output is correct |
29 |
Correct |
21 ms |
23888 KB |
Output is correct |
30 |
Correct |
13 ms |
21140 KB |
Output is correct |
31 |
Correct |
17 ms |
22248 KB |
Output is correct |
32 |
Correct |
17 ms |
21456 KB |
Output is correct |
33 |
Correct |
37 ms |
27456 KB |
Output is correct |
34 |
Correct |
33 ms |
27428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19796 KB |
Output is correct |
2 |
Correct |
9 ms |
19796 KB |
Output is correct |
3 |
Correct |
9 ms |
19796 KB |
Output is correct |
4 |
Correct |
9 ms |
19860 KB |
Output is correct |
5 |
Correct |
11 ms |
19796 KB |
Output is correct |
6 |
Correct |
9 ms |
19796 KB |
Output is correct |
7 |
Correct |
9 ms |
19856 KB |
Output is correct |
8 |
Correct |
9 ms |
19888 KB |
Output is correct |
9 |
Correct |
9 ms |
19796 KB |
Output is correct |
10 |
Correct |
10 ms |
19924 KB |
Output is correct |
11 |
Correct |
11 ms |
20168 KB |
Output is correct |
12 |
Correct |
12 ms |
19924 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
14 ms |
20216 KB |
Output is correct |
15 |
Correct |
11 ms |
20308 KB |
Output is correct |
16 |
Correct |
12 ms |
20088 KB |
Output is correct |
17 |
Correct |
13 ms |
20864 KB |
Output is correct |
18 |
Correct |
11 ms |
20548 KB |
Output is correct |
19 |
Correct |
11 ms |
20516 KB |
Output is correct |
20 |
Correct |
11 ms |
20464 KB |
Output is correct |
21 |
Correct |
10 ms |
20052 KB |
Output is correct |
22 |
Correct |
10 ms |
20564 KB |
Output is correct |
23 |
Correct |
12 ms |
20516 KB |
Output is correct |
24 |
Correct |
12 ms |
20948 KB |
Output is correct |
25 |
Correct |
12 ms |
20736 KB |
Output is correct |
26 |
Correct |
12 ms |
20692 KB |
Output is correct |
27 |
Correct |
12 ms |
20644 KB |
Output is correct |
28 |
Correct |
17 ms |
21584 KB |
Output is correct |
29 |
Correct |
22 ms |
23792 KB |
Output is correct |
30 |
Correct |
13 ms |
21204 KB |
Output is correct |
31 |
Correct |
15 ms |
22180 KB |
Output is correct |
32 |
Correct |
15 ms |
21484 KB |
Output is correct |
33 |
Correct |
34 ms |
27508 KB |
Output is correct |
34 |
Correct |
35 ms |
27452 KB |
Output is correct |
35 |
Correct |
46 ms |
26532 KB |
Output is correct |
36 |
Correct |
15 ms |
21076 KB |
Output is correct |
37 |
Correct |
69 ms |
30696 KB |
Output is correct |
38 |
Correct |
65 ms |
30640 KB |
Output is correct |
39 |
Correct |
65 ms |
30544 KB |
Output is correct |
40 |
Correct |
66 ms |
30620 KB |
Output is correct |
41 |
Correct |
78 ms |
30596 KB |
Output is correct |
42 |
Correct |
22 ms |
20788 KB |
Output is correct |
43 |
Correct |
22 ms |
20688 KB |
Output is correct |
44 |
Correct |
23 ms |
20648 KB |
Output is correct |
45 |
Correct |
106 ms |
47824 KB |
Output is correct |
46 |
Correct |
105 ms |
47784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19796 KB |
Output is correct |
2 |
Correct |
11 ms |
19760 KB |
Output is correct |
3 |
Correct |
10 ms |
19796 KB |
Output is correct |
4 |
Correct |
9 ms |
19872 KB |
Output is correct |
5 |
Correct |
10 ms |
19844 KB |
Output is correct |
6 |
Correct |
9 ms |
19796 KB |
Output is correct |
7 |
Correct |
10 ms |
19860 KB |
Output is correct |
8 |
Correct |
9 ms |
19796 KB |
Output is correct |
9 |
Correct |
10 ms |
19796 KB |
Output is correct |
10 |
Correct |
10 ms |
19924 KB |
Output is correct |
11 |
Correct |
11 ms |
20188 KB |
Output is correct |
12 |
Correct |
10 ms |
19924 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
11 ms |
20308 KB |
Output is correct |
15 |
Correct |
12 ms |
20308 KB |
Output is correct |
16 |
Correct |
11 ms |
20140 KB |
Output is correct |
17 |
Correct |
14 ms |
20884 KB |
Output is correct |
18 |
Correct |
11 ms |
20564 KB |
Output is correct |
19 |
Correct |
11 ms |
20460 KB |
Output is correct |
20 |
Correct |
12 ms |
20632 KB |
Output is correct |
21 |
Correct |
10 ms |
20080 KB |
Output is correct |
22 |
Correct |
11 ms |
20528 KB |
Output is correct |
23 |
Correct |
13 ms |
20564 KB |
Output is correct |
24 |
Correct |
13 ms |
20888 KB |
Output is correct |
25 |
Correct |
13 ms |
20792 KB |
Output is correct |
26 |
Correct |
12 ms |
20648 KB |
Output is correct |
27 |
Correct |
11 ms |
20692 KB |
Output is correct |
28 |
Correct |
17 ms |
21548 KB |
Output is correct |
29 |
Correct |
20 ms |
23884 KB |
Output is correct |
30 |
Correct |
12 ms |
21204 KB |
Output is correct |
31 |
Correct |
18 ms |
22264 KB |
Output is correct |
32 |
Correct |
14 ms |
21552 KB |
Output is correct |
33 |
Correct |
33 ms |
27492 KB |
Output is correct |
34 |
Correct |
34 ms |
27476 KB |
Output is correct |
35 |
Correct |
46 ms |
26580 KB |
Output is correct |
36 |
Correct |
16 ms |
21088 KB |
Output is correct |
37 |
Correct |
61 ms |
30720 KB |
Output is correct |
38 |
Correct |
66 ms |
30632 KB |
Output is correct |
39 |
Correct |
66 ms |
30604 KB |
Output is correct |
40 |
Correct |
73 ms |
30524 KB |
Output is correct |
41 |
Correct |
68 ms |
30608 KB |
Output is correct |
42 |
Correct |
22 ms |
20812 KB |
Output is correct |
43 |
Correct |
22 ms |
20748 KB |
Output is correct |
44 |
Correct |
21 ms |
20580 KB |
Output is correct |
45 |
Correct |
106 ms |
47772 KB |
Output is correct |
46 |
Correct |
107 ms |
47824 KB |
Output is correct |
47 |
Correct |
263 ms |
66764 KB |
Output is correct |
48 |
Correct |
84 ms |
37756 KB |
Output is correct |
49 |
Correct |
63 ms |
33496 KB |
Output is correct |
50 |
Correct |
58 ms |
33868 KB |
Output is correct |
51 |
Correct |
143 ms |
43920 KB |
Output is correct |
52 |
Correct |
137 ms |
45604 KB |
Output is correct |
53 |
Correct |
54 ms |
32368 KB |
Output is correct |
54 |
Correct |
20 ms |
28500 KB |
Output is correct |
55 |
Correct |
24 ms |
29652 KB |
Output is correct |
56 |
Correct |
37 ms |
29604 KB |
Output is correct |
57 |
Correct |
105 ms |
51148 KB |
Output is correct |
58 |
Correct |
36 ms |
30300 KB |
Output is correct |
59 |
Correct |
48 ms |
31592 KB |
Output is correct |
60 |
Correct |
51 ms |
32864 KB |
Output is correct |
61 |
Correct |
51 ms |
32244 KB |
Output is correct |
62 |
Correct |
163 ms |
55072 KB |
Output is correct |
63 |
Correct |
840 ms |
167120 KB |
Output is correct |
64 |
Correct |
913 ms |
190060 KB |
Output is correct |
65 |
Runtime error |
720 ms |
262144 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |