#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> B(M), P(M);
vector<vector<int> > vmod(N), vx(N);
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
vx[B[i]].push_back(P[i]);
if (P[i] < N) vmod[P[i]].push_back(B[i] % P[i]);
}
for (int i = 0; i < N; i++) {
sort(vx[i].begin(), vx[i].end());
}
vector<vector<int> > g(N);
for (int i = 1; i < N; i++) {
sort(vmod[i].begin(), vmod[i].end());
vmod[i].erase(unique(vmod[i].begin(), vmod[i].end()), vmod[i].end());
for (int j : vmod[i]) {
int pre = -1;
for (int k = j; k < N; k += i) {
if (j != k) g[pre].push_back(g.size() * 2 + 1);
g.push_back(j == k ? vector<int>({ k * 2 }) : vector<int>({ k * 2, pre * 2 + 1 }));
pre = g.size() - 1;
if (binary_search(vx[k].begin(), vx[k].end(), i)) {
g[k].push_back(pre * 2);
}
}
}
}
vector<int> dist(g.size(), -1); dist[B[0]] = 0;
deque<int> que; que.push_back(B[0]);
while (!que.empty()) {
int u = que.front(); que.pop_front();
for (int e : g[u]) {
if (dist[e >> 1] == -1) {
dist[e >> 1] = dist[u] + (e & 1);
if (e & 1) que.push_back(e >> 1);
else que.push_front(e >> 1);
}
}
}
cout << dist[B[1]] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
644 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
740 KB |
Output is correct |
3 |
Correct |
2 ms |
740 KB |
Output is correct |
4 |
Correct |
2 ms |
740 KB |
Output is correct |
5 |
Correct |
2 ms |
740 KB |
Output is correct |
6 |
Correct |
2 ms |
740 KB |
Output is correct |
7 |
Correct |
2 ms |
740 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
2 ms |
740 KB |
Output is correct |
10 |
Correct |
2 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
892 KB |
Output is correct |
12 |
Correct |
2 ms |
892 KB |
Output is correct |
13 |
Correct |
2 ms |
892 KB |
Output is correct |
14 |
Correct |
4 ms |
1020 KB |
Output is correct |
15 |
Correct |
3 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
1020 KB |
Output is correct |
3 |
Correct |
2 ms |
1020 KB |
Output is correct |
4 |
Correct |
2 ms |
1020 KB |
Output is correct |
5 |
Correct |
2 ms |
1020 KB |
Output is correct |
6 |
Correct |
2 ms |
1020 KB |
Output is correct |
7 |
Correct |
2 ms |
1020 KB |
Output is correct |
8 |
Correct |
2 ms |
1020 KB |
Output is correct |
9 |
Correct |
2 ms |
1020 KB |
Output is correct |
10 |
Correct |
3 ms |
1020 KB |
Output is correct |
11 |
Correct |
3 ms |
1020 KB |
Output is correct |
12 |
Correct |
2 ms |
1020 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
3 ms |
1020 KB |
Output is correct |
15 |
Correct |
4 ms |
1020 KB |
Output is correct |
16 |
Correct |
3 ms |
1020 KB |
Output is correct |
17 |
Correct |
6 ms |
1712 KB |
Output is correct |
18 |
Correct |
5 ms |
1712 KB |
Output is correct |
19 |
Correct |
4 ms |
1712 KB |
Output is correct |
20 |
Correct |
3 ms |
1712 KB |
Output is correct |
21 |
Correct |
2 ms |
1712 KB |
Output is correct |
22 |
Correct |
4 ms |
1712 KB |
Output is correct |
23 |
Correct |
4 ms |
1712 KB |
Output is correct |
24 |
Correct |
6 ms |
1712 KB |
Output is correct |
25 |
Correct |
5 ms |
1712 KB |
Output is correct |
26 |
Correct |
4 ms |
1712 KB |
Output is correct |
27 |
Correct |
4 ms |
1712 KB |
Output is correct |
28 |
Correct |
7 ms |
2248 KB |
Output is correct |
29 |
Correct |
15 ms |
4308 KB |
Output is correct |
30 |
Correct |
7 ms |
4308 KB |
Output is correct |
31 |
Correct |
8 ms |
4308 KB |
Output is correct |
32 |
Correct |
6 ms |
4308 KB |
Output is correct |
33 |
Correct |
32 ms |
7960 KB |
Output is correct |
34 |
Correct |
26 ms |
7960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7960 KB |
Output is correct |
2 |
Correct |
2 ms |
7960 KB |
Output is correct |
3 |
Correct |
0 ms |
7960 KB |
Output is correct |
4 |
Correct |
2 ms |
7960 KB |
Output is correct |
5 |
Correct |
2 ms |
7960 KB |
Output is correct |
6 |
Correct |
2 ms |
7960 KB |
Output is correct |
7 |
Correct |
2 ms |
7960 KB |
Output is correct |
8 |
Correct |
2 ms |
7960 KB |
Output is correct |
9 |
Correct |
2 ms |
7960 KB |
Output is correct |
10 |
Correct |
2 ms |
7960 KB |
Output is correct |
11 |
Correct |
3 ms |
7960 KB |
Output is correct |
12 |
Correct |
3 ms |
7960 KB |
Output is correct |
13 |
Correct |
2 ms |
7960 KB |
Output is correct |
14 |
Correct |
3 ms |
7960 KB |
Output is correct |
15 |
Correct |
3 ms |
7960 KB |
Output is correct |
16 |
Correct |
3 ms |
7960 KB |
Output is correct |
17 |
Correct |
6 ms |
7960 KB |
Output is correct |
18 |
Correct |
4 ms |
7960 KB |
Output is correct |
19 |
Correct |
3 ms |
7960 KB |
Output is correct |
20 |
Correct |
3 ms |
7960 KB |
Output is correct |
21 |
Correct |
3 ms |
7960 KB |
Output is correct |
22 |
Correct |
3 ms |
7960 KB |
Output is correct |
23 |
Correct |
4 ms |
7960 KB |
Output is correct |
24 |
Correct |
6 ms |
7960 KB |
Output is correct |
25 |
Correct |
4 ms |
7960 KB |
Output is correct |
26 |
Correct |
4 ms |
7960 KB |
Output is correct |
27 |
Correct |
4 ms |
7960 KB |
Output is correct |
28 |
Correct |
7 ms |
7960 KB |
Output is correct |
29 |
Correct |
13 ms |
7960 KB |
Output is correct |
30 |
Correct |
5 ms |
7960 KB |
Output is correct |
31 |
Correct |
8 ms |
7960 KB |
Output is correct |
32 |
Correct |
6 ms |
7960 KB |
Output is correct |
33 |
Correct |
25 ms |
7960 KB |
Output is correct |
34 |
Correct |
25 ms |
7960 KB |
Output is correct |
35 |
Correct |
33 ms |
8460 KB |
Output is correct |
36 |
Correct |
7 ms |
8460 KB |
Output is correct |
37 |
Correct |
54 ms |
11528 KB |
Output is correct |
38 |
Correct |
54 ms |
11528 KB |
Output is correct |
39 |
Correct |
53 ms |
11528 KB |
Output is correct |
40 |
Correct |
56 ms |
11528 KB |
Output is correct |
41 |
Correct |
59 ms |
11528 KB |
Output is correct |
42 |
Correct |
9 ms |
11528 KB |
Output is correct |
43 |
Correct |
9 ms |
11528 KB |
Output is correct |
44 |
Correct |
8 ms |
11528 KB |
Output is correct |
45 |
Correct |
137 ms |
29876 KB |
Output is correct |
46 |
Correct |
123 ms |
29876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
29876 KB |
Output is correct |
2 |
Correct |
2 ms |
29876 KB |
Output is correct |
3 |
Correct |
2 ms |
29876 KB |
Output is correct |
4 |
Correct |
2 ms |
29876 KB |
Output is correct |
5 |
Correct |
2 ms |
29876 KB |
Output is correct |
6 |
Correct |
2 ms |
29876 KB |
Output is correct |
7 |
Correct |
2 ms |
29876 KB |
Output is correct |
8 |
Correct |
2 ms |
29876 KB |
Output is correct |
9 |
Correct |
2 ms |
29876 KB |
Output is correct |
10 |
Correct |
2 ms |
29876 KB |
Output is correct |
11 |
Correct |
3 ms |
29876 KB |
Output is correct |
12 |
Correct |
2 ms |
29876 KB |
Output is correct |
13 |
Correct |
2 ms |
29876 KB |
Output is correct |
14 |
Correct |
3 ms |
29876 KB |
Output is correct |
15 |
Correct |
3 ms |
29876 KB |
Output is correct |
16 |
Correct |
3 ms |
29876 KB |
Output is correct |
17 |
Correct |
5 ms |
29876 KB |
Output is correct |
18 |
Correct |
4 ms |
29876 KB |
Output is correct |
19 |
Correct |
3 ms |
29876 KB |
Output is correct |
20 |
Correct |
3 ms |
29876 KB |
Output is correct |
21 |
Correct |
2 ms |
29876 KB |
Output is correct |
22 |
Correct |
3 ms |
29876 KB |
Output is correct |
23 |
Correct |
4 ms |
29876 KB |
Output is correct |
24 |
Correct |
6 ms |
29876 KB |
Output is correct |
25 |
Correct |
5 ms |
29876 KB |
Output is correct |
26 |
Correct |
4 ms |
29876 KB |
Output is correct |
27 |
Correct |
4 ms |
29876 KB |
Output is correct |
28 |
Correct |
7 ms |
29876 KB |
Output is correct |
29 |
Correct |
14 ms |
29876 KB |
Output is correct |
30 |
Correct |
5 ms |
29876 KB |
Output is correct |
31 |
Correct |
8 ms |
29876 KB |
Output is correct |
32 |
Correct |
6 ms |
29876 KB |
Output is correct |
33 |
Correct |
27 ms |
29876 KB |
Output is correct |
34 |
Correct |
26 ms |
29876 KB |
Output is correct |
35 |
Correct |
34 ms |
29876 KB |
Output is correct |
36 |
Correct |
10 ms |
29876 KB |
Output is correct |
37 |
Correct |
55 ms |
29876 KB |
Output is correct |
38 |
Correct |
57 ms |
29876 KB |
Output is correct |
39 |
Correct |
56 ms |
29876 KB |
Output is correct |
40 |
Correct |
59 ms |
29876 KB |
Output is correct |
41 |
Correct |
59 ms |
29876 KB |
Output is correct |
42 |
Correct |
9 ms |
29876 KB |
Output is correct |
43 |
Correct |
9 ms |
29876 KB |
Output is correct |
44 |
Correct |
16 ms |
29876 KB |
Output is correct |
45 |
Correct |
130 ms |
29876 KB |
Output is correct |
46 |
Correct |
124 ms |
29876 KB |
Output is correct |
47 |
Correct |
282 ms |
45992 KB |
Output is correct |
48 |
Correct |
67 ms |
45992 KB |
Output is correct |
49 |
Correct |
46 ms |
45992 KB |
Output is correct |
50 |
Correct |
43 ms |
45992 KB |
Output is correct |
51 |
Correct |
122 ms |
45992 KB |
Output is correct |
52 |
Correct |
134 ms |
45992 KB |
Output is correct |
53 |
Correct |
44 ms |
45992 KB |
Output is correct |
54 |
Correct |
8 ms |
45992 KB |
Output is correct |
55 |
Correct |
12 ms |
45992 KB |
Output is correct |
56 |
Correct |
22 ms |
45992 KB |
Output is correct |
57 |
Correct |
109 ms |
45992 KB |
Output is correct |
58 |
Correct |
21 ms |
45992 KB |
Output is correct |
59 |
Correct |
46 ms |
45992 KB |
Output is correct |
60 |
Correct |
34 ms |
45992 KB |
Output is correct |
61 |
Correct |
31 ms |
45992 KB |
Output is correct |
62 |
Correct |
128 ms |
45992 KB |
Output is correct |
63 |
Correct |
890 ms |
152084 KB |
Output is correct |
64 |
Correct |
964 ms |
168164 KB |
Output is correct |
65 |
Execution timed out |
1098 ms |
262144 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |