#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 = 15000000;
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<vector<int>> press(N);
REP(i, N) {
press[i].reserve(lst[i].size());
REP(j, (int)lst[i].size()) press[i].push_back(lst[i][j].first);
}
auto get_key = [&](int i, int w) { return lower_bound(ALL(press[i]), w) - press[i].begin(); };
REP(i, N) {
for (const auto &[w, c] : lst[i]) {
const int id = size_r[i] + get_key(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 = size_r[l] + get_key(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 (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 |
33 ms |
73688 KB |
Output is correct |
2 |
Correct |
32 ms |
73652 KB |
Output is correct |
3 |
Correct |
33 ms |
73616 KB |
Output is correct |
4 |
Correct |
38 ms |
73628 KB |
Output is correct |
5 |
Correct |
32 ms |
73660 KB |
Output is correct |
6 |
Correct |
31 ms |
73676 KB |
Output is correct |
7 |
Correct |
31 ms |
73656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
73620 KB |
Output is correct |
2 |
Correct |
37 ms |
73672 KB |
Output is correct |
3 |
Correct |
31 ms |
73652 KB |
Output is correct |
4 |
Correct |
31 ms |
73684 KB |
Output is correct |
5 |
Correct |
31 ms |
73576 KB |
Output is correct |
6 |
Correct |
33 ms |
73624 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
32 ms |
73688 KB |
Output is correct |
9 |
Correct |
32 ms |
73680 KB |
Output is correct |
10 |
Correct |
34 ms |
73812 KB |
Output is correct |
11 |
Correct |
34 ms |
73824 KB |
Output is correct |
12 |
Correct |
34 ms |
73604 KB |
Output is correct |
13 |
Correct |
35 ms |
73712 KB |
Output is correct |
14 |
Correct |
36 ms |
73792 KB |
Output is correct |
15 |
Correct |
39 ms |
73804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
73604 KB |
Output is correct |
2 |
Correct |
41 ms |
73624 KB |
Output is correct |
3 |
Correct |
37 ms |
73648 KB |
Output is correct |
4 |
Correct |
33 ms |
73676 KB |
Output is correct |
5 |
Correct |
39 ms |
73596 KB |
Output is correct |
6 |
Correct |
34 ms |
73684 KB |
Output is correct |
7 |
Correct |
34 ms |
73704 KB |
Output is correct |
8 |
Correct |
33 ms |
73604 KB |
Output is correct |
9 |
Correct |
40 ms |
73676 KB |
Output is correct |
10 |
Correct |
35 ms |
73728 KB |
Output is correct |
11 |
Correct |
34 ms |
73744 KB |
Output is correct |
12 |
Correct |
32 ms |
73600 KB |
Output is correct |
13 |
Correct |
37 ms |
73692 KB |
Output is correct |
14 |
Correct |
33 ms |
73868 KB |
Output is correct |
15 |
Correct |
33 ms |
73804 KB |
Output is correct |
16 |
Correct |
33 ms |
73704 KB |
Output is correct |
17 |
Correct |
36 ms |
73988 KB |
Output is correct |
18 |
Correct |
34 ms |
73932 KB |
Output is correct |
19 |
Correct |
34 ms |
73996 KB |
Output is correct |
20 |
Correct |
34 ms |
73992 KB |
Output is correct |
21 |
Correct |
35 ms |
73812 KB |
Output is correct |
22 |
Correct |
32 ms |
73948 KB |
Output is correct |
23 |
Correct |
35 ms |
73968 KB |
Output is correct |
24 |
Correct |
35 ms |
74208 KB |
Output is correct |
25 |
Correct |
34 ms |
74060 KB |
Output is correct |
26 |
Correct |
34 ms |
74068 KB |
Output is correct |
27 |
Correct |
33 ms |
74068 KB |
Output is correct |
28 |
Correct |
34 ms |
74368 KB |
Output is correct |
29 |
Correct |
39 ms |
75044 KB |
Output is correct |
30 |
Correct |
35 ms |
74224 KB |
Output is correct |
31 |
Correct |
36 ms |
74624 KB |
Output is correct |
32 |
Correct |
36 ms |
74432 KB |
Output is correct |
33 |
Correct |
45 ms |
76364 KB |
Output is correct |
34 |
Correct |
44 ms |
76352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
73580 KB |
Output is correct |
2 |
Correct |
32 ms |
73676 KB |
Output is correct |
3 |
Correct |
32 ms |
73652 KB |
Output is correct |
4 |
Correct |
31 ms |
73672 KB |
Output is correct |
5 |
Correct |
31 ms |
73676 KB |
Output is correct |
6 |
Correct |
35 ms |
73704 KB |
Output is correct |
7 |
Correct |
33 ms |
73676 KB |
Output is correct |
8 |
Correct |
37 ms |
73664 KB |
Output is correct |
9 |
Correct |
32 ms |
73612 KB |
Output is correct |
10 |
Correct |
32 ms |
73624 KB |
Output is correct |
11 |
Correct |
33 ms |
73856 KB |
Output is correct |
12 |
Correct |
33 ms |
73652 KB |
Output is correct |
13 |
Correct |
33 ms |
73684 KB |
Output is correct |
14 |
Correct |
33 ms |
73772 KB |
Output is correct |
15 |
Correct |
35 ms |
73796 KB |
Output is correct |
16 |
Correct |
32 ms |
73696 KB |
Output is correct |
17 |
Correct |
34 ms |
74060 KB |
Output is correct |
18 |
Correct |
34 ms |
73980 KB |
Output is correct |
19 |
Correct |
34 ms |
73940 KB |
Output is correct |
20 |
Correct |
34 ms |
74056 KB |
Output is correct |
21 |
Correct |
34 ms |
73784 KB |
Output is correct |
22 |
Correct |
33 ms |
73940 KB |
Output is correct |
23 |
Correct |
33 ms |
73976 KB |
Output is correct |
24 |
Correct |
37 ms |
74116 KB |
Output is correct |
25 |
Correct |
35 ms |
74004 KB |
Output is correct |
26 |
Correct |
33 ms |
73988 KB |
Output is correct |
27 |
Correct |
33 ms |
73956 KB |
Output is correct |
28 |
Correct |
34 ms |
74380 KB |
Output is correct |
29 |
Correct |
40 ms |
75028 KB |
Output is correct |
30 |
Correct |
34 ms |
74256 KB |
Output is correct |
31 |
Correct |
35 ms |
74572 KB |
Output is correct |
32 |
Correct |
35 ms |
74320 KB |
Output is correct |
33 |
Correct |
45 ms |
76284 KB |
Output is correct |
34 |
Correct |
46 ms |
76300 KB |
Output is correct |
35 |
Correct |
59 ms |
76456 KB |
Output is correct |
36 |
Correct |
36 ms |
74264 KB |
Output is correct |
37 |
Correct |
64 ms |
77944 KB |
Output is correct |
38 |
Correct |
67 ms |
77976 KB |
Output is correct |
39 |
Correct |
67 ms |
77980 KB |
Output is correct |
40 |
Correct |
68 ms |
78072 KB |
Output is correct |
41 |
Correct |
67 ms |
77980 KB |
Output is correct |
42 |
Correct |
44 ms |
74140 KB |
Output is correct |
43 |
Correct |
47 ms |
74168 KB |
Output is correct |
44 |
Correct |
46 ms |
74100 KB |
Output is correct |
45 |
Correct |
105 ms |
84520 KB |
Output is correct |
46 |
Correct |
110 ms |
84580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
73572 KB |
Output is correct |
2 |
Correct |
31 ms |
73652 KB |
Output is correct |
3 |
Correct |
33 ms |
73560 KB |
Output is correct |
4 |
Correct |
32 ms |
73684 KB |
Output is correct |
5 |
Correct |
33 ms |
73780 KB |
Output is correct |
6 |
Correct |
32 ms |
73688 KB |
Output is correct |
7 |
Correct |
33 ms |
73672 KB |
Output is correct |
8 |
Correct |
34 ms |
73624 KB |
Output is correct |
9 |
Correct |
32 ms |
73708 KB |
Output is correct |
10 |
Correct |
33 ms |
73720 KB |
Output is correct |
11 |
Correct |
33 ms |
73776 KB |
Output is correct |
12 |
Correct |
34 ms |
73632 KB |
Output is correct |
13 |
Correct |
33 ms |
73608 KB |
Output is correct |
14 |
Correct |
34 ms |
73800 KB |
Output is correct |
15 |
Correct |
36 ms |
73872 KB |
Output is correct |
16 |
Correct |
33 ms |
73720 KB |
Output is correct |
17 |
Correct |
35 ms |
74060 KB |
Output is correct |
18 |
Correct |
34 ms |
73952 KB |
Output is correct |
19 |
Correct |
34 ms |
73996 KB |
Output is correct |
20 |
Correct |
33 ms |
74008 KB |
Output is correct |
21 |
Correct |
33 ms |
73796 KB |
Output is correct |
22 |
Correct |
32 ms |
74020 KB |
Output is correct |
23 |
Correct |
35 ms |
73948 KB |
Output is correct |
24 |
Correct |
35 ms |
74156 KB |
Output is correct |
25 |
Correct |
34 ms |
74056 KB |
Output is correct |
26 |
Correct |
42 ms |
73980 KB |
Output is correct |
27 |
Correct |
35 ms |
74056 KB |
Output is correct |
28 |
Correct |
36 ms |
74348 KB |
Output is correct |
29 |
Correct |
38 ms |
75084 KB |
Output is correct |
30 |
Correct |
35 ms |
74256 KB |
Output is correct |
31 |
Correct |
36 ms |
74572 KB |
Output is correct |
32 |
Correct |
35 ms |
74372 KB |
Output is correct |
33 |
Correct |
47 ms |
76320 KB |
Output is correct |
34 |
Correct |
47 ms |
76320 KB |
Output is correct |
35 |
Correct |
58 ms |
76488 KB |
Output is correct |
36 |
Correct |
36 ms |
74140 KB |
Output is correct |
37 |
Correct |
64 ms |
77864 KB |
Output is correct |
38 |
Correct |
69 ms |
77964 KB |
Output is correct |
39 |
Correct |
66 ms |
77996 KB |
Output is correct |
40 |
Correct |
69 ms |
78016 KB |
Output is correct |
41 |
Correct |
68 ms |
78016 KB |
Output is correct |
42 |
Correct |
44 ms |
74120 KB |
Output is correct |
43 |
Correct |
46 ms |
74156 KB |
Output is correct |
44 |
Correct |
43 ms |
74044 KB |
Output is correct |
45 |
Correct |
105 ms |
84584 KB |
Output is correct |
46 |
Correct |
107 ms |
84548 KB |
Output is correct |
47 |
Correct |
184 ms |
91152 KB |
Output is correct |
48 |
Correct |
70 ms |
81824 KB |
Output is correct |
49 |
Correct |
64 ms |
80196 KB |
Output is correct |
50 |
Correct |
61 ms |
80300 KB |
Output is correct |
51 |
Correct |
102 ms |
84628 KB |
Output is correct |
52 |
Correct |
106 ms |
85192 KB |
Output is correct |
53 |
Correct |
65 ms |
80104 KB |
Output is correct |
54 |
Correct |
36 ms |
78112 KB |
Output is correct |
55 |
Correct |
38 ms |
78392 KB |
Output is correct |
56 |
Correct |
52 ms |
79132 KB |
Output is correct |
57 |
Correct |
79 ms |
85732 KB |
Output is correct |
58 |
Correct |
48 ms |
78668 KB |
Output is correct |
59 |
Correct |
56 ms |
79040 KB |
Output is correct |
60 |
Correct |
64 ms |
79632 KB |
Output is correct |
61 |
Correct |
63 ms |
79388 KB |
Output is correct |
62 |
Correct |
128 ms |
88880 KB |
Output is correct |
63 |
Correct |
547 ms |
128488 KB |
Output is correct |
64 |
Correct |
647 ms |
137676 KB |
Output is correct |
65 |
Correct |
892 ms |
168532 KB |
Output is correct |
66 |
Execution timed out |
1066 ms |
222992 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |