#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5226152+12, MOD = (int)1e9+7;
const int M = 3e4 + 21;
short n,m,b[M];
int ft[M];
vector<int> g[N];
vector<pair<short,short>> x[M];
void add(int v,int u) {
g[v].push_back(u);
g[u].push_back(v);
}
bool sgn(int x) {
return (x < n);
}
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 > (int)3e4 * (int)180){
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);
}
}
int v = b[x[i][j].second];
g[v].push_back(ft[v]);
}
// 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(int to:g[v]){
int w = (sgn(to) == sgn(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 |
65 ms |
123728 KB |
Output is correct |
2 |
Correct |
47 ms |
123728 KB |
Output is correct |
3 |
Correct |
64 ms |
123728 KB |
Output is correct |
4 |
Correct |
49 ms |
123736 KB |
Output is correct |
5 |
Correct |
45 ms |
123728 KB |
Output is correct |
6 |
Correct |
58 ms |
123648 KB |
Output is correct |
7 |
Correct |
64 ms |
123804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
123732 KB |
Output is correct |
2 |
Correct |
55 ms |
123876 KB |
Output is correct |
3 |
Correct |
52 ms |
123700 KB |
Output is correct |
4 |
Correct |
54 ms |
123684 KB |
Output is correct |
5 |
Correct |
51 ms |
123776 KB |
Output is correct |
6 |
Correct |
47 ms |
123720 KB |
Output is correct |
7 |
Correct |
65 ms |
123848 KB |
Output is correct |
8 |
Correct |
66 ms |
123684 KB |
Output is correct |
9 |
Correct |
60 ms |
123728 KB |
Output is correct |
10 |
Correct |
52 ms |
123788 KB |
Output is correct |
11 |
Correct |
60 ms |
123944 KB |
Output is correct |
12 |
Correct |
48 ms |
123824 KB |
Output is correct |
13 |
Correct |
48 ms |
123664 KB |
Output is correct |
14 |
Correct |
49 ms |
124184 KB |
Output is correct |
15 |
Correct |
66 ms |
123984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
123728 KB |
Output is correct |
2 |
Correct |
56 ms |
123728 KB |
Output is correct |
3 |
Correct |
53 ms |
123728 KB |
Output is correct |
4 |
Correct |
43 ms |
123732 KB |
Output is correct |
5 |
Correct |
53 ms |
123836 KB |
Output is correct |
6 |
Correct |
51 ms |
123732 KB |
Output is correct |
7 |
Correct |
50 ms |
123732 KB |
Output is correct |
8 |
Correct |
51 ms |
123728 KB |
Output is correct |
9 |
Correct |
59 ms |
123728 KB |
Output is correct |
10 |
Correct |
51 ms |
123788 KB |
Output is correct |
11 |
Correct |
54 ms |
124140 KB |
Output is correct |
12 |
Correct |
47 ms |
123888 KB |
Output is correct |
13 |
Correct |
48 ms |
123740 KB |
Output is correct |
14 |
Correct |
54 ms |
123988 KB |
Output is correct |
15 |
Correct |
52 ms |
124228 KB |
Output is correct |
16 |
Correct |
63 ms |
124036 KB |
Output is correct |
17 |
Correct |
54 ms |
124528 KB |
Output is correct |
18 |
Correct |
48 ms |
123988 KB |
Output is correct |
19 |
Correct |
50 ms |
123984 KB |
Output is correct |
20 |
Correct |
48 ms |
123960 KB |
Output is correct |
21 |
Correct |
50 ms |
123732 KB |
Output is correct |
22 |
Correct |
52 ms |
124136 KB |
Output is correct |
23 |
Correct |
46 ms |
123984 KB |
Output is correct |
24 |
Correct |
61 ms |
124408 KB |
Output is correct |
25 |
Correct |
56 ms |
124240 KB |
Output is correct |
26 |
Correct |
53 ms |
124056 KB |
Output is correct |
27 |
Correct |
70 ms |
123988 KB |
Output is correct |
28 |
Correct |
67 ms |
124548 KB |
Output is correct |
29 |
Correct |
59 ms |
126040 KB |
Output is correct |
30 |
Correct |
50 ms |
124496 KB |
Output is correct |
31 |
Correct |
78 ms |
124992 KB |
Output is correct |
32 |
Correct |
60 ms |
124756 KB |
Output is correct |
33 |
Correct |
73 ms |
128012 KB |
Output is correct |
34 |
Correct |
76 ms |
128088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
123732 KB |
Output is correct |
2 |
Correct |
48 ms |
123732 KB |
Output is correct |
3 |
Correct |
47 ms |
123728 KB |
Output is correct |
4 |
Correct |
47 ms |
123876 KB |
Output is correct |
5 |
Correct |
47 ms |
123732 KB |
Output is correct |
6 |
Correct |
47 ms |
123728 KB |
Output is correct |
7 |
Correct |
50 ms |
123704 KB |
Output is correct |
8 |
Correct |
57 ms |
123728 KB |
Output is correct |
9 |
Correct |
49 ms |
123732 KB |
Output is correct |
10 |
Correct |
49 ms |
123912 KB |
Output is correct |
11 |
Correct |
68 ms |
123984 KB |
Output is correct |
12 |
Correct |
53 ms |
123752 KB |
Output is correct |
13 |
Correct |
56 ms |
123728 KB |
Output is correct |
14 |
Correct |
53 ms |
123988 KB |
Output is correct |
15 |
Correct |
52 ms |
123988 KB |
Output is correct |
16 |
Correct |
48 ms |
123988 KB |
Output is correct |
17 |
Correct |
74 ms |
124496 KB |
Output is correct |
18 |
Correct |
50 ms |
123984 KB |
Output is correct |
19 |
Correct |
53 ms |
123928 KB |
Output is correct |
20 |
Correct |
52 ms |
123984 KB |
Output is correct |
21 |
Correct |
50 ms |
123732 KB |
Output is correct |
22 |
Correct |
50 ms |
123988 KB |
Output is correct |
23 |
Correct |
52 ms |
124080 KB |
Output is correct |
24 |
Correct |
51 ms |
124368 KB |
Output is correct |
25 |
Correct |
57 ms |
123988 KB |
Output is correct |
26 |
Correct |
51 ms |
123988 KB |
Output is correct |
27 |
Correct |
55 ms |
124156 KB |
Output is correct |
28 |
Correct |
50 ms |
124752 KB |
Output is correct |
29 |
Correct |
59 ms |
126032 KB |
Output is correct |
30 |
Correct |
56 ms |
124496 KB |
Output is correct |
31 |
Correct |
56 ms |
125008 KB |
Output is correct |
32 |
Correct |
59 ms |
124760 KB |
Output is correct |
33 |
Correct |
74 ms |
128080 KB |
Output is correct |
34 |
Correct |
71 ms |
128084 KB |
Output is correct |
35 |
Correct |
86 ms |
128404 KB |
Output is correct |
36 |
Correct |
53 ms |
124504 KB |
Output is correct |
37 |
Correct |
99 ms |
131272 KB |
Output is correct |
38 |
Correct |
98 ms |
131360 KB |
Output is correct |
39 |
Correct |
95 ms |
131156 KB |
Output is correct |
40 |
Correct |
98 ms |
131152 KB |
Output is correct |
41 |
Correct |
92 ms |
131152 KB |
Output is correct |
42 |
Correct |
53 ms |
124316 KB |
Output is correct |
43 |
Correct |
56 ms |
124240 KB |
Output is correct |
44 |
Correct |
57 ms |
124144 KB |
Output is correct |
45 |
Correct |
140 ms |
142672 KB |
Output is correct |
46 |
Correct |
148 ms |
142420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
123848 KB |
Output is correct |
2 |
Correct |
51 ms |
123728 KB |
Output is correct |
3 |
Correct |
51 ms |
123728 KB |
Output is correct |
4 |
Correct |
49 ms |
123728 KB |
Output is correct |
5 |
Correct |
50 ms |
123732 KB |
Output is correct |
6 |
Correct |
49 ms |
123732 KB |
Output is correct |
7 |
Correct |
50 ms |
123732 KB |
Output is correct |
8 |
Correct |
51 ms |
123728 KB |
Output is correct |
9 |
Correct |
49 ms |
123800 KB |
Output is correct |
10 |
Correct |
51 ms |
123740 KB |
Output is correct |
11 |
Correct |
54 ms |
123992 KB |
Output is correct |
12 |
Correct |
48 ms |
123736 KB |
Output is correct |
13 |
Correct |
49 ms |
123732 KB |
Output is correct |
14 |
Correct |
56 ms |
123984 KB |
Output is correct |
15 |
Correct |
48 ms |
123984 KB |
Output is correct |
16 |
Correct |
51 ms |
123988 KB |
Output is correct |
17 |
Correct |
52 ms |
124500 KB |
Output is correct |
18 |
Correct |
53 ms |
123984 KB |
Output is correct |
19 |
Correct |
54 ms |
124032 KB |
Output is correct |
20 |
Correct |
50 ms |
123896 KB |
Output is correct |
21 |
Correct |
47 ms |
123732 KB |
Output is correct |
22 |
Correct |
47 ms |
123984 KB |
Output is correct |
23 |
Correct |
51 ms |
124068 KB |
Output is correct |
24 |
Correct |
54 ms |
124244 KB |
Output is correct |
25 |
Correct |
52 ms |
124084 KB |
Output is correct |
26 |
Correct |
51 ms |
124192 KB |
Output is correct |
27 |
Correct |
50 ms |
124036 KB |
Output is correct |
28 |
Correct |
56 ms |
124608 KB |
Output is correct |
29 |
Correct |
62 ms |
126036 KB |
Output is correct |
30 |
Correct |
53 ms |
124508 KB |
Output is correct |
31 |
Correct |
56 ms |
124872 KB |
Output is correct |
32 |
Correct |
53 ms |
124752 KB |
Output is correct |
33 |
Correct |
72 ms |
128124 KB |
Output is correct |
34 |
Correct |
74 ms |
128084 KB |
Output is correct |
35 |
Correct |
74 ms |
128260 KB |
Output is correct |
36 |
Correct |
55 ms |
124496 KB |
Output is correct |
37 |
Correct |
108 ms |
131416 KB |
Output is correct |
38 |
Correct |
92 ms |
131412 KB |
Output is correct |
39 |
Correct |
94 ms |
131272 KB |
Output is correct |
40 |
Correct |
90 ms |
131156 KB |
Output is correct |
41 |
Correct |
96 ms |
131340 KB |
Output is correct |
42 |
Correct |
55 ms |
124268 KB |
Output is correct |
43 |
Correct |
53 ms |
124196 KB |
Output is correct |
44 |
Correct |
53 ms |
124244 KB |
Output is correct |
45 |
Correct |
150 ms |
142576 KB |
Output is correct |
46 |
Correct |
172 ms |
142632 KB |
Output is correct |
47 |
Correct |
278 ms |
152116 KB |
Output is correct |
48 |
Correct |
71 ms |
133208 KB |
Output is correct |
49 |
Correct |
72 ms |
129836 KB |
Output is correct |
50 |
Correct |
75 ms |
129876 KB |
Output is correct |
51 |
Correct |
159 ms |
136912 KB |
Output is correct |
52 |
Correct |
135 ms |
137880 KB |
Output is correct |
53 |
Correct |
72 ms |
128176 KB |
Output is correct |
54 |
Correct |
57 ms |
124948 KB |
Output is correct |
55 |
Correct |
58 ms |
125920 KB |
Output is correct |
56 |
Correct |
62 ms |
126148 KB |
Output is correct |
57 |
Correct |
164 ms |
139236 KB |
Output is correct |
58 |
Correct |
62 ms |
126592 KB |
Output is correct |
59 |
Correct |
75 ms |
127828 KB |
Output is correct |
60 |
Correct |
76 ms |
128688 KB |
Output is correct |
61 |
Correct |
85 ms |
128304 KB |
Output is correct |
62 |
Correct |
111 ms |
142416 KB |
Output is correct |
63 |
Correct |
582 ms |
205648 KB |
Output is correct |
64 |
Correct |
665 ms |
223824 KB |
Output is correct |
65 |
Runtime error |
320 ms |
262144 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |