#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 20, MOD = (int)1e9+7;
int n,m,b[N],p[N],ft[N];
vector<pair<int,int>> x[N],g[N * 5];
void add(int v,int u) {
g[v].push_back({u,1});
g[u].push_back({v,1});
}
int d[N * 5];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
cin >> b[i] >> p[i];
int d = b[i] % p[i];
x[p[i]].emplace_back(d,i);
}
int it = n - 1;
for(int i = 1;i < n;i++){
sort(x[i].begin(),x[i].end());
for(int j = 0;j < (int)x[i].size();j++){
if(!j || x[i][j].first != x[i][j - 1].first){
// cout << i << ' ';
for(int k = x[i][j].first;k < n;k += i){
ft[k] = ++it;
if(k - i >= 0){
add(ft[k - i],ft[k]);
}
g[ft[k]].push_back({k,0});
}
}
int v = b[x[i][j].second];
g[v].push_back({ft[v],0});
}
// cout << '\n';
}
set<pair<int,int>> st;
for(int i = 0;i <= it;i++){
d[i] = 1e9;
}
st.insert({0,b[0]});
d[b[0]] = 0;
while(!st.empty()){
auto [c,v] = (*st.begin());
st.erase(st.begin());
for(auto [to,w]:g[v]){
if(d[to] > d[v] + w){
st.erase({d[to],to});
d[to] = d[v] + w;
st.insert({d[to],to});
}
}
}
if(d[b[1]] == 1e9){
d[b[1]] = -1;
}
cout << d[b[1]];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
141136 KB |
Output is correct |
2 |
Correct |
58 ms |
141140 KB |
Output is correct |
3 |
Correct |
56 ms |
141136 KB |
Output is correct |
4 |
Correct |
59 ms |
141140 KB |
Output is correct |
5 |
Correct |
54 ms |
141380 KB |
Output is correct |
6 |
Correct |
57 ms |
141144 KB |
Output is correct |
7 |
Correct |
59 ms |
141136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
141136 KB |
Output is correct |
2 |
Correct |
57 ms |
141136 KB |
Output is correct |
3 |
Correct |
53 ms |
141136 KB |
Output is correct |
4 |
Correct |
56 ms |
141172 KB |
Output is correct |
5 |
Correct |
56 ms |
141136 KB |
Output is correct |
6 |
Correct |
58 ms |
141140 KB |
Output is correct |
7 |
Correct |
59 ms |
141228 KB |
Output is correct |
8 |
Correct |
60 ms |
141140 KB |
Output is correct |
9 |
Correct |
59 ms |
141140 KB |
Output is correct |
10 |
Correct |
58 ms |
141396 KB |
Output is correct |
11 |
Correct |
59 ms |
141648 KB |
Output is correct |
12 |
Correct |
67 ms |
141372 KB |
Output is correct |
13 |
Correct |
59 ms |
141456 KB |
Output is correct |
14 |
Correct |
59 ms |
141544 KB |
Output is correct |
15 |
Correct |
59 ms |
141652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
141140 KB |
Output is correct |
2 |
Correct |
59 ms |
141140 KB |
Output is correct |
3 |
Correct |
60 ms |
141372 KB |
Output is correct |
4 |
Correct |
57 ms |
141200 KB |
Output is correct |
5 |
Correct |
58 ms |
141140 KB |
Output is correct |
6 |
Correct |
60 ms |
141136 KB |
Output is correct |
7 |
Correct |
58 ms |
141604 KB |
Output is correct |
8 |
Correct |
61 ms |
141320 KB |
Output is correct |
9 |
Correct |
59 ms |
141136 KB |
Output is correct |
10 |
Correct |
56 ms |
141392 KB |
Output is correct |
11 |
Correct |
54 ms |
141648 KB |
Output is correct |
12 |
Correct |
58 ms |
141320 KB |
Output is correct |
13 |
Correct |
58 ms |
141248 KB |
Output is correct |
14 |
Correct |
58 ms |
141648 KB |
Output is correct |
15 |
Correct |
59 ms |
141648 KB |
Output is correct |
16 |
Correct |
57 ms |
141396 KB |
Output is correct |
17 |
Correct |
63 ms |
142164 KB |
Output is correct |
18 |
Correct |
59 ms |
141652 KB |
Output is correct |
19 |
Correct |
60 ms |
141652 KB |
Output is correct |
20 |
Correct |
57 ms |
141396 KB |
Output is correct |
21 |
Correct |
56 ms |
141392 KB |
Output is correct |
22 |
Correct |
57 ms |
141652 KB |
Output is correct |
23 |
Correct |
58 ms |
141648 KB |
Output is correct |
24 |
Correct |
64 ms |
142076 KB |
Output is correct |
25 |
Correct |
58 ms |
141700 KB |
Output is correct |
26 |
Correct |
55 ms |
141648 KB |
Output is correct |
27 |
Correct |
58 ms |
141652 KB |
Output is correct |
28 |
Correct |
60 ms |
142416 KB |
Output is correct |
29 |
Correct |
67 ms |
144540 KB |
Output is correct |
30 |
Correct |
61 ms |
142160 KB |
Output is correct |
31 |
Correct |
63 ms |
142932 KB |
Output is correct |
32 |
Correct |
62 ms |
142416 KB |
Output is correct |
33 |
Correct |
83 ms |
147536 KB |
Output is correct |
34 |
Correct |
79 ms |
147596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
141136 KB |
Output is correct |
2 |
Correct |
59 ms |
141140 KB |
Output is correct |
3 |
Correct |
59 ms |
141136 KB |
Output is correct |
4 |
Correct |
57 ms |
141140 KB |
Output is correct |
5 |
Correct |
57 ms |
141140 KB |
Output is correct |
6 |
Correct |
57 ms |
141396 KB |
Output is correct |
7 |
Correct |
59 ms |
141140 KB |
Output is correct |
8 |
Correct |
58 ms |
141136 KB |
Output is correct |
9 |
Correct |
57 ms |
141352 KB |
Output is correct |
10 |
Correct |
58 ms |
141396 KB |
Output is correct |
11 |
Correct |
59 ms |
141596 KB |
Output is correct |
12 |
Correct |
59 ms |
141400 KB |
Output is correct |
13 |
Correct |
60 ms |
141288 KB |
Output is correct |
14 |
Correct |
60 ms |
141652 KB |
Output is correct |
15 |
Correct |
60 ms |
141744 KB |
Output is correct |
16 |
Correct |
59 ms |
141404 KB |
Output is correct |
17 |
Correct |
61 ms |
142112 KB |
Output is correct |
18 |
Correct |
60 ms |
141564 KB |
Output is correct |
19 |
Correct |
59 ms |
141652 KB |
Output is correct |
20 |
Correct |
58 ms |
141472 KB |
Output is correct |
21 |
Correct |
58 ms |
141392 KB |
Output is correct |
22 |
Correct |
57 ms |
141652 KB |
Output is correct |
23 |
Correct |
58 ms |
141648 KB |
Output is correct |
24 |
Correct |
63 ms |
142048 KB |
Output is correct |
25 |
Correct |
58 ms |
141652 KB |
Output is correct |
26 |
Correct |
59 ms |
141624 KB |
Output is correct |
27 |
Correct |
62 ms |
141708 KB |
Output is correct |
28 |
Correct |
64 ms |
142420 KB |
Output is correct |
29 |
Correct |
68 ms |
144464 KB |
Output is correct |
30 |
Correct |
61 ms |
142164 KB |
Output is correct |
31 |
Correct |
64 ms |
142896 KB |
Output is correct |
32 |
Correct |
61 ms |
142392 KB |
Output is correct |
33 |
Correct |
82 ms |
147508 KB |
Output is correct |
34 |
Correct |
81 ms |
147540 KB |
Output is correct |
35 |
Correct |
94 ms |
147280 KB |
Output is correct |
36 |
Correct |
62 ms |
142392 KB |
Output is correct |
37 |
Correct |
110 ms |
151376 KB |
Output is correct |
38 |
Correct |
106 ms |
151432 KB |
Output is correct |
39 |
Correct |
108 ms |
151124 KB |
Output is correct |
40 |
Correct |
113 ms |
151124 KB |
Output is correct |
41 |
Correct |
118 ms |
151124 KB |
Output is correct |
42 |
Correct |
63 ms |
142704 KB |
Output is correct |
43 |
Correct |
65 ms |
142548 KB |
Output is correct |
44 |
Correct |
61 ms |
142560 KB |
Output is correct |
45 |
Correct |
173 ms |
167248 KB |
Output is correct |
46 |
Correct |
208 ms |
167248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
141324 KB |
Output is correct |
2 |
Correct |
57 ms |
141136 KB |
Output is correct |
3 |
Correct |
58 ms |
141148 KB |
Output is correct |
4 |
Correct |
58 ms |
141368 KB |
Output is correct |
5 |
Correct |
60 ms |
141284 KB |
Output is correct |
6 |
Correct |
71 ms |
141128 KB |
Output is correct |
7 |
Correct |
72 ms |
141136 KB |
Output is correct |
8 |
Correct |
59 ms |
141132 KB |
Output is correct |
9 |
Correct |
61 ms |
141136 KB |
Output is correct |
10 |
Correct |
58 ms |
141396 KB |
Output is correct |
11 |
Correct |
59 ms |
141536 KB |
Output is correct |
12 |
Correct |
58 ms |
141400 KB |
Output is correct |
13 |
Correct |
57 ms |
141396 KB |
Output is correct |
14 |
Correct |
59 ms |
141648 KB |
Output is correct |
15 |
Correct |
61 ms |
141648 KB |
Output is correct |
16 |
Correct |
59 ms |
141388 KB |
Output is correct |
17 |
Correct |
64 ms |
142104 KB |
Output is correct |
18 |
Correct |
62 ms |
141652 KB |
Output is correct |
19 |
Correct |
58 ms |
141468 KB |
Output is correct |
20 |
Correct |
58 ms |
141396 KB |
Output is correct |
21 |
Correct |
59 ms |
141460 KB |
Output is correct |
22 |
Correct |
62 ms |
141496 KB |
Output is correct |
23 |
Correct |
58 ms |
141652 KB |
Output is correct |
24 |
Correct |
61 ms |
141904 KB |
Output is correct |
25 |
Correct |
62 ms |
141652 KB |
Output is correct |
26 |
Correct |
60 ms |
141828 KB |
Output is correct |
27 |
Correct |
62 ms |
141788 KB |
Output is correct |
28 |
Correct |
66 ms |
142416 KB |
Output is correct |
29 |
Correct |
72 ms |
144360 KB |
Output is correct |
30 |
Correct |
62 ms |
142164 KB |
Output is correct |
31 |
Correct |
64 ms |
142932 KB |
Output is correct |
32 |
Correct |
65 ms |
142552 KB |
Output is correct |
33 |
Correct |
85 ms |
147596 KB |
Output is correct |
34 |
Correct |
79 ms |
147540 KB |
Output is correct |
35 |
Correct |
91 ms |
147516 KB |
Output is correct |
36 |
Correct |
62 ms |
142160 KB |
Output is correct |
37 |
Correct |
107 ms |
151568 KB |
Output is correct |
38 |
Correct |
106 ms |
151380 KB |
Output is correct |
39 |
Correct |
103 ms |
151120 KB |
Output is correct |
40 |
Correct |
111 ms |
151124 KB |
Output is correct |
41 |
Correct |
122 ms |
151124 KB |
Output is correct |
42 |
Correct |
61 ms |
142528 KB |
Output is correct |
43 |
Correct |
63 ms |
142548 KB |
Output is correct |
44 |
Correct |
61 ms |
142488 KB |
Output is correct |
45 |
Correct |
197 ms |
167296 KB |
Output is correct |
46 |
Correct |
178 ms |
167412 KB |
Output is correct |
47 |
Correct |
283 ms |
180948 KB |
Output is correct |
48 |
Correct |
84 ms |
154196 KB |
Output is correct |
49 |
Correct |
78 ms |
149432 KB |
Output is correct |
50 |
Correct |
74 ms |
149420 KB |
Output is correct |
51 |
Correct |
138 ms |
159056 KB |
Output is correct |
52 |
Correct |
157 ms |
160508 KB |
Output is correct |
53 |
Correct |
84 ms |
147128 KB |
Output is correct |
54 |
Correct |
61 ms |
142928 KB |
Output is correct |
55 |
Correct |
64 ms |
144212 KB |
Output is correct |
56 |
Correct |
71 ms |
144596 KB |
Output is correct |
57 |
Correct |
161 ms |
163664 KB |
Output is correct |
58 |
Correct |
70 ms |
145748 KB |
Output is correct |
59 |
Correct |
74 ms |
147284 KB |
Output is correct |
60 |
Correct |
82 ms |
148820 KB |
Output is correct |
61 |
Correct |
78 ms |
148044 KB |
Output is correct |
62 |
Correct |
124 ms |
167764 KB |
Output is correct |
63 |
Correct |
616 ms |
258640 KB |
Output is correct |
64 |
Runtime error |
330 ms |
262392 KB |
Execution killed with signal 9 |
65 |
Halted |
0 ms |
0 KB |
- |