#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 = 2000000;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8020 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8120 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8096 KB |
Output is correct |
9 |
Correct |
5 ms |
8052 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8404 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8132 KB |
Output is correct |
14 |
Correct |
6 ms |
8532 KB |
Output is correct |
15 |
Correct |
6 ms |
8500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8044 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8404 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8532 KB |
Output is correct |
15 |
Correct |
6 ms |
8532 KB |
Output is correct |
16 |
Correct |
5 ms |
8532 KB |
Output is correct |
17 |
Correct |
9 ms |
9152 KB |
Output is correct |
18 |
Correct |
7 ms |
8840 KB |
Output is correct |
19 |
Correct |
8 ms |
8848 KB |
Output is correct |
20 |
Correct |
6 ms |
8776 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
22 |
Correct |
6 ms |
8788 KB |
Output is correct |
23 |
Correct |
6 ms |
8788 KB |
Output is correct |
24 |
Correct |
8 ms |
9256 KB |
Output is correct |
25 |
Correct |
8 ms |
9008 KB |
Output is correct |
26 |
Correct |
7 ms |
8916 KB |
Output is correct |
27 |
Correct |
7 ms |
8916 KB |
Output is correct |
28 |
Correct |
10 ms |
9812 KB |
Output is correct |
29 |
Correct |
16 ms |
12084 KB |
Output is correct |
30 |
Correct |
7 ms |
9428 KB |
Output is correct |
31 |
Correct |
11 ms |
10532 KB |
Output is correct |
32 |
Correct |
9 ms |
9684 KB |
Output is correct |
33 |
Correct |
29 ms |
15792 KB |
Output is correct |
34 |
Correct |
29 ms |
15700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8020 KB |
Output is correct |
3 |
Correct |
5 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8120 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
6 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8404 KB |
Output is correct |
12 |
Correct |
5 ms |
8112 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
6 ms |
8532 KB |
Output is correct |
15 |
Correct |
6 ms |
8532 KB |
Output is correct |
16 |
Correct |
5 ms |
8404 KB |
Output is correct |
17 |
Correct |
8 ms |
9172 KB |
Output is correct |
18 |
Correct |
7 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
8788 KB |
Output is correct |
20 |
Correct |
7 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
22 |
Correct |
5 ms |
8756 KB |
Output is correct |
23 |
Correct |
6 ms |
8788 KB |
Output is correct |
24 |
Correct |
9 ms |
9172 KB |
Output is correct |
25 |
Correct |
7 ms |
9044 KB |
Output is correct |
26 |
Correct |
6 ms |
8892 KB |
Output is correct |
27 |
Correct |
7 ms |
8916 KB |
Output is correct |
28 |
Correct |
10 ms |
9828 KB |
Output is correct |
29 |
Correct |
17 ms |
12088 KB |
Output is correct |
30 |
Correct |
8 ms |
9380 KB |
Output is correct |
31 |
Correct |
11 ms |
10452 KB |
Output is correct |
32 |
Correct |
8 ms |
9684 KB |
Output is correct |
33 |
Correct |
29 ms |
15704 KB |
Output is correct |
34 |
Correct |
32 ms |
15772 KB |
Output is correct |
35 |
Correct |
42 ms |
14960 KB |
Output is correct |
36 |
Correct |
11 ms |
9428 KB |
Output is correct |
37 |
Correct |
57 ms |
19196 KB |
Output is correct |
38 |
Correct |
66 ms |
18972 KB |
Output is correct |
39 |
Correct |
63 ms |
19036 KB |
Output is correct |
40 |
Correct |
61 ms |
18992 KB |
Output is correct |
41 |
Correct |
61 ms |
19024 KB |
Output is correct |
42 |
Correct |
18 ms |
9188 KB |
Output is correct |
43 |
Correct |
18 ms |
9124 KB |
Output is correct |
44 |
Correct |
18 ms |
8992 KB |
Output is correct |
45 |
Correct |
100 ms |
36172 KB |
Output is correct |
46 |
Correct |
103 ms |
36236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8120 KB |
Output is correct |
3 |
Correct |
4 ms |
8020 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8404 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8124 KB |
Output is correct |
14 |
Correct |
6 ms |
8532 KB |
Output is correct |
15 |
Correct |
6 ms |
8532 KB |
Output is correct |
16 |
Correct |
5 ms |
8404 KB |
Output is correct |
17 |
Correct |
8 ms |
9172 KB |
Output is correct |
18 |
Correct |
7 ms |
8816 KB |
Output is correct |
19 |
Correct |
6 ms |
8788 KB |
Output is correct |
20 |
Correct |
6 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
22 |
Correct |
6 ms |
8784 KB |
Output is correct |
23 |
Correct |
6 ms |
8788 KB |
Output is correct |
24 |
Correct |
8 ms |
9156 KB |
Output is correct |
25 |
Correct |
7 ms |
8972 KB |
Output is correct |
26 |
Correct |
7 ms |
8916 KB |
Output is correct |
27 |
Correct |
7 ms |
8916 KB |
Output is correct |
28 |
Correct |
11 ms |
9884 KB |
Output is correct |
29 |
Correct |
16 ms |
12140 KB |
Output is correct |
30 |
Correct |
8 ms |
9464 KB |
Output is correct |
31 |
Correct |
12 ms |
10416 KB |
Output is correct |
32 |
Correct |
10 ms |
9760 KB |
Output is correct |
33 |
Correct |
30 ms |
15788 KB |
Output is correct |
34 |
Correct |
30 ms |
15768 KB |
Output is correct |
35 |
Correct |
45 ms |
14988 KB |
Output is correct |
36 |
Correct |
10 ms |
9428 KB |
Output is correct |
37 |
Correct |
56 ms |
19140 KB |
Output is correct |
38 |
Correct |
64 ms |
18996 KB |
Output is correct |
39 |
Correct |
64 ms |
18976 KB |
Output is correct |
40 |
Correct |
63 ms |
19020 KB |
Output is correct |
41 |
Correct |
64 ms |
18984 KB |
Output is correct |
42 |
Correct |
18 ms |
9156 KB |
Output is correct |
43 |
Correct |
19 ms |
9124 KB |
Output is correct |
44 |
Correct |
17 ms |
8924 KB |
Output is correct |
45 |
Correct |
101 ms |
36124 KB |
Output is correct |
46 |
Correct |
102 ms |
36204 KB |
Output is correct |
47 |
Correct |
261 ms |
55004 KB |
Output is correct |
48 |
Correct |
78 ms |
26064 KB |
Output is correct |
49 |
Correct |
55 ms |
21900 KB |
Output is correct |
50 |
Correct |
50 ms |
22220 KB |
Output is correct |
51 |
Correct |
127 ms |
32356 KB |
Output is correct |
52 |
Correct |
136 ms |
34052 KB |
Output is correct |
53 |
Correct |
53 ms |
20900 KB |
Output is correct |
54 |
Correct |
17 ms |
16824 KB |
Output is correct |
55 |
Correct |
19 ms |
17852 KB |
Output is correct |
56 |
Correct |
33 ms |
17952 KB |
Output is correct |
57 |
Correct |
103 ms |
39408 KB |
Output is correct |
58 |
Correct |
30 ms |
18580 KB |
Output is correct |
59 |
Correct |
49 ms |
19996 KB |
Output is correct |
60 |
Correct |
46 ms |
21292 KB |
Output is correct |
61 |
Correct |
44 ms |
20656 KB |
Output is correct |
62 |
Correct |
161 ms |
43576 KB |
Output is correct |
63 |
Runtime error |
570 ms |
262144 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |