#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;
}
}
}
if(num > N){
cout << -2;
return;
}
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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
21 ms |
47968 KB |
Output is correct |
3 |
Correct |
21 ms |
48036 KB |
Output is correct |
4 |
Correct |
20 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
47960 KB |
Output is correct |
6 |
Correct |
22 ms |
47936 KB |
Output is correct |
7 |
Correct |
21 ms |
47964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47964 KB |
Output is correct |
2 |
Correct |
20 ms |
47964 KB |
Output is correct |
3 |
Correct |
23 ms |
48004 KB |
Output is correct |
4 |
Correct |
22 ms |
47964 KB |
Output is correct |
5 |
Correct |
21 ms |
48132 KB |
Output is correct |
6 |
Correct |
21 ms |
47964 KB |
Output is correct |
7 |
Correct |
21 ms |
47960 KB |
Output is correct |
8 |
Correct |
21 ms |
47964 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
22 ms |
48220 KB |
Output is correct |
11 |
Correct |
22 ms |
48424 KB |
Output is correct |
12 |
Correct |
22 ms |
47952 KB |
Output is correct |
13 |
Correct |
22 ms |
48216 KB |
Output is correct |
14 |
Correct |
24 ms |
48476 KB |
Output is correct |
15 |
Correct |
23 ms |
48476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
22 ms |
47964 KB |
Output is correct |
3 |
Correct |
20 ms |
47960 KB |
Output is correct |
4 |
Correct |
21 ms |
47964 KB |
Output is correct |
5 |
Correct |
24 ms |
48052 KB |
Output is correct |
6 |
Correct |
22 ms |
47964 KB |
Output is correct |
7 |
Correct |
22 ms |
47964 KB |
Output is correct |
8 |
Correct |
21 ms |
47964 KB |
Output is correct |
9 |
Correct |
22 ms |
47976 KB |
Output is correct |
10 |
Correct |
25 ms |
48100 KB |
Output is correct |
11 |
Correct |
23 ms |
48220 KB |
Output is correct |
12 |
Correct |
22 ms |
47960 KB |
Output is correct |
13 |
Correct |
22 ms |
48220 KB |
Output is correct |
14 |
Correct |
24 ms |
48476 KB |
Output is correct |
15 |
Correct |
22 ms |
48532 KB |
Output is correct |
16 |
Correct |
21 ms |
48336 KB |
Output is correct |
17 |
Correct |
26 ms |
48732 KB |
Output is correct |
18 |
Correct |
22 ms |
48384 KB |
Output is correct |
19 |
Correct |
22 ms |
48216 KB |
Output is correct |
20 |
Correct |
22 ms |
48172 KB |
Output is correct |
21 |
Correct |
24 ms |
48220 KB |
Output is correct |
22 |
Correct |
23 ms |
48216 KB |
Output is correct |
23 |
Correct |
22 ms |
48472 KB |
Output is correct |
24 |
Correct |
25 ms |
48732 KB |
Output is correct |
25 |
Correct |
23 ms |
48588 KB |
Output is correct |
26 |
Correct |
22 ms |
48356 KB |
Output is correct |
27 |
Correct |
22 ms |
48412 KB |
Output is correct |
28 |
Correct |
24 ms |
49244 KB |
Output is correct |
29 |
Correct |
31 ms |
51184 KB |
Output is correct |
30 |
Correct |
23 ms |
48988 KB |
Output is correct |
31 |
Correct |
27 ms |
49840 KB |
Output is correct |
32 |
Correct |
25 ms |
49236 KB |
Output is correct |
33 |
Correct |
45 ms |
54100 KB |
Output is correct |
34 |
Correct |
42 ms |
54096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47960 KB |
Output is correct |
2 |
Correct |
21 ms |
47964 KB |
Output is correct |
3 |
Correct |
23 ms |
47872 KB |
Output is correct |
4 |
Correct |
20 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
47964 KB |
Output is correct |
6 |
Correct |
21 ms |
47964 KB |
Output is correct |
7 |
Correct |
21 ms |
47944 KB |
Output is correct |
8 |
Correct |
21 ms |
47964 KB |
Output is correct |
9 |
Correct |
22 ms |
47964 KB |
Output is correct |
10 |
Correct |
23 ms |
48220 KB |
Output is correct |
11 |
Correct |
22 ms |
48220 KB |
Output is correct |
12 |
Correct |
21 ms |
48212 KB |
Output is correct |
13 |
Correct |
27 ms |
48156 KB |
Output is correct |
14 |
Correct |
22 ms |
48476 KB |
Output is correct |
15 |
Correct |
23 ms |
48476 KB |
Output is correct |
16 |
Correct |
22 ms |
48216 KB |
Output is correct |
17 |
Correct |
25 ms |
48732 KB |
Output is correct |
18 |
Correct |
21 ms |
48476 KB |
Output is correct |
19 |
Correct |
22 ms |
48220 KB |
Output is correct |
20 |
Correct |
22 ms |
48208 KB |
Output is correct |
21 |
Correct |
22 ms |
48216 KB |
Output is correct |
22 |
Correct |
23 ms |
48216 KB |
Output is correct |
23 |
Correct |
27 ms |
48464 KB |
Output is correct |
24 |
Correct |
26 ms |
48732 KB |
Output is correct |
25 |
Correct |
22 ms |
48472 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
25 ms |
48380 KB |
Output is correct |
28 |
Correct |
24 ms |
49244 KB |
Output is correct |
29 |
Correct |
31 ms |
51028 KB |
Output is correct |
30 |
Correct |
24 ms |
48952 KB |
Output is correct |
31 |
Correct |
28 ms |
49756 KB |
Output is correct |
32 |
Correct |
25 ms |
49244 KB |
Output is correct |
33 |
Correct |
50 ms |
54104 KB |
Output is correct |
34 |
Correct |
46 ms |
54108 KB |
Output is correct |
35 |
Correct |
53 ms |
53844 KB |
Output is correct |
36 |
Correct |
27 ms |
48984 KB |
Output is correct |
37 |
Correct |
75 ms |
58124 KB |
Output is correct |
38 |
Correct |
68 ms |
57680 KB |
Output is correct |
39 |
Correct |
76 ms |
57680 KB |
Output is correct |
40 |
Correct |
76 ms |
57684 KB |
Output is correct |
41 |
Correct |
68 ms |
57684 KB |
Output is correct |
42 |
Correct |
25 ms |
48980 KB |
Output is correct |
43 |
Correct |
25 ms |
48988 KB |
Output is correct |
44 |
Correct |
25 ms |
48860 KB |
Output is correct |
45 |
Correct |
137 ms |
73812 KB |
Output is correct |
46 |
Correct |
135 ms |
73652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47960 KB |
Output is correct |
2 |
Correct |
21 ms |
47964 KB |
Output is correct |
3 |
Correct |
21 ms |
47964 KB |
Output is correct |
4 |
Correct |
24 ms |
48220 KB |
Output is correct |
5 |
Correct |
21 ms |
47964 KB |
Output is correct |
6 |
Correct |
21 ms |
47964 KB |
Output is correct |
7 |
Correct |
22 ms |
47964 KB |
Output is correct |
8 |
Correct |
21 ms |
47960 KB |
Output is correct |
9 |
Correct |
22 ms |
47960 KB |
Output is correct |
10 |
Correct |
22 ms |
48220 KB |
Output is correct |
11 |
Correct |
22 ms |
48216 KB |
Output is correct |
12 |
Correct |
21 ms |
47980 KB |
Output is correct |
13 |
Correct |
21 ms |
47964 KB |
Output is correct |
14 |
Correct |
22 ms |
48544 KB |
Output is correct |
15 |
Correct |
22 ms |
48476 KB |
Output is correct |
16 |
Correct |
22 ms |
48348 KB |
Output is correct |
17 |
Correct |
25 ms |
48728 KB |
Output is correct |
18 |
Correct |
22 ms |
48476 KB |
Output is correct |
19 |
Correct |
22 ms |
48220 KB |
Output is correct |
20 |
Correct |
23 ms |
48216 KB |
Output is correct |
21 |
Correct |
22 ms |
48216 KB |
Output is correct |
22 |
Correct |
23 ms |
48220 KB |
Output is correct |
23 |
Correct |
23 ms |
48420 KB |
Output is correct |
24 |
Correct |
24 ms |
48664 KB |
Output is correct |
25 |
Correct |
26 ms |
48540 KB |
Output is correct |
26 |
Correct |
24 ms |
48476 KB |
Output is correct |
27 |
Correct |
24 ms |
48544 KB |
Output is correct |
28 |
Correct |
26 ms |
49244 KB |
Output is correct |
29 |
Correct |
34 ms |
51292 KB |
Output is correct |
30 |
Correct |
27 ms |
48988 KB |
Output is correct |
31 |
Correct |
28 ms |
49752 KB |
Output is correct |
32 |
Correct |
27 ms |
49240 KB |
Output is correct |
33 |
Correct |
45 ms |
54344 KB |
Output is correct |
34 |
Correct |
45 ms |
54104 KB |
Output is correct |
35 |
Correct |
54 ms |
53844 KB |
Output is correct |
36 |
Correct |
25 ms |
48988 KB |
Output is correct |
37 |
Correct |
84 ms |
58128 KB |
Output is correct |
38 |
Correct |
76 ms |
57828 KB |
Output is correct |
39 |
Correct |
76 ms |
57680 KB |
Output is correct |
40 |
Correct |
69 ms |
57680 KB |
Output is correct |
41 |
Correct |
76 ms |
57692 KB |
Output is correct |
42 |
Correct |
29 ms |
48988 KB |
Output is correct |
43 |
Correct |
26 ms |
48988 KB |
Output is correct |
44 |
Correct |
25 ms |
48992 KB |
Output is correct |
45 |
Correct |
139 ms |
73812 KB |
Output is correct |
46 |
Correct |
138 ms |
73828 KB |
Output is correct |
47 |
Correct |
256 ms |
87632 KB |
Output is correct |
48 |
Correct |
46 ms |
60496 KB |
Output is correct |
49 |
Correct |
47 ms |
55892 KB |
Output is correct |
50 |
Correct |
38 ms |
55892 KB |
Output is correct |
51 |
Correct |
106 ms |
65364 KB |
Output is correct |
52 |
Correct |
116 ms |
66688 KB |
Output is correct |
53 |
Correct |
41 ms |
53328 KB |
Output is correct |
54 |
Correct |
33 ms |
49760 KB |
Output is correct |
55 |
Correct |
27 ms |
51032 KB |
Output is correct |
56 |
Correct |
33 ms |
51168 KB |
Output is correct |
57 |
Correct |
136 ms |
70484 KB |
Output is correct |
58 |
Correct |
37 ms |
52296 KB |
Output is correct |
59 |
Correct |
37 ms |
53840 KB |
Output is correct |
60 |
Correct |
46 ms |
55128 KB |
Output is correct |
61 |
Correct |
39 ms |
54356 KB |
Output is correct |
62 |
Correct |
85 ms |
73900 KB |
Output is correct |
63 |
Incorrect |
26 ms |
48728 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |