#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, press;
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) {
int id = size_r[i];
for (const auto &[w, c] : lst[i]) {
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;
}
++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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50592 KB |
Output is correct |
2 |
Correct |
20 ms |
50620 KB |
Output is correct |
3 |
Correct |
19 ms |
50580 KB |
Output is correct |
4 |
Correct |
20 ms |
50632 KB |
Output is correct |
5 |
Correct |
21 ms |
50656 KB |
Output is correct |
6 |
Correct |
20 ms |
50604 KB |
Output is correct |
7 |
Correct |
21 ms |
50540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
50544 KB |
Output is correct |
2 |
Correct |
20 ms |
50620 KB |
Output is correct |
3 |
Correct |
18 ms |
50576 KB |
Output is correct |
4 |
Correct |
23 ms |
50644 KB |
Output is correct |
5 |
Correct |
20 ms |
50644 KB |
Output is correct |
6 |
Correct |
21 ms |
50636 KB |
Output is correct |
7 |
Correct |
20 ms |
50576 KB |
Output is correct |
8 |
Correct |
21 ms |
50600 KB |
Output is correct |
9 |
Correct |
20 ms |
50644 KB |
Output is correct |
10 |
Correct |
20 ms |
50664 KB |
Output is correct |
11 |
Correct |
21 ms |
50772 KB |
Output is correct |
12 |
Correct |
21 ms |
50688 KB |
Output is correct |
13 |
Correct |
21 ms |
50644 KB |
Output is correct |
14 |
Correct |
22 ms |
50816 KB |
Output is correct |
15 |
Correct |
21 ms |
50900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50644 KB |
Output is correct |
2 |
Correct |
20 ms |
50548 KB |
Output is correct |
3 |
Correct |
20 ms |
50588 KB |
Output is correct |
4 |
Correct |
22 ms |
50632 KB |
Output is correct |
5 |
Correct |
20 ms |
50644 KB |
Output is correct |
6 |
Correct |
20 ms |
50644 KB |
Output is correct |
7 |
Correct |
20 ms |
50644 KB |
Output is correct |
8 |
Correct |
20 ms |
50644 KB |
Output is correct |
9 |
Correct |
20 ms |
50644 KB |
Output is correct |
10 |
Correct |
20 ms |
50656 KB |
Output is correct |
11 |
Correct |
21 ms |
50824 KB |
Output is correct |
12 |
Correct |
19 ms |
50592 KB |
Output is correct |
13 |
Correct |
21 ms |
50596 KB |
Output is correct |
14 |
Correct |
21 ms |
50820 KB |
Output is correct |
15 |
Correct |
22 ms |
50764 KB |
Output is correct |
16 |
Correct |
21 ms |
50772 KB |
Output is correct |
17 |
Correct |
22 ms |
51044 KB |
Output is correct |
18 |
Correct |
21 ms |
50860 KB |
Output is correct |
19 |
Correct |
21 ms |
50900 KB |
Output is correct |
20 |
Correct |
21 ms |
50820 KB |
Output is correct |
21 |
Correct |
21 ms |
50644 KB |
Output is correct |
22 |
Correct |
21 ms |
50916 KB |
Output is correct |
23 |
Correct |
21 ms |
50888 KB |
Output is correct |
24 |
Correct |
22 ms |
51168 KB |
Output is correct |
25 |
Correct |
22 ms |
51028 KB |
Output is correct |
26 |
Correct |
21 ms |
50912 KB |
Output is correct |
27 |
Correct |
22 ms |
50872 KB |
Output is correct |
28 |
Correct |
24 ms |
51448 KB |
Output is correct |
29 |
Correct |
25 ms |
52180 KB |
Output is correct |
30 |
Correct |
25 ms |
51296 KB |
Output is correct |
31 |
Correct |
22 ms |
51724 KB |
Output is correct |
32 |
Correct |
22 ms |
51412 KB |
Output is correct |
33 |
Correct |
30 ms |
53716 KB |
Output is correct |
34 |
Correct |
28 ms |
53728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
50644 KB |
Output is correct |
2 |
Correct |
19 ms |
50536 KB |
Output is correct |
3 |
Correct |
19 ms |
50620 KB |
Output is correct |
4 |
Correct |
20 ms |
50644 KB |
Output is correct |
5 |
Correct |
20 ms |
50644 KB |
Output is correct |
6 |
Correct |
20 ms |
50600 KB |
Output is correct |
7 |
Correct |
20 ms |
50580 KB |
Output is correct |
8 |
Correct |
20 ms |
50644 KB |
Output is correct |
9 |
Correct |
22 ms |
50656 KB |
Output is correct |
10 |
Correct |
20 ms |
50608 KB |
Output is correct |
11 |
Correct |
21 ms |
50848 KB |
Output is correct |
12 |
Correct |
22 ms |
50756 KB |
Output is correct |
13 |
Correct |
20 ms |
50644 KB |
Output is correct |
14 |
Correct |
21 ms |
50788 KB |
Output is correct |
15 |
Correct |
21 ms |
50824 KB |
Output is correct |
16 |
Correct |
20 ms |
50736 KB |
Output is correct |
17 |
Correct |
21 ms |
51104 KB |
Output is correct |
18 |
Correct |
21 ms |
50876 KB |
Output is correct |
19 |
Correct |
22 ms |
50900 KB |
Output is correct |
20 |
Correct |
23 ms |
50924 KB |
Output is correct |
21 |
Correct |
20 ms |
50644 KB |
Output is correct |
22 |
Correct |
24 ms |
50904 KB |
Output is correct |
23 |
Correct |
21 ms |
50840 KB |
Output is correct |
24 |
Correct |
23 ms |
51068 KB |
Output is correct |
25 |
Correct |
23 ms |
51020 KB |
Output is correct |
26 |
Correct |
22 ms |
50892 KB |
Output is correct |
27 |
Correct |
24 ms |
50900 KB |
Output is correct |
28 |
Correct |
24 ms |
51412 KB |
Output is correct |
29 |
Correct |
24 ms |
52264 KB |
Output is correct |
30 |
Correct |
22 ms |
51220 KB |
Output is correct |
31 |
Correct |
23 ms |
51668 KB |
Output is correct |
32 |
Correct |
21 ms |
51412 KB |
Output is correct |
33 |
Correct |
32 ms |
53684 KB |
Output is correct |
34 |
Correct |
29 ms |
53716 KB |
Output is correct |
35 |
Correct |
41 ms |
54332 KB |
Output is correct |
36 |
Correct |
25 ms |
51300 KB |
Output is correct |
37 |
Correct |
44 ms |
55912 KB |
Output is correct |
38 |
Correct |
51 ms |
56288 KB |
Output is correct |
39 |
Correct |
46 ms |
56236 KB |
Output is correct |
40 |
Correct |
46 ms |
56156 KB |
Output is correct |
41 |
Correct |
48 ms |
56268 KB |
Output is correct |
42 |
Correct |
31 ms |
51404 KB |
Output is correct |
43 |
Correct |
34 ms |
51348 KB |
Output is correct |
44 |
Correct |
34 ms |
51332 KB |
Output is correct |
45 |
Correct |
82 ms |
63984 KB |
Output is correct |
46 |
Correct |
77 ms |
63988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
50648 KB |
Output is correct |
2 |
Correct |
20 ms |
50636 KB |
Output is correct |
3 |
Correct |
21 ms |
50644 KB |
Output is correct |
4 |
Correct |
21 ms |
50552 KB |
Output is correct |
5 |
Correct |
21 ms |
50656 KB |
Output is correct |
6 |
Correct |
19 ms |
50644 KB |
Output is correct |
7 |
Correct |
20 ms |
50616 KB |
Output is correct |
8 |
Correct |
20 ms |
50644 KB |
Output is correct |
9 |
Correct |
21 ms |
50644 KB |
Output is correct |
10 |
Correct |
20 ms |
50648 KB |
Output is correct |
11 |
Correct |
21 ms |
50772 KB |
Output is correct |
12 |
Correct |
21 ms |
50688 KB |
Output is correct |
13 |
Correct |
20 ms |
50644 KB |
Output is correct |
14 |
Correct |
22 ms |
50872 KB |
Output is correct |
15 |
Correct |
21 ms |
50888 KB |
Output is correct |
16 |
Correct |
21 ms |
50676 KB |
Output is correct |
17 |
Correct |
22 ms |
51044 KB |
Output is correct |
18 |
Correct |
21 ms |
50960 KB |
Output is correct |
19 |
Correct |
21 ms |
50860 KB |
Output is correct |
20 |
Correct |
22 ms |
50900 KB |
Output is correct |
21 |
Correct |
20 ms |
50636 KB |
Output is correct |
22 |
Correct |
22 ms |
50900 KB |
Output is correct |
23 |
Correct |
21 ms |
50944 KB |
Output is correct |
24 |
Correct |
24 ms |
51080 KB |
Output is correct |
25 |
Correct |
22 ms |
50952 KB |
Output is correct |
26 |
Correct |
23 ms |
50968 KB |
Output is correct |
27 |
Correct |
22 ms |
50944 KB |
Output is correct |
28 |
Correct |
23 ms |
51412 KB |
Output is correct |
29 |
Correct |
26 ms |
52192 KB |
Output is correct |
30 |
Correct |
21 ms |
51256 KB |
Output is correct |
31 |
Correct |
22 ms |
51680 KB |
Output is correct |
32 |
Correct |
22 ms |
51412 KB |
Output is correct |
33 |
Correct |
33 ms |
53748 KB |
Output is correct |
34 |
Correct |
31 ms |
53700 KB |
Output is correct |
35 |
Correct |
41 ms |
54352 KB |
Output is correct |
36 |
Correct |
24 ms |
51276 KB |
Output is correct |
37 |
Correct |
42 ms |
55928 KB |
Output is correct |
38 |
Correct |
48 ms |
56248 KB |
Output is correct |
39 |
Correct |
46 ms |
56140 KB |
Output is correct |
40 |
Correct |
48 ms |
56204 KB |
Output is correct |
41 |
Correct |
49 ms |
56228 KB |
Output is correct |
42 |
Correct |
32 ms |
51348 KB |
Output is correct |
43 |
Correct |
32 ms |
51380 KB |
Output is correct |
44 |
Correct |
30 ms |
51412 KB |
Output is correct |
45 |
Correct |
80 ms |
63968 KB |
Output is correct |
46 |
Correct |
77 ms |
64064 KB |
Output is correct |
47 |
Correct |
95 ms |
71500 KB |
Output is correct |
48 |
Correct |
57 ms |
59340 KB |
Output is correct |
49 |
Correct |
50 ms |
56996 KB |
Output is correct |
50 |
Correct |
46 ms |
56892 KB |
Output is correct |
51 |
Correct |
76 ms |
62240 KB |
Output is correct |
52 |
Correct |
78 ms |
63120 KB |
Output is correct |
53 |
Correct |
49 ms |
56656 KB |
Output is correct |
54 |
Correct |
24 ms |
53616 KB |
Output is correct |
55 |
Correct |
25 ms |
54040 KB |
Output is correct |
56 |
Correct |
40 ms |
55080 KB |
Output is correct |
57 |
Correct |
49 ms |
63048 KB |
Output is correct |
58 |
Correct |
34 ms |
54612 KB |
Output is correct |
59 |
Correct |
42 ms |
55504 KB |
Output is correct |
60 |
Correct |
44 ms |
56096 KB |
Output is correct |
61 |
Correct |
45 ms |
55840 KB |
Output is correct |
62 |
Correct |
110 ms |
67648 KB |
Output is correct |
63 |
Correct |
475 ms |
119312 KB |
Output is correct |
64 |
Correct |
555 ms |
128540 KB |
Output is correct |
65 |
Correct |
820 ms |
171244 KB |
Output is correct |
66 |
Execution timed out |
1109 ms |
228196 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |