#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6 +12, MOD = (int)1e9+7;
const int M = 3e4 + 21;
int n,m,b[M],ft[M];
vector<pair<int,int>> x[M],g[N];
void add(int v,int u) {
g[v].push_back({u,1});
g[u].push_back({v,1});
}
int d[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
int p;
cin >> b[i] >> p;
int d = b[i] % p;
x[p].emplace_back(d,i);
}
int it = n - 1;
int num = 0;
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){
num += (n - x[i][j].first) / i;
}
}
}
assert(num <= (int)3e4 * (int)sqrtl(3e4));
for(int i = 1;i < n;i++){
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 |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
22 ms |
47964 KB |
Output is correct |
3 |
Correct |
26 ms |
47964 KB |
Output is correct |
4 |
Correct |
21 ms |
48164 KB |
Output is correct |
5 |
Correct |
22 ms |
48128 KB |
Output is correct |
6 |
Correct |
21 ms |
47960 KB |
Output is correct |
7 |
Correct |
21 ms |
47960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
21 ms |
48152 KB |
Output is correct |
3 |
Correct |
21 ms |
47964 KB |
Output is correct |
4 |
Correct |
21 ms |
47960 KB |
Output is correct |
5 |
Correct |
25 ms |
47952 KB |
Output is correct |
6 |
Correct |
21 ms |
47964 KB |
Output is correct |
7 |
Correct |
22 ms |
47964 KB |
Output is correct |
8 |
Correct |
26 ms |
47916 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
21 ms |
48216 KB |
Output is correct |
11 |
Correct |
32 ms |
48348 KB |
Output is correct |
12 |
Correct |
24 ms |
48184 KB |
Output is correct |
13 |
Correct |
19 ms |
48220 KB |
Output is correct |
14 |
Correct |
21 ms |
48476 KB |
Output is correct |
15 |
Correct |
21 ms |
48476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
47964 KB |
Output is correct |
2 |
Correct |
19 ms |
48152 KB |
Output is correct |
3 |
Correct |
24 ms |
47964 KB |
Output is correct |
4 |
Correct |
19 ms |
48116 KB |
Output is correct |
5 |
Correct |
20 ms |
47964 KB |
Output is correct |
6 |
Correct |
20 ms |
47964 KB |
Output is correct |
7 |
Correct |
23 ms |
47964 KB |
Output is correct |
8 |
Correct |
20 ms |
47972 KB |
Output is correct |
9 |
Correct |
19 ms |
47960 KB |
Output is correct |
10 |
Correct |
29 ms |
48216 KB |
Output is correct |
11 |
Correct |
26 ms |
48220 KB |
Output is correct |
12 |
Correct |
23 ms |
48144 KB |
Output is correct |
13 |
Correct |
21 ms |
48116 KB |
Output is correct |
14 |
Correct |
24 ms |
48480 KB |
Output is correct |
15 |
Correct |
24 ms |
48396 KB |
Output is correct |
16 |
Correct |
22 ms |
48220 KB |
Output is correct |
17 |
Correct |
25 ms |
48816 KB |
Output is correct |
18 |
Correct |
23 ms |
48336 KB |
Output is correct |
19 |
Correct |
21 ms |
48220 KB |
Output is correct |
20 |
Correct |
23 ms |
48320 KB |
Output is correct |
21 |
Correct |
21 ms |
48220 KB |
Output is correct |
22 |
Correct |
25 ms |
48376 KB |
Output is correct |
23 |
Correct |
24 ms |
48476 KB |
Output is correct |
24 |
Correct |
26 ms |
48720 KB |
Output is correct |
25 |
Correct |
24 ms |
48476 KB |
Output is correct |
26 |
Correct |
23 ms |
48544 KB |
Output is correct |
27 |
Correct |
23 ms |
48364 KB |
Output is correct |
28 |
Correct |
24 ms |
49244 KB |
Output is correct |
29 |
Correct |
37 ms |
51204 KB |
Output is correct |
30 |
Correct |
24 ms |
49036 KB |
Output is correct |
31 |
Correct |
28 ms |
49880 KB |
Output is correct |
32 |
Correct |
25 ms |
49244 KB |
Output is correct |
33 |
Correct |
46 ms |
54096 KB |
Output is correct |
34 |
Correct |
42 ms |
54352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
47940 KB |
Output is correct |
2 |
Correct |
20 ms |
47964 KB |
Output is correct |
3 |
Correct |
18 ms |
47976 KB |
Output is correct |
4 |
Correct |
18 ms |
47960 KB |
Output is correct |
5 |
Correct |
18 ms |
47964 KB |
Output is correct |
6 |
Correct |
18 ms |
47964 KB |
Output is correct |
7 |
Correct |
16 ms |
47964 KB |
Output is correct |
8 |
Correct |
18 ms |
48132 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
17 ms |
48108 KB |
Output is correct |
11 |
Correct |
24 ms |
48256 KB |
Output is correct |
12 |
Correct |
16 ms |
48220 KB |
Output is correct |
13 |
Correct |
20 ms |
48004 KB |
Output is correct |
14 |
Correct |
20 ms |
48476 KB |
Output is correct |
15 |
Correct |
20 ms |
48472 KB |
Output is correct |
16 |
Correct |
21 ms |
48360 KB |
Output is correct |
17 |
Correct |
22 ms |
48984 KB |
Output is correct |
18 |
Correct |
29 ms |
48476 KB |
Output is correct |
19 |
Correct |
19 ms |
48220 KB |
Output is correct |
20 |
Correct |
20 ms |
48220 KB |
Output is correct |
21 |
Correct |
19 ms |
48040 KB |
Output is correct |
22 |
Correct |
19 ms |
48216 KB |
Output is correct |
23 |
Correct |
21 ms |
48412 KB |
Output is correct |
24 |
Correct |
23 ms |
48728 KB |
Output is correct |
25 |
Correct |
21 ms |
48476 KB |
Output is correct |
26 |
Correct |
22 ms |
48392 KB |
Output is correct |
27 |
Correct |
22 ms |
48476 KB |
Output is correct |
28 |
Correct |
22 ms |
49244 KB |
Output is correct |
29 |
Correct |
34 ms |
51288 KB |
Output is correct |
30 |
Correct |
28 ms |
48944 KB |
Output is correct |
31 |
Correct |
28 ms |
49752 KB |
Output is correct |
32 |
Correct |
23 ms |
49244 KB |
Output is correct |
33 |
Correct |
48 ms |
54364 KB |
Output is correct |
34 |
Correct |
39 ms |
54100 KB |
Output is correct |
35 |
Correct |
47 ms |
54100 KB |
Output is correct |
36 |
Correct |
25 ms |
48988 KB |
Output is correct |
37 |
Correct |
77 ms |
58284 KB |
Output is correct |
38 |
Correct |
76 ms |
58092 KB |
Output is correct |
39 |
Correct |
75 ms |
57936 KB |
Output is correct |
40 |
Correct |
69 ms |
57948 KB |
Output is correct |
41 |
Correct |
75 ms |
57812 KB |
Output is correct |
42 |
Correct |
27 ms |
49140 KB |
Output is correct |
43 |
Correct |
26 ms |
49236 KB |
Output is correct |
44 |
Correct |
26 ms |
49120 KB |
Output is correct |
45 |
Correct |
139 ms |
74056 KB |
Output is correct |
46 |
Correct |
145 ms |
73808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47960 KB |
Output is correct |
2 |
Correct |
23 ms |
47964 KB |
Output is correct |
3 |
Correct |
21 ms |
47960 KB |
Output is correct |
4 |
Correct |
21 ms |
48148 KB |
Output is correct |
5 |
Correct |
21 ms |
47960 KB |
Output is correct |
6 |
Correct |
26 ms |
47964 KB |
Output is correct |
7 |
Correct |
22 ms |
47964 KB |
Output is correct |
8 |
Correct |
22 ms |
47964 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
24 ms |
48220 KB |
Output is correct |
11 |
Correct |
23 ms |
48472 KB |
Output is correct |
12 |
Correct |
22 ms |
48220 KB |
Output is correct |
13 |
Correct |
22 ms |
48216 KB |
Output is correct |
14 |
Correct |
22 ms |
48760 KB |
Output is correct |
15 |
Correct |
22 ms |
48476 KB |
Output is correct |
16 |
Correct |
24 ms |
48204 KB |
Output is correct |
17 |
Correct |
24 ms |
48988 KB |
Output is correct |
18 |
Correct |
21 ms |
48472 KB |
Output is correct |
19 |
Correct |
22 ms |
48220 KB |
Output is correct |
20 |
Correct |
22 ms |
48220 KB |
Output is correct |
21 |
Correct |
21 ms |
48116 KB |
Output is correct |
22 |
Correct |
22 ms |
48476 KB |
Output is correct |
23 |
Correct |
23 ms |
48300 KB |
Output is correct |
24 |
Correct |
31 ms |
48732 KB |
Output is correct |
25 |
Correct |
23 ms |
48472 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
23 ms |
48380 KB |
Output is correct |
28 |
Correct |
25 ms |
49244 KB |
Output is correct |
29 |
Correct |
31 ms |
51168 KB |
Output is correct |
30 |
Correct |
24 ms |
48988 KB |
Output is correct |
31 |
Correct |
37 ms |
49784 KB |
Output is correct |
32 |
Correct |
26 ms |
49240 KB |
Output is correct |
33 |
Correct |
46 ms |
54364 KB |
Output is correct |
34 |
Correct |
43 ms |
54364 KB |
Output is correct |
35 |
Correct |
52 ms |
54100 KB |
Output is correct |
36 |
Correct |
25 ms |
48984 KB |
Output is correct |
37 |
Correct |
79 ms |
58196 KB |
Output is correct |
38 |
Correct |
73 ms |
58120 KB |
Output is correct |
39 |
Correct |
69 ms |
57960 KB |
Output is correct |
40 |
Correct |
83 ms |
57940 KB |
Output is correct |
41 |
Correct |
70 ms |
57936 KB |
Output is correct |
42 |
Correct |
26 ms |
49236 KB |
Output is correct |
43 |
Correct |
26 ms |
49244 KB |
Output is correct |
44 |
Correct |
28 ms |
49080 KB |
Output is correct |
45 |
Correct |
132 ms |
74064 KB |
Output is correct |
46 |
Correct |
131 ms |
73984 KB |
Output is correct |
47 |
Correct |
251 ms |
87632 KB |
Output is correct |
48 |
Correct |
53 ms |
61032 KB |
Output is correct |
49 |
Correct |
39 ms |
56148 KB |
Output is correct |
50 |
Correct |
38 ms |
56156 KB |
Output is correct |
51 |
Correct |
116 ms |
65620 KB |
Output is correct |
52 |
Correct |
121 ms |
67096 KB |
Output is correct |
53 |
Correct |
44 ms |
53840 KB |
Output is correct |
54 |
Correct |
25 ms |
49756 KB |
Output is correct |
55 |
Correct |
27 ms |
51032 KB |
Output is correct |
56 |
Correct |
31 ms |
51160 KB |
Output is correct |
57 |
Correct |
139 ms |
70500 KB |
Output is correct |
58 |
Correct |
33 ms |
52316 KB |
Output is correct |
59 |
Correct |
37 ms |
54096 KB |
Output is correct |
60 |
Correct |
47 ms |
55372 KB |
Output is correct |
61 |
Correct |
40 ms |
54696 KB |
Output is correct |
62 |
Correct |
85 ms |
74320 KB |
Output is correct |
63 |
Runtime error |
704 ms |
262144 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |