#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 + 1), vx(N);
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
vmod[P[i]].push_back(B[i] % P[i]);
vx[B[i]].push_back(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 |
408 KB |
Output is correct |
4 |
Runtime error |
2 ms |
724 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Runtime error |
2 ms |
764 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
2 ms |
768 KB |
Output is correct |
4 |
Runtime error |
2 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
832 KB |
Output is correct |
2 |
Correct |
2 ms |
832 KB |
Output is correct |
3 |
Correct |
2 ms |
832 KB |
Output is correct |
4 |
Runtime error |
3 ms |
832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
832 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
2 ms |
960 KB |
Output is correct |
4 |
Runtime error |
2 ms |
960 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |