#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 = 3000000;
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 |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11956 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11960 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12056 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12372 KB |
Output is correct |
12 |
Correct |
7 ms |
12104 KB |
Output is correct |
13 |
Correct |
7 ms |
12056 KB |
Output is correct |
14 |
Correct |
9 ms |
12460 KB |
Output is correct |
15 |
Correct |
8 ms |
12500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11956 KB |
Output is correct |
3 |
Correct |
6 ms |
12052 KB |
Output is correct |
4 |
Correct |
7 ms |
11952 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12084 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
9 ms |
12316 KB |
Output is correct |
12 |
Correct |
7 ms |
12092 KB |
Output is correct |
13 |
Correct |
7 ms |
12108 KB |
Output is correct |
14 |
Correct |
9 ms |
12500 KB |
Output is correct |
15 |
Correct |
8 ms |
12500 KB |
Output is correct |
16 |
Correct |
8 ms |
12344 KB |
Output is correct |
17 |
Correct |
13 ms |
13012 KB |
Output is correct |
18 |
Correct |
9 ms |
12744 KB |
Output is correct |
19 |
Correct |
9 ms |
12712 KB |
Output is correct |
20 |
Correct |
8 ms |
12728 KB |
Output is correct |
21 |
Correct |
7 ms |
12244 KB |
Output is correct |
22 |
Correct |
8 ms |
12756 KB |
Output is correct |
23 |
Correct |
9 ms |
12756 KB |
Output is correct |
24 |
Correct |
11 ms |
13116 KB |
Output is correct |
25 |
Correct |
10 ms |
12884 KB |
Output is correct |
26 |
Correct |
9 ms |
12884 KB |
Output is correct |
27 |
Correct |
9 ms |
12780 KB |
Output is correct |
28 |
Correct |
12 ms |
13760 KB |
Output is correct |
29 |
Correct |
18 ms |
15980 KB |
Output is correct |
30 |
Correct |
10 ms |
13368 KB |
Output is correct |
31 |
Correct |
14 ms |
14432 KB |
Output is correct |
32 |
Correct |
10 ms |
13652 KB |
Output is correct |
33 |
Correct |
30 ms |
19632 KB |
Output is correct |
34 |
Correct |
31 ms |
19628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12056 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11956 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
11948 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
8 ms |
12368 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
7 ms |
12088 KB |
Output is correct |
14 |
Correct |
8 ms |
12476 KB |
Output is correct |
15 |
Correct |
8 ms |
12500 KB |
Output is correct |
16 |
Correct |
7 ms |
12244 KB |
Output is correct |
17 |
Correct |
12 ms |
12996 KB |
Output is correct |
18 |
Correct |
8 ms |
12716 KB |
Output is correct |
19 |
Correct |
8 ms |
12732 KB |
Output is correct |
20 |
Correct |
8 ms |
12608 KB |
Output is correct |
21 |
Correct |
7 ms |
12244 KB |
Output is correct |
22 |
Correct |
8 ms |
12688 KB |
Output is correct |
23 |
Correct |
8 ms |
12756 KB |
Output is correct |
24 |
Correct |
11 ms |
13124 KB |
Output is correct |
25 |
Correct |
9 ms |
12884 KB |
Output is correct |
26 |
Correct |
10 ms |
12884 KB |
Output is correct |
27 |
Correct |
9 ms |
12756 KB |
Output is correct |
28 |
Correct |
13 ms |
13780 KB |
Output is correct |
29 |
Correct |
19 ms |
15952 KB |
Output is correct |
30 |
Correct |
10 ms |
13364 KB |
Output is correct |
31 |
Correct |
13 ms |
14432 KB |
Output is correct |
32 |
Correct |
10 ms |
13684 KB |
Output is correct |
33 |
Correct |
30 ms |
19704 KB |
Output is correct |
34 |
Correct |
32 ms |
19668 KB |
Output is correct |
35 |
Correct |
44 ms |
19020 KB |
Output is correct |
36 |
Correct |
13 ms |
13292 KB |
Output is correct |
37 |
Correct |
59 ms |
23104 KB |
Output is correct |
38 |
Correct |
63 ms |
23052 KB |
Output is correct |
39 |
Correct |
64 ms |
23124 KB |
Output is correct |
40 |
Correct |
67 ms |
23080 KB |
Output is correct |
41 |
Correct |
68 ms |
22984 KB |
Output is correct |
42 |
Correct |
20 ms |
13100 KB |
Output is correct |
43 |
Correct |
20 ms |
13132 KB |
Output is correct |
44 |
Correct |
20 ms |
12912 KB |
Output is correct |
45 |
Correct |
113 ms |
40124 KB |
Output is correct |
46 |
Correct |
113 ms |
40156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12040 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12020 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11952 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
8 ms |
12372 KB |
Output is correct |
12 |
Correct |
7 ms |
12096 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
8 ms |
12476 KB |
Output is correct |
15 |
Correct |
8 ms |
12500 KB |
Output is correct |
16 |
Correct |
7 ms |
12244 KB |
Output is correct |
17 |
Correct |
11 ms |
12992 KB |
Output is correct |
18 |
Correct |
8 ms |
12756 KB |
Output is correct |
19 |
Correct |
8 ms |
12756 KB |
Output is correct |
20 |
Correct |
10 ms |
12700 KB |
Output is correct |
21 |
Correct |
7 ms |
12220 KB |
Output is correct |
22 |
Correct |
8 ms |
12756 KB |
Output is correct |
23 |
Correct |
9 ms |
12756 KB |
Output is correct |
24 |
Correct |
11 ms |
13140 KB |
Output is correct |
25 |
Correct |
10 ms |
12884 KB |
Output is correct |
26 |
Correct |
9 ms |
12864 KB |
Output is correct |
27 |
Correct |
9 ms |
12756 KB |
Output is correct |
28 |
Correct |
13 ms |
13780 KB |
Output is correct |
29 |
Correct |
19 ms |
15964 KB |
Output is correct |
30 |
Correct |
12 ms |
13364 KB |
Output is correct |
31 |
Correct |
13 ms |
14340 KB |
Output is correct |
32 |
Correct |
10 ms |
13652 KB |
Output is correct |
33 |
Correct |
30 ms |
19688 KB |
Output is correct |
34 |
Correct |
30 ms |
19648 KB |
Output is correct |
35 |
Correct |
46 ms |
19000 KB |
Output is correct |
36 |
Correct |
12 ms |
13332 KB |
Output is correct |
37 |
Correct |
62 ms |
23096 KB |
Output is correct |
38 |
Correct |
64 ms |
23068 KB |
Output is correct |
39 |
Correct |
63 ms |
22996 KB |
Output is correct |
40 |
Correct |
64 ms |
22964 KB |
Output is correct |
41 |
Correct |
64 ms |
23076 KB |
Output is correct |
42 |
Correct |
20 ms |
13156 KB |
Output is correct |
43 |
Correct |
19 ms |
13168 KB |
Output is correct |
44 |
Correct |
19 ms |
12912 KB |
Output is correct |
45 |
Correct |
111 ms |
40148 KB |
Output is correct |
46 |
Correct |
108 ms |
40252 KB |
Output is correct |
47 |
Correct |
274 ms |
59228 KB |
Output is correct |
48 |
Correct |
86 ms |
30100 KB |
Output is correct |
49 |
Correct |
63 ms |
25792 KB |
Output is correct |
50 |
Correct |
59 ms |
26208 KB |
Output is correct |
51 |
Correct |
129 ms |
36324 KB |
Output is correct |
52 |
Correct |
139 ms |
38180 KB |
Output is correct |
53 |
Correct |
54 ms |
24804 KB |
Output is correct |
54 |
Correct |
19 ms |
20820 KB |
Output is correct |
55 |
Correct |
22 ms |
21844 KB |
Output is correct |
56 |
Correct |
35 ms |
22008 KB |
Output is correct |
57 |
Correct |
109 ms |
43296 KB |
Output is correct |
58 |
Correct |
32 ms |
22596 KB |
Output is correct |
59 |
Correct |
44 ms |
24012 KB |
Output is correct |
60 |
Correct |
49 ms |
25324 KB |
Output is correct |
61 |
Correct |
49 ms |
24632 KB |
Output is correct |
62 |
Correct |
172 ms |
47724 KB |
Output is correct |
63 |
Correct |
864 ms |
159516 KB |
Output is correct |
64 |
Correct |
921 ms |
182524 KB |
Output is correct |
65 |
Runtime error |
900 ms |
262144 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |