#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxN = 3e4 + 20;
const int maxC = 1e7 + 20;
const int inf = 1e9 + 20;
int B[maxN];
int P[maxN];
vector<int> adj0[maxN];
bool can_nxt[maxC];
bool can_prv[maxC];
int dist[maxC];
int pos[maxC];
bool flag[maxN];
int id[maxN];
void just_do_it() {
fill_n(pos, maxC, -1);
int N, M;
cin >> N >> M;
const int lim = sqrt(N);
int cnt = N;
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
if (P[i] > lim) {
int cur = B[i] % P[i];
int nxt = cur + P[i];
for (; cur < N; cur += P[i], nxt += P[i]) {
if (nxt < N) {
can_nxt[cnt] = true;
can_prv[cnt + 1] = true;
}
if (cur == B[i]) {
adj0[B[i]].emplace_back(cnt);
}
pos[cnt] = cur;
cnt++;
if (cnt == maxC) {
exit(0);
}
}
}
}
for (int d = 1; d <= lim; d++) {
for (int i = 0; i < N; i++) {
flag[i] = false;
}
bool found = false;
for (int i = 0; i < M; i++) {
if (P[i] == d) {
found = true;
break;
}
}
if (!found) {
continue;
}
for (int i = 0; i < d; i++) {
int cur = i;
int nxt = cur + d;
for (; cur < N; cur += d, nxt += d) {
if (nxt < N) {
can_nxt[cnt] = true;
can_prv[cnt + 1] = true;
}
id[cur] = cnt;
pos[cnt] = cur;
cnt++;
if (cnt == maxC) {
exit(0);
}
}
}
for (int i = 0; i < M; i++) {
if (P[i] == d && !flag[B[i]]) {
flag[B[i]] = true;
adj0[B[i]].emplace_back(id[B[i]]);
}
}
}
for (int i = 0; i < cnt; i++) {
dist[i] = inf;
}
dist[B[0]] = 0;
deque<int> q = {B[0]};
while (!q.empty()) {
int u = q.front();
if (u == B[1]) {
cout << dist[u];
return;
}
q.pop_front();
if (u < N) {
for (auto v: adj0[u]) {
if (dist[u] < dist[v]) {
dist[v] = dist[u];
q.push_front(v);
}
}
}
else {
if (pos[u] != -1) {
int v = pos[u];
if (dist[u] < dist[v]) {
dist[v] = dist[u];
q.push_front(v);
}
}
if (can_nxt[u]) {
int v = u + 1;
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
q.push_back(v);
}
}
if (can_prv[u]) {
int v = u - 1;
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
q.push_back(v);
}
}
}
}
cout << -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
40148 KB |
Output is correct |
2 |
Correct |
16 ms |
40180 KB |
Output is correct |
3 |
Correct |
18 ms |
40172 KB |
Output is correct |
4 |
Correct |
17 ms |
40144 KB |
Output is correct |
5 |
Correct |
19 ms |
40132 KB |
Output is correct |
6 |
Correct |
16 ms |
40148 KB |
Output is correct |
7 |
Correct |
17 ms |
40148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
40180 KB |
Output is correct |
2 |
Correct |
17 ms |
40148 KB |
Output is correct |
3 |
Correct |
16 ms |
40148 KB |
Output is correct |
4 |
Correct |
17 ms |
40148 KB |
Output is correct |
5 |
Correct |
18 ms |
40092 KB |
Output is correct |
6 |
Correct |
18 ms |
40120 KB |
Output is correct |
7 |
Correct |
17 ms |
40148 KB |
Output is correct |
8 |
Correct |
20 ms |
40140 KB |
Output is correct |
9 |
Correct |
20 ms |
40144 KB |
Output is correct |
10 |
Correct |
17 ms |
40148 KB |
Output is correct |
11 |
Correct |
20 ms |
40208 KB |
Output is correct |
12 |
Correct |
17 ms |
40128 KB |
Output is correct |
13 |
Correct |
17 ms |
40148 KB |
Output is correct |
14 |
Correct |
17 ms |
40276 KB |
Output is correct |
15 |
Correct |
18 ms |
40248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
40276 KB |
Output is correct |
2 |
Correct |
17 ms |
40148 KB |
Output is correct |
3 |
Correct |
17 ms |
40148 KB |
Output is correct |
4 |
Correct |
18 ms |
40136 KB |
Output is correct |
5 |
Correct |
17 ms |
40148 KB |
Output is correct |
6 |
Correct |
17 ms |
40148 KB |
Output is correct |
7 |
Correct |
17 ms |
40148 KB |
Output is correct |
8 |
Correct |
16 ms |
40140 KB |
Output is correct |
9 |
Correct |
19 ms |
40148 KB |
Output is correct |
10 |
Correct |
17 ms |
40144 KB |
Output is correct |
11 |
Correct |
17 ms |
40244 KB |
Output is correct |
12 |
Correct |
17 ms |
40164 KB |
Output is correct |
13 |
Correct |
17 ms |
40148 KB |
Output is correct |
14 |
Correct |
20 ms |
40212 KB |
Output is correct |
15 |
Correct |
17 ms |
40276 KB |
Output is correct |
16 |
Correct |
17 ms |
40176 KB |
Output is correct |
17 |
Correct |
17 ms |
40424 KB |
Output is correct |
18 |
Correct |
17 ms |
40388 KB |
Output is correct |
19 |
Correct |
18 ms |
40372 KB |
Output is correct |
20 |
Correct |
19 ms |
40296 KB |
Output is correct |
21 |
Correct |
19 ms |
40136 KB |
Output is correct |
22 |
Correct |
20 ms |
40276 KB |
Output is correct |
23 |
Correct |
18 ms |
40404 KB |
Output is correct |
24 |
Correct |
18 ms |
40532 KB |
Output is correct |
25 |
Correct |
18 ms |
40276 KB |
Output is correct |
26 |
Correct |
17 ms |
40460 KB |
Output is correct |
27 |
Correct |
17 ms |
40404 KB |
Output is correct |
28 |
Correct |
19 ms |
40644 KB |
Output is correct |
29 |
Correct |
18 ms |
40516 KB |
Output is correct |
30 |
Correct |
17 ms |
40236 KB |
Output is correct |
31 |
Correct |
19 ms |
40404 KB |
Output is correct |
32 |
Correct |
18 ms |
40364 KB |
Output is correct |
33 |
Correct |
19 ms |
40916 KB |
Output is correct |
34 |
Correct |
21 ms |
41008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
40148 KB |
Output is correct |
2 |
Correct |
17 ms |
40148 KB |
Output is correct |
3 |
Correct |
17 ms |
40152 KB |
Output is correct |
4 |
Correct |
17 ms |
40084 KB |
Output is correct |
5 |
Correct |
19 ms |
40232 KB |
Output is correct |
6 |
Correct |
17 ms |
40148 KB |
Output is correct |
7 |
Correct |
20 ms |
40176 KB |
Output is correct |
8 |
Correct |
18 ms |
40120 KB |
Output is correct |
9 |
Correct |
20 ms |
40108 KB |
Output is correct |
10 |
Correct |
17 ms |
40148 KB |
Output is correct |
11 |
Correct |
17 ms |
40220 KB |
Output is correct |
12 |
Correct |
20 ms |
40228 KB |
Output is correct |
13 |
Correct |
20 ms |
40148 KB |
Output is correct |
14 |
Correct |
17 ms |
40232 KB |
Output is correct |
15 |
Correct |
17 ms |
40276 KB |
Output is correct |
16 |
Correct |
19 ms |
40260 KB |
Output is correct |
17 |
Correct |
16 ms |
40404 KB |
Output is correct |
18 |
Correct |
18 ms |
40484 KB |
Output is correct |
19 |
Correct |
18 ms |
40276 KB |
Output is correct |
20 |
Correct |
18 ms |
40276 KB |
Output is correct |
21 |
Correct |
18 ms |
40152 KB |
Output is correct |
22 |
Correct |
17 ms |
40276 KB |
Output is correct |
23 |
Correct |
19 ms |
40424 KB |
Output is correct |
24 |
Correct |
18 ms |
40524 KB |
Output is correct |
25 |
Correct |
18 ms |
40220 KB |
Output is correct |
26 |
Correct |
20 ms |
40556 KB |
Output is correct |
27 |
Correct |
17 ms |
40364 KB |
Output is correct |
28 |
Correct |
18 ms |
40704 KB |
Output is correct |
29 |
Correct |
18 ms |
40552 KB |
Output is correct |
30 |
Correct |
20 ms |
40276 KB |
Output is correct |
31 |
Correct |
17 ms |
40388 KB |
Output is correct |
32 |
Correct |
20 ms |
40276 KB |
Output is correct |
33 |
Correct |
19 ms |
40916 KB |
Output is correct |
34 |
Correct |
18 ms |
40916 KB |
Output is correct |
35 |
Correct |
23 ms |
41336 KB |
Output is correct |
36 |
Correct |
18 ms |
40496 KB |
Output is correct |
37 |
Correct |
23 ms |
41876 KB |
Output is correct |
38 |
Correct |
25 ms |
41904 KB |
Output is correct |
39 |
Correct |
24 ms |
41868 KB |
Output is correct |
40 |
Correct |
24 ms |
41804 KB |
Output is correct |
41 |
Correct |
24 ms |
41812 KB |
Output is correct |
42 |
Correct |
21 ms |
40624 KB |
Output is correct |
43 |
Correct |
21 ms |
40608 KB |
Output is correct |
44 |
Correct |
21 ms |
40472 KB |
Output is correct |
45 |
Correct |
30 ms |
43600 KB |
Output is correct |
46 |
Correct |
30 ms |
43592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
40084 KB |
Output is correct |
2 |
Correct |
19 ms |
40064 KB |
Output is correct |
3 |
Correct |
18 ms |
40148 KB |
Output is correct |
4 |
Correct |
17 ms |
40140 KB |
Output is correct |
5 |
Correct |
17 ms |
40208 KB |
Output is correct |
6 |
Correct |
18 ms |
40136 KB |
Output is correct |
7 |
Correct |
17 ms |
40200 KB |
Output is correct |
8 |
Correct |
18 ms |
40168 KB |
Output is correct |
9 |
Correct |
19 ms |
40148 KB |
Output is correct |
10 |
Correct |
19 ms |
40232 KB |
Output is correct |
11 |
Correct |
18 ms |
40276 KB |
Output is correct |
12 |
Correct |
18 ms |
40196 KB |
Output is correct |
13 |
Correct |
17 ms |
40148 KB |
Output is correct |
14 |
Correct |
18 ms |
40232 KB |
Output is correct |
15 |
Correct |
18 ms |
40264 KB |
Output is correct |
16 |
Correct |
17 ms |
40148 KB |
Output is correct |
17 |
Correct |
19 ms |
40352 KB |
Output is correct |
18 |
Correct |
17 ms |
40452 KB |
Output is correct |
19 |
Correct |
20 ms |
40304 KB |
Output is correct |
20 |
Correct |
23 ms |
40248 KB |
Output is correct |
21 |
Correct |
18 ms |
40196 KB |
Output is correct |
22 |
Correct |
19 ms |
40404 KB |
Output is correct |
23 |
Correct |
18 ms |
40404 KB |
Output is correct |
24 |
Correct |
21 ms |
40612 KB |
Output is correct |
25 |
Correct |
18 ms |
40276 KB |
Output is correct |
26 |
Correct |
18 ms |
40524 KB |
Output is correct |
27 |
Correct |
17 ms |
40400 KB |
Output is correct |
28 |
Correct |
18 ms |
40724 KB |
Output is correct |
29 |
Correct |
18 ms |
40512 KB |
Output is correct |
30 |
Correct |
19 ms |
40276 KB |
Output is correct |
31 |
Correct |
17 ms |
40436 KB |
Output is correct |
32 |
Correct |
17 ms |
40356 KB |
Output is correct |
33 |
Correct |
20 ms |
40916 KB |
Output is correct |
34 |
Correct |
18 ms |
40952 KB |
Output is correct |
35 |
Correct |
22 ms |
41372 KB |
Output is correct |
36 |
Correct |
18 ms |
40532 KB |
Output is correct |
37 |
Correct |
22 ms |
41860 KB |
Output is correct |
38 |
Correct |
23 ms |
41804 KB |
Output is correct |
39 |
Correct |
24 ms |
41840 KB |
Output is correct |
40 |
Correct |
24 ms |
41892 KB |
Output is correct |
41 |
Correct |
24 ms |
41812 KB |
Output is correct |
42 |
Correct |
20 ms |
40628 KB |
Output is correct |
43 |
Correct |
22 ms |
40540 KB |
Output is correct |
44 |
Correct |
21 ms |
40532 KB |
Output is correct |
45 |
Correct |
32 ms |
43616 KB |
Output is correct |
46 |
Correct |
29 ms |
43604 KB |
Output is correct |
47 |
Correct |
34 ms |
51168 KB |
Output is correct |
48 |
Correct |
34 ms |
54376 KB |
Output is correct |
49 |
Correct |
31 ms |
51844 KB |
Output is correct |
50 |
Correct |
29 ms |
51916 KB |
Output is correct |
51 |
Correct |
43 ms |
61404 KB |
Output is correct |
52 |
Correct |
46 ms |
63824 KB |
Output is correct |
53 |
Correct |
27 ms |
41884 KB |
Output is correct |
54 |
Correct |
18 ms |
40532 KB |
Output is correct |
55 |
Correct |
18 ms |
40976 KB |
Output is correct |
56 |
Correct |
28 ms |
41812 KB |
Output is correct |
57 |
Correct |
20 ms |
43196 KB |
Output is correct |
58 |
Correct |
23 ms |
42964 KB |
Output is correct |
59 |
Correct |
27 ms |
46388 KB |
Output is correct |
60 |
Correct |
34 ms |
56332 KB |
Output is correct |
61 |
Correct |
28 ms |
48068 KB |
Output is correct |
62 |
Correct |
44 ms |
57148 KB |
Output is correct |
63 |
Correct |
103 ms |
54376 KB |
Output is correct |
64 |
Correct |
125 ms |
57420 KB |
Output is correct |
65 |
Correct |
186 ms |
70776 KB |
Output is correct |
66 |
Correct |
265 ms |
84244 KB |
Output is correct |
67 |
Correct |
255 ms |
84432 KB |
Output is correct |