#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 |
34 ms |
73672 KB |
Output is correct |
2 |
Correct |
32 ms |
73668 KB |
Output is correct |
3 |
Correct |
35 ms |
73676 KB |
Output is correct |
4 |
Correct |
32 ms |
73696 KB |
Output is correct |
5 |
Correct |
34 ms |
73676 KB |
Output is correct |
6 |
Correct |
32 ms |
73696 KB |
Output is correct |
7 |
Correct |
33 ms |
73676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
73644 KB |
Output is correct |
2 |
Correct |
35 ms |
73608 KB |
Output is correct |
3 |
Correct |
35 ms |
73572 KB |
Output is correct |
4 |
Correct |
34 ms |
73644 KB |
Output is correct |
5 |
Correct |
35 ms |
73608 KB |
Output is correct |
6 |
Correct |
34 ms |
73676 KB |
Output is correct |
7 |
Correct |
38 ms |
73612 KB |
Output is correct |
8 |
Correct |
35 ms |
73640 KB |
Output is correct |
9 |
Correct |
34 ms |
73600 KB |
Output is correct |
10 |
Correct |
33 ms |
73676 KB |
Output is correct |
11 |
Correct |
39 ms |
73772 KB |
Output is correct |
12 |
Correct |
34 ms |
73684 KB |
Output is correct |
13 |
Correct |
41 ms |
73676 KB |
Output is correct |
14 |
Correct |
36 ms |
73860 KB |
Output is correct |
15 |
Correct |
35 ms |
73832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
73580 KB |
Output is correct |
2 |
Correct |
32 ms |
73576 KB |
Output is correct |
3 |
Correct |
33 ms |
73692 KB |
Output is correct |
4 |
Correct |
45 ms |
73652 KB |
Output is correct |
5 |
Correct |
37 ms |
73644 KB |
Output is correct |
6 |
Correct |
32 ms |
73560 KB |
Output is correct |
7 |
Correct |
34 ms |
73632 KB |
Output is correct |
8 |
Correct |
34 ms |
73700 KB |
Output is correct |
9 |
Correct |
34 ms |
73628 KB |
Output is correct |
10 |
Correct |
34 ms |
73668 KB |
Output is correct |
11 |
Correct |
37 ms |
73732 KB |
Output is correct |
12 |
Correct |
45 ms |
73632 KB |
Output is correct |
13 |
Correct |
35 ms |
73696 KB |
Output is correct |
14 |
Correct |
35 ms |
73804 KB |
Output is correct |
15 |
Correct |
36 ms |
73784 KB |
Output is correct |
16 |
Correct |
35 ms |
73684 KB |
Output is correct |
17 |
Correct |
46 ms |
74076 KB |
Output is correct |
18 |
Correct |
36 ms |
74000 KB |
Output is correct |
19 |
Correct |
35 ms |
73996 KB |
Output is correct |
20 |
Correct |
36 ms |
73968 KB |
Output is correct |
21 |
Correct |
36 ms |
73740 KB |
Output is correct |
22 |
Correct |
35 ms |
73936 KB |
Output is correct |
23 |
Correct |
38 ms |
73932 KB |
Output is correct |
24 |
Correct |
38 ms |
74164 KB |
Output is correct |
25 |
Correct |
37 ms |
74088 KB |
Output is correct |
26 |
Correct |
45 ms |
74060 KB |
Output is correct |
27 |
Correct |
35 ms |
74060 KB |
Output is correct |
28 |
Correct |
39 ms |
74480 KB |
Output is correct |
29 |
Correct |
40 ms |
75084 KB |
Output is correct |
30 |
Correct |
36 ms |
74284 KB |
Output is correct |
31 |
Correct |
37 ms |
74572 KB |
Output is correct |
32 |
Correct |
38 ms |
74372 KB |
Output is correct |
33 |
Correct |
49 ms |
76284 KB |
Output is correct |
34 |
Correct |
50 ms |
76372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
73628 KB |
Output is correct |
2 |
Correct |
34 ms |
73672 KB |
Output is correct |
3 |
Correct |
34 ms |
73648 KB |
Output is correct |
4 |
Correct |
34 ms |
73676 KB |
Output is correct |
5 |
Correct |
35 ms |
73640 KB |
Output is correct |
6 |
Correct |
33 ms |
73676 KB |
Output is correct |
7 |
Correct |
33 ms |
73596 KB |
Output is correct |
8 |
Correct |
35 ms |
73636 KB |
Output is correct |
9 |
Correct |
35 ms |
73672 KB |
Output is correct |
10 |
Correct |
35 ms |
73692 KB |
Output is correct |
11 |
Correct |
38 ms |
73804 KB |
Output is correct |
12 |
Correct |
34 ms |
73684 KB |
Output is correct |
13 |
Correct |
35 ms |
73788 KB |
Output is correct |
14 |
Correct |
35 ms |
73776 KB |
Output is correct |
15 |
Correct |
34 ms |
73772 KB |
Output is correct |
16 |
Correct |
35 ms |
73788 KB |
Output is correct |
17 |
Correct |
37 ms |
74044 KB |
Output is correct |
18 |
Correct |
36 ms |
74060 KB |
Output is correct |
19 |
Correct |
41 ms |
73972 KB |
Output is correct |
20 |
Correct |
35 ms |
74016 KB |
Output is correct |
21 |
Correct |
35 ms |
73776 KB |
Output is correct |
22 |
Correct |
43 ms |
74024 KB |
Output is correct |
23 |
Correct |
43 ms |
74008 KB |
Output is correct |
24 |
Correct |
41 ms |
74184 KB |
Output is correct |
25 |
Correct |
36 ms |
74144 KB |
Output is correct |
26 |
Correct |
35 ms |
74008 KB |
Output is correct |
27 |
Correct |
37 ms |
74044 KB |
Output is correct |
28 |
Correct |
39 ms |
74444 KB |
Output is correct |
29 |
Correct |
39 ms |
75116 KB |
Output is correct |
30 |
Correct |
39 ms |
74244 KB |
Output is correct |
31 |
Correct |
40 ms |
74572 KB |
Output is correct |
32 |
Correct |
37 ms |
74420 KB |
Output is correct |
33 |
Correct |
47 ms |
76364 KB |
Output is correct |
34 |
Correct |
53 ms |
76436 KB |
Output is correct |
35 |
Correct |
61 ms |
76748 KB |
Output is correct |
36 |
Correct |
52 ms |
74200 KB |
Output is correct |
37 |
Correct |
76 ms |
78100 KB |
Output is correct |
38 |
Correct |
77 ms |
78244 KB |
Output is correct |
39 |
Correct |
70 ms |
78300 KB |
Output is correct |
40 |
Correct |
79 ms |
78296 KB |
Output is correct |
41 |
Correct |
74 ms |
78232 KB |
Output is correct |
42 |
Correct |
46 ms |
74316 KB |
Output is correct |
43 |
Correct |
48 ms |
74336 KB |
Output is correct |
44 |
Correct |
51 ms |
74304 KB |
Output is correct |
45 |
Correct |
112 ms |
84672 KB |
Output is correct |
46 |
Correct |
118 ms |
84684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
73656 KB |
Output is correct |
2 |
Correct |
36 ms |
73648 KB |
Output is correct |
3 |
Correct |
41 ms |
73676 KB |
Output is correct |
4 |
Correct |
36 ms |
73676 KB |
Output is correct |
5 |
Correct |
36 ms |
73716 KB |
Output is correct |
6 |
Correct |
33 ms |
73632 KB |
Output is correct |
7 |
Correct |
38 ms |
73652 KB |
Output is correct |
8 |
Correct |
35 ms |
73568 KB |
Output is correct |
9 |
Correct |
36 ms |
73632 KB |
Output is correct |
10 |
Correct |
38 ms |
73644 KB |
Output is correct |
11 |
Correct |
47 ms |
73824 KB |
Output is correct |
12 |
Correct |
37 ms |
73660 KB |
Output is correct |
13 |
Correct |
33 ms |
73672 KB |
Output is correct |
14 |
Correct |
36 ms |
73804 KB |
Output is correct |
15 |
Correct |
35 ms |
73816 KB |
Output is correct |
16 |
Correct |
38 ms |
73700 KB |
Output is correct |
17 |
Correct |
47 ms |
74088 KB |
Output is correct |
18 |
Correct |
36 ms |
73968 KB |
Output is correct |
19 |
Correct |
35 ms |
73988 KB |
Output is correct |
20 |
Correct |
34 ms |
73992 KB |
Output is correct |
21 |
Correct |
36 ms |
73744 KB |
Output is correct |
22 |
Correct |
35 ms |
73956 KB |
Output is correct |
23 |
Correct |
36 ms |
73932 KB |
Output is correct |
24 |
Correct |
38 ms |
74212 KB |
Output is correct |
25 |
Correct |
37 ms |
74064 KB |
Output is correct |
26 |
Correct |
35 ms |
74068 KB |
Output is correct |
27 |
Correct |
34 ms |
74056 KB |
Output is correct |
28 |
Correct |
37 ms |
74444 KB |
Output is correct |
29 |
Correct |
43 ms |
75060 KB |
Output is correct |
30 |
Correct |
45 ms |
74284 KB |
Output is correct |
31 |
Correct |
45 ms |
74572 KB |
Output is correct |
32 |
Correct |
37 ms |
74404 KB |
Output is correct |
33 |
Correct |
48 ms |
76296 KB |
Output is correct |
34 |
Correct |
50 ms |
76372 KB |
Output is correct |
35 |
Correct |
67 ms |
76636 KB |
Output is correct |
36 |
Correct |
39 ms |
74224 KB |
Output is correct |
37 |
Correct |
73 ms |
78024 KB |
Output is correct |
38 |
Correct |
73 ms |
78360 KB |
Output is correct |
39 |
Correct |
82 ms |
78248 KB |
Output is correct |
40 |
Correct |
78 ms |
78244 KB |
Output is correct |
41 |
Correct |
73 ms |
78228 KB |
Output is correct |
42 |
Correct |
46 ms |
74316 KB |
Output is correct |
43 |
Correct |
46 ms |
74316 KB |
Output is correct |
44 |
Correct |
47 ms |
74304 KB |
Output is correct |
45 |
Correct |
112 ms |
84816 KB |
Output is correct |
46 |
Correct |
123 ms |
84724 KB |
Output is correct |
47 |
Correct |
197 ms |
91216 KB |
Output is correct |
48 |
Correct |
75 ms |
82112 KB |
Output is correct |
49 |
Correct |
72 ms |
80352 KB |
Output is correct |
50 |
Correct |
66 ms |
80536 KB |
Output is correct |
51 |
Correct |
106 ms |
84940 KB |
Output is correct |
52 |
Correct |
114 ms |
85576 KB |
Output is correct |
53 |
Correct |
68 ms |
80520 KB |
Output is correct |
54 |
Correct |
41 ms |
78068 KB |
Output is correct |
55 |
Correct |
43 ms |
78340 KB |
Output is correct |
56 |
Correct |
58 ms |
79384 KB |
Output is correct |
57 |
Correct |
83 ms |
85708 KB |
Output is correct |
58 |
Correct |
51 ms |
78832 KB |
Output is correct |
59 |
Correct |
62 ms |
79260 KB |
Output is correct |
60 |
Correct |
61 ms |
79832 KB |
Output is correct |
61 |
Correct |
66 ms |
79616 KB |
Output is correct |
62 |
Correct |
142 ms |
89164 KB |
Output is correct |
63 |
Correct |
564 ms |
128740 KB |
Output is correct |
64 |
Correct |
687 ms |
137940 KB |
Output is correct |
65 |
Correct |
988 ms |
168688 KB |
Output is correct |
66 |
Execution timed out |
1101 ms |
223028 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |