#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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 = 10000000;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> A(M);
REP(i, M) {
cin >> A[i].first >> A[i].second;
chmin(A[i].second, 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<int, 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;
}
}
static array<int, 30010> size_r;
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;
static array<vector<int>, 30010> chd;
static array<unordered_map<int, int>, 30010> id_m;
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;
static array<bool, V> used;
fill(ALL(dist), inf);
fill(ALL(used), false);
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] + A[T].first) break;
if (used[f]) continue;
used[f] = true;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
51540 KB |
Output is correct |
2 |
Correct |
24 ms |
51508 KB |
Output is correct |
3 |
Correct |
21 ms |
51640 KB |
Output is correct |
4 |
Correct |
20 ms |
51564 KB |
Output is correct |
5 |
Correct |
21 ms |
51576 KB |
Output is correct |
6 |
Correct |
20 ms |
51512 KB |
Output is correct |
7 |
Correct |
20 ms |
51540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
51540 KB |
Output is correct |
2 |
Correct |
21 ms |
51668 KB |
Output is correct |
3 |
Correct |
21 ms |
51528 KB |
Output is correct |
4 |
Correct |
21 ms |
51540 KB |
Output is correct |
5 |
Correct |
21 ms |
51560 KB |
Output is correct |
6 |
Correct |
23 ms |
51540 KB |
Output is correct |
7 |
Correct |
22 ms |
51660 KB |
Output is correct |
8 |
Correct |
21 ms |
51540 KB |
Output is correct |
9 |
Correct |
21 ms |
51572 KB |
Output is correct |
10 |
Correct |
20 ms |
51692 KB |
Output is correct |
11 |
Correct |
22 ms |
51884 KB |
Output is correct |
12 |
Correct |
21 ms |
51596 KB |
Output is correct |
13 |
Correct |
21 ms |
51596 KB |
Output is correct |
14 |
Correct |
24 ms |
52020 KB |
Output is correct |
15 |
Correct |
23 ms |
52040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
51516 KB |
Output is correct |
2 |
Correct |
21 ms |
51516 KB |
Output is correct |
3 |
Correct |
20 ms |
51520 KB |
Output is correct |
4 |
Correct |
23 ms |
51628 KB |
Output is correct |
5 |
Correct |
20 ms |
51540 KB |
Output is correct |
6 |
Correct |
21 ms |
51540 KB |
Output is correct |
7 |
Correct |
21 ms |
51516 KB |
Output is correct |
8 |
Correct |
21 ms |
51540 KB |
Output is correct |
9 |
Correct |
21 ms |
51512 KB |
Output is correct |
10 |
Correct |
22 ms |
51772 KB |
Output is correct |
11 |
Correct |
22 ms |
51864 KB |
Output is correct |
12 |
Correct |
21 ms |
51644 KB |
Output is correct |
13 |
Correct |
21 ms |
51500 KB |
Output is correct |
14 |
Correct |
22 ms |
52028 KB |
Output is correct |
15 |
Correct |
24 ms |
52024 KB |
Output is correct |
16 |
Correct |
22 ms |
51848 KB |
Output is correct |
17 |
Correct |
26 ms |
52648 KB |
Output is correct |
18 |
Correct |
22 ms |
52196 KB |
Output is correct |
19 |
Correct |
21 ms |
52044 KB |
Output is correct |
20 |
Correct |
21 ms |
52084 KB |
Output is correct |
21 |
Correct |
21 ms |
51796 KB |
Output is correct |
22 |
Correct |
23 ms |
52132 KB |
Output is correct |
23 |
Correct |
23 ms |
52156 KB |
Output is correct |
24 |
Correct |
27 ms |
52584 KB |
Output is correct |
25 |
Correct |
24 ms |
52296 KB |
Output is correct |
26 |
Correct |
22 ms |
52308 KB |
Output is correct |
27 |
Correct |
23 ms |
52172 KB |
Output is correct |
28 |
Correct |
26 ms |
53296 KB |
Output is correct |
29 |
Correct |
32 ms |
55624 KB |
Output is correct |
30 |
Correct |
25 ms |
52792 KB |
Output is correct |
31 |
Correct |
27 ms |
53920 KB |
Output is correct |
32 |
Correct |
24 ms |
53176 KB |
Output is correct |
33 |
Correct |
44 ms |
59488 KB |
Output is correct |
34 |
Correct |
43 ms |
59592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
51572 KB |
Output is correct |
2 |
Correct |
20 ms |
51504 KB |
Output is correct |
3 |
Correct |
21 ms |
51488 KB |
Output is correct |
4 |
Correct |
21 ms |
51556 KB |
Output is correct |
5 |
Correct |
22 ms |
51532 KB |
Output is correct |
6 |
Correct |
21 ms |
51544 KB |
Output is correct |
7 |
Correct |
21 ms |
51592 KB |
Output is correct |
8 |
Correct |
22 ms |
51584 KB |
Output is correct |
9 |
Correct |
20 ms |
51512 KB |
Output is correct |
10 |
Correct |
23 ms |
51712 KB |
Output is correct |
11 |
Correct |
22 ms |
51832 KB |
Output is correct |
12 |
Correct |
21 ms |
51668 KB |
Output is correct |
13 |
Correct |
21 ms |
51552 KB |
Output is correct |
14 |
Correct |
22 ms |
51980 KB |
Output is correct |
15 |
Correct |
22 ms |
52032 KB |
Output is correct |
16 |
Correct |
22 ms |
51784 KB |
Output is correct |
17 |
Correct |
24 ms |
52540 KB |
Output is correct |
18 |
Correct |
23 ms |
52252 KB |
Output is correct |
19 |
Correct |
22 ms |
52052 KB |
Output is correct |
20 |
Correct |
20 ms |
52044 KB |
Output is correct |
21 |
Correct |
20 ms |
51748 KB |
Output is correct |
22 |
Correct |
22 ms |
52148 KB |
Output is correct |
23 |
Correct |
23 ms |
52160 KB |
Output is correct |
24 |
Correct |
24 ms |
52556 KB |
Output is correct |
25 |
Correct |
23 ms |
52384 KB |
Output is correct |
26 |
Correct |
22 ms |
52292 KB |
Output is correct |
27 |
Correct |
25 ms |
52268 KB |
Output is correct |
28 |
Correct |
26 ms |
53336 KB |
Output is correct |
29 |
Correct |
33 ms |
55656 KB |
Output is correct |
30 |
Correct |
24 ms |
52916 KB |
Output is correct |
31 |
Correct |
29 ms |
53948 KB |
Output is correct |
32 |
Correct |
24 ms |
53076 KB |
Output is correct |
33 |
Correct |
44 ms |
59600 KB |
Output is correct |
34 |
Correct |
43 ms |
59596 KB |
Output is correct |
35 |
Correct |
58 ms |
59224 KB |
Output is correct |
36 |
Correct |
28 ms |
52904 KB |
Output is correct |
37 |
Correct |
68 ms |
63576 KB |
Output is correct |
38 |
Correct |
72 ms |
63516 KB |
Output is correct |
39 |
Correct |
73 ms |
63400 KB |
Output is correct |
40 |
Correct |
73 ms |
63436 KB |
Output is correct |
41 |
Correct |
74 ms |
63476 KB |
Output is correct |
42 |
Correct |
34 ms |
52668 KB |
Output is correct |
43 |
Correct |
33 ms |
52596 KB |
Output is correct |
44 |
Correct |
33 ms |
52428 KB |
Output is correct |
45 |
Correct |
117 ms |
81892 KB |
Output is correct |
46 |
Correct |
112 ms |
81884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
51592 KB |
Output is correct |
2 |
Correct |
20 ms |
51540 KB |
Output is correct |
3 |
Correct |
20 ms |
51580 KB |
Output is correct |
4 |
Correct |
21 ms |
51520 KB |
Output is correct |
5 |
Correct |
20 ms |
51520 KB |
Output is correct |
6 |
Correct |
21 ms |
51576 KB |
Output is correct |
7 |
Correct |
22 ms |
51548 KB |
Output is correct |
8 |
Correct |
22 ms |
51508 KB |
Output is correct |
9 |
Correct |
21 ms |
51600 KB |
Output is correct |
10 |
Correct |
21 ms |
51656 KB |
Output is correct |
11 |
Correct |
23 ms |
51864 KB |
Output is correct |
12 |
Correct |
21 ms |
51600 KB |
Output is correct |
13 |
Correct |
21 ms |
51540 KB |
Output is correct |
14 |
Correct |
23 ms |
52064 KB |
Output is correct |
15 |
Correct |
25 ms |
52040 KB |
Output is correct |
16 |
Correct |
21 ms |
51796 KB |
Output is correct |
17 |
Correct |
24 ms |
52560 KB |
Output is correct |
18 |
Correct |
22 ms |
52224 KB |
Output is correct |
19 |
Correct |
23 ms |
52044 KB |
Output is correct |
20 |
Correct |
22 ms |
52052 KB |
Output is correct |
21 |
Correct |
21 ms |
51752 KB |
Output is correct |
22 |
Correct |
23 ms |
52100 KB |
Output is correct |
23 |
Correct |
23 ms |
52200 KB |
Output is correct |
24 |
Correct |
24 ms |
52660 KB |
Output is correct |
25 |
Correct |
23 ms |
52412 KB |
Output is correct |
26 |
Correct |
23 ms |
52248 KB |
Output is correct |
27 |
Correct |
26 ms |
52172 KB |
Output is correct |
28 |
Correct |
26 ms |
53204 KB |
Output is correct |
29 |
Correct |
36 ms |
55652 KB |
Output is correct |
30 |
Correct |
25 ms |
52868 KB |
Output is correct |
31 |
Correct |
27 ms |
53940 KB |
Output is correct |
32 |
Correct |
26 ms |
53104 KB |
Output is correct |
33 |
Correct |
48 ms |
59660 KB |
Output is correct |
34 |
Correct |
43 ms |
59596 KB |
Output is correct |
35 |
Correct |
56 ms |
59212 KB |
Output is correct |
36 |
Correct |
26 ms |
52852 KB |
Output is correct |
37 |
Correct |
69 ms |
63504 KB |
Output is correct |
38 |
Correct |
77 ms |
63612 KB |
Output is correct |
39 |
Correct |
80 ms |
63432 KB |
Output is correct |
40 |
Correct |
73 ms |
63408 KB |
Output is correct |
41 |
Correct |
76 ms |
63564 KB |
Output is correct |
42 |
Correct |
33 ms |
52684 KB |
Output is correct |
43 |
Correct |
35 ms |
52684 KB |
Output is correct |
44 |
Correct |
36 ms |
52412 KB |
Output is correct |
45 |
Correct |
118 ms |
81868 KB |
Output is correct |
46 |
Correct |
132 ms |
81904 KB |
Output is correct |
47 |
Correct |
221 ms |
101524 KB |
Output is correct |
48 |
Correct |
96 ms |
69212 KB |
Output is correct |
49 |
Correct |
84 ms |
64348 KB |
Output is correct |
50 |
Correct |
68 ms |
64360 KB |
Output is correct |
51 |
Correct |
131 ms |
75456 KB |
Output is correct |
52 |
Correct |
128 ms |
77204 KB |
Output is correct |
53 |
Correct |
63 ms |
62752 KB |
Output is correct |
54 |
Correct |
29 ms |
57844 KB |
Output is correct |
55 |
Correct |
31 ms |
58992 KB |
Output is correct |
56 |
Correct |
43 ms |
59308 KB |
Output is correct |
57 |
Correct |
108 ms |
82280 KB |
Output is correct |
58 |
Correct |
44 ms |
59980 KB |
Output is correct |
59 |
Correct |
52 ms |
61716 KB |
Output is correct |
60 |
Correct |
67 ms |
63308 KB |
Output is correct |
61 |
Correct |
67 ms |
62512 KB |
Output is correct |
62 |
Correct |
189 ms |
87916 KB |
Output is correct |
63 |
Correct |
854 ms |
211336 KB |
Output is correct |
64 |
Correct |
956 ms |
235152 KB |
Output is correct |
65 |
Runtime error |
572 ms |
262144 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |