#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
int to, cost;
edge() : to(-1), cost(-1) {};
edge(int to_, int cost_) : to(to_), cost(cost_) {};
};
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<edge> > 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(edge(g.size(), 1));
g.push_back(j == k ? vector<edge>({ edge(k, 0) }) : vector<edge>({ edge(k, 0), edge(pre, 1) }));
pre = g.size() - 1;
if (binary_search(vx[k].begin(), vx[k].end(), i)) {
g[k].push_back(edge(pre, 0));
}
}
}
}
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 (edge e : g[u]) {
if (dist[e.to] == -1) {
dist[e.to] = dist[u] + e.cost;
if (e.cost == 0) que.push_front(e.to);
else que.push_back(e.to);
}
}
}
cout << dist[B[1]] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
412 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
544 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
2 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
636 KB |
Output is correct |
10 |
Correct |
2 ms |
764 KB |
Output is correct |
11 |
Correct |
3 ms |
904 KB |
Output is correct |
12 |
Correct |
2 ms |
904 KB |
Output is correct |
13 |
Correct |
2 ms |
904 KB |
Output is correct |
14 |
Correct |
4 ms |
1020 KB |
Output is correct |
15 |
Correct |
4 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 |
2 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 |
3 ms |
1020 KB |
Output is correct |
16 |
Correct |
3 ms |
1020 KB |
Output is correct |
17 |
Correct |
6 ms |
1796 KB |
Output is correct |
18 |
Correct |
3 ms |
1796 KB |
Output is correct |
19 |
Correct |
3 ms |
1796 KB |
Output is correct |
20 |
Correct |
3 ms |
1796 KB |
Output is correct |
21 |
Correct |
2 ms |
1796 KB |
Output is correct |
22 |
Correct |
3 ms |
1796 KB |
Output is correct |
23 |
Correct |
4 ms |
1796 KB |
Output is correct |
24 |
Correct |
5 ms |
1796 KB |
Output is correct |
25 |
Correct |
4 ms |
1796 KB |
Output is correct |
26 |
Correct |
4 ms |
1796 KB |
Output is correct |
27 |
Correct |
4 ms |
1796 KB |
Output is correct |
28 |
Correct |
7 ms |
2508 KB |
Output is correct |
29 |
Correct |
22 ms |
5332 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
14 ms |
5332 KB |
Output is correct |
32 |
Correct |
10 ms |
5332 KB |
Output is correct |
33 |
Correct |
44 ms |
9716 KB |
Output is correct |
34 |
Correct |
31 ms |
9716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9716 KB |
Output is correct |
2 |
Correct |
2 ms |
9716 KB |
Output is correct |
3 |
Correct |
2 ms |
9716 KB |
Output is correct |
4 |
Correct |
2 ms |
9716 KB |
Output is correct |
5 |
Correct |
2 ms |
9716 KB |
Output is correct |
6 |
Correct |
2 ms |
9716 KB |
Output is correct |
7 |
Correct |
2 ms |
9716 KB |
Output is correct |
8 |
Correct |
2 ms |
9716 KB |
Output is correct |
9 |
Correct |
2 ms |
9716 KB |
Output is correct |
10 |
Correct |
2 ms |
9716 KB |
Output is correct |
11 |
Correct |
4 ms |
9716 KB |
Output is correct |
12 |
Correct |
2 ms |
9716 KB |
Output is correct |
13 |
Correct |
2 ms |
9716 KB |
Output is correct |
14 |
Correct |
3 ms |
9716 KB |
Output is correct |
15 |
Correct |
3 ms |
9716 KB |
Output is correct |
16 |
Correct |
3 ms |
9716 KB |
Output is correct |
17 |
Correct |
9 ms |
9716 KB |
Output is correct |
18 |
Correct |
4 ms |
9716 KB |
Output is correct |
19 |
Correct |
3 ms |
9716 KB |
Output is correct |
20 |
Correct |
4 ms |
9716 KB |
Output is correct |
21 |
Correct |
3 ms |
9716 KB |
Output is correct |
22 |
Correct |
3 ms |
9716 KB |
Output is correct |
23 |
Correct |
4 ms |
9716 KB |
Output is correct |
24 |
Correct |
5 ms |
9716 KB |
Output is correct |
25 |
Correct |
5 ms |
9716 KB |
Output is correct |
26 |
Correct |
4 ms |
9716 KB |
Output is correct |
27 |
Correct |
3 ms |
9716 KB |
Output is correct |
28 |
Correct |
7 ms |
9716 KB |
Output is correct |
29 |
Correct |
14 ms |
9716 KB |
Output is correct |
30 |
Correct |
5 ms |
9716 KB |
Output is correct |
31 |
Correct |
9 ms |
9716 KB |
Output is correct |
32 |
Correct |
7 ms |
9716 KB |
Output is correct |
33 |
Correct |
31 ms |
9716 KB |
Output is correct |
34 |
Correct |
30 ms |
9716 KB |
Output is correct |
35 |
Correct |
37 ms |
9716 KB |
Output is correct |
36 |
Correct |
7 ms |
9716 KB |
Output is correct |
37 |
Correct |
65 ms |
13760 KB |
Output is correct |
38 |
Correct |
64 ms |
13760 KB |
Output is correct |
39 |
Correct |
62 ms |
13760 KB |
Output is correct |
40 |
Correct |
60 ms |
13760 KB |
Output is correct |
41 |
Correct |
61 ms |
13760 KB |
Output is correct |
42 |
Correct |
9 ms |
13760 KB |
Output is correct |
43 |
Correct |
9 ms |
13760 KB |
Output is correct |
44 |
Correct |
8 ms |
13760 KB |
Output is correct |
45 |
Correct |
139 ms |
36528 KB |
Output is correct |
46 |
Correct |
150 ms |
36672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
36672 KB |
Output is correct |
2 |
Correct |
2 ms |
36672 KB |
Output is correct |
3 |
Correct |
2 ms |
36672 KB |
Output is correct |
4 |
Correct |
2 ms |
36672 KB |
Output is correct |
5 |
Correct |
2 ms |
36672 KB |
Output is correct |
6 |
Correct |
2 ms |
36672 KB |
Output is correct |
7 |
Correct |
2 ms |
36672 KB |
Output is correct |
8 |
Correct |
2 ms |
36672 KB |
Output is correct |
9 |
Correct |
2 ms |
36672 KB |
Output is correct |
10 |
Correct |
2 ms |
36672 KB |
Output is correct |
11 |
Correct |
3 ms |
36672 KB |
Output is correct |
12 |
Correct |
2 ms |
36672 KB |
Output is correct |
13 |
Correct |
2 ms |
36672 KB |
Output is correct |
14 |
Correct |
3 ms |
36672 KB |
Output is correct |
15 |
Correct |
3 ms |
36672 KB |
Output is correct |
16 |
Correct |
3 ms |
36672 KB |
Output is correct |
17 |
Correct |
6 ms |
36672 KB |
Output is correct |
18 |
Correct |
3 ms |
36672 KB |
Output is correct |
19 |
Correct |
3 ms |
36672 KB |
Output is correct |
20 |
Correct |
3 ms |
36672 KB |
Output is correct |
21 |
Correct |
2 ms |
36672 KB |
Output is correct |
22 |
Correct |
4 ms |
36672 KB |
Output is correct |
23 |
Correct |
5 ms |
36672 KB |
Output is correct |
24 |
Correct |
8 ms |
36672 KB |
Output is correct |
25 |
Correct |
6 ms |
36672 KB |
Output is correct |
26 |
Correct |
4 ms |
36672 KB |
Output is correct |
27 |
Correct |
4 ms |
36672 KB |
Output is correct |
28 |
Correct |
11 ms |
36672 KB |
Output is correct |
29 |
Correct |
23 ms |
36672 KB |
Output is correct |
30 |
Correct |
7 ms |
36672 KB |
Output is correct |
31 |
Correct |
8 ms |
36672 KB |
Output is correct |
32 |
Correct |
10 ms |
36672 KB |
Output is correct |
33 |
Correct |
47 ms |
36672 KB |
Output is correct |
34 |
Correct |
46 ms |
36672 KB |
Output is correct |
35 |
Correct |
36 ms |
36672 KB |
Output is correct |
36 |
Correct |
7 ms |
36672 KB |
Output is correct |
37 |
Correct |
62 ms |
36672 KB |
Output is correct |
38 |
Correct |
64 ms |
36672 KB |
Output is correct |
39 |
Correct |
91 ms |
36672 KB |
Output is correct |
40 |
Correct |
96 ms |
36672 KB |
Output is correct |
41 |
Correct |
59 ms |
36672 KB |
Output is correct |
42 |
Correct |
15 ms |
36672 KB |
Output is correct |
43 |
Correct |
15 ms |
36672 KB |
Output is correct |
44 |
Correct |
10 ms |
36672 KB |
Output is correct |
45 |
Correct |
170 ms |
36672 KB |
Output is correct |
46 |
Correct |
155 ms |
36672 KB |
Output is correct |
47 |
Correct |
301 ms |
56796 KB |
Output is correct |
48 |
Correct |
69 ms |
56796 KB |
Output is correct |
49 |
Correct |
47 ms |
56796 KB |
Output is correct |
50 |
Correct |
43 ms |
56796 KB |
Output is correct |
51 |
Correct |
154 ms |
56796 KB |
Output is correct |
52 |
Correct |
148 ms |
56796 KB |
Output is correct |
53 |
Correct |
47 ms |
56796 KB |
Output is correct |
54 |
Correct |
9 ms |
56796 KB |
Output is correct |
55 |
Correct |
13 ms |
56796 KB |
Output is correct |
56 |
Correct |
22 ms |
56796 KB |
Output is correct |
57 |
Correct |
134 ms |
56796 KB |
Output is correct |
58 |
Correct |
21 ms |
56796 KB |
Output is correct |
59 |
Correct |
30 ms |
56796 KB |
Output is correct |
60 |
Correct |
38 ms |
56796 KB |
Output is correct |
61 |
Correct |
35 ms |
56796 KB |
Output is correct |
62 |
Correct |
150 ms |
56796 KB |
Output is correct |
63 |
Execution timed out |
1008 ms |
181068 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |