#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 > (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,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 |
20 ms |
47964 KB |
Output is correct |
2 |
Correct |
25 ms |
48088 KB |
Output is correct |
3 |
Correct |
19 ms |
47964 KB |
Output is correct |
4 |
Correct |
19 ms |
47980 KB |
Output is correct |
5 |
Correct |
18 ms |
47960 KB |
Output is correct |
6 |
Correct |
25 ms |
47964 KB |
Output is correct |
7 |
Correct |
27 ms |
47964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
47960 KB |
Output is correct |
2 |
Correct |
18 ms |
47984 KB |
Output is correct |
3 |
Correct |
18 ms |
48020 KB |
Output is correct |
4 |
Correct |
25 ms |
47964 KB |
Output is correct |
5 |
Correct |
19 ms |
48024 KB |
Output is correct |
6 |
Correct |
18 ms |
47964 KB |
Output is correct |
7 |
Correct |
18 ms |
47968 KB |
Output is correct |
8 |
Correct |
25 ms |
47964 KB |
Output is correct |
9 |
Correct |
28 ms |
47960 KB |
Output is correct |
10 |
Correct |
22 ms |
48220 KB |
Output is correct |
11 |
Correct |
24 ms |
48216 KB |
Output is correct |
12 |
Correct |
21 ms |
48216 KB |
Output is correct |
13 |
Correct |
21 ms |
48220 KB |
Output is correct |
14 |
Correct |
23 ms |
48476 KB |
Output is correct |
15 |
Correct |
22 ms |
48380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
47964 KB |
Output is correct |
2 |
Correct |
30 ms |
47964 KB |
Output is correct |
3 |
Correct |
29 ms |
47956 KB |
Output is correct |
4 |
Correct |
19 ms |
47960 KB |
Output is correct |
5 |
Correct |
23 ms |
47964 KB |
Output is correct |
6 |
Correct |
21 ms |
47960 KB |
Output is correct |
7 |
Correct |
21 ms |
47960 KB |
Output is correct |
8 |
Correct |
21 ms |
48148 KB |
Output is correct |
9 |
Correct |
20 ms |
47960 KB |
Output is correct |
10 |
Correct |
21 ms |
48220 KB |
Output is correct |
11 |
Correct |
22 ms |
48364 KB |
Output is correct |
12 |
Correct |
21 ms |
47964 KB |
Output is correct |
13 |
Correct |
21 ms |
48220 KB |
Output is correct |
14 |
Correct |
23 ms |
48472 KB |
Output is correct |
15 |
Correct |
22 ms |
48488 KB |
Output is correct |
16 |
Correct |
21 ms |
48220 KB |
Output is correct |
17 |
Correct |
23 ms |
48732 KB |
Output is correct |
18 |
Correct |
21 ms |
48476 KB |
Output is correct |
19 |
Correct |
21 ms |
48220 KB |
Output is correct |
20 |
Correct |
24 ms |
48220 KB |
Output is correct |
21 |
Correct |
21 ms |
48128 KB |
Output is correct |
22 |
Correct |
22 ms |
48216 KB |
Output is correct |
23 |
Correct |
22 ms |
48476 KB |
Output is correct |
24 |
Correct |
25 ms |
48728 KB |
Output is correct |
25 |
Correct |
23 ms |
48472 KB |
Output is correct |
26 |
Correct |
23 ms |
48476 KB |
Output is correct |
27 |
Correct |
29 ms |
48404 KB |
Output is correct |
28 |
Correct |
24 ms |
49244 KB |
Output is correct |
29 |
Correct |
31 ms |
51060 KB |
Output is correct |
30 |
Correct |
24 ms |
48988 KB |
Output is correct |
31 |
Correct |
27 ms |
49756 KB |
Output is correct |
32 |
Correct |
26 ms |
49252 KB |
Output is correct |
33 |
Correct |
47 ms |
54108 KB |
Output is correct |
34 |
Correct |
49 ms |
54100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
20 ms |
47964 KB |
Output is correct |
3 |
Correct |
21 ms |
48152 KB |
Output is correct |
4 |
Correct |
21 ms |
47964 KB |
Output is correct |
5 |
Correct |
20 ms |
47948 KB |
Output is correct |
6 |
Correct |
21 ms |
47964 KB |
Output is correct |
7 |
Correct |
21 ms |
47964 KB |
Output is correct |
8 |
Correct |
21 ms |
48156 KB |
Output is correct |
9 |
Correct |
21 ms |
47964 KB |
Output is correct |
10 |
Correct |
22 ms |
48220 KB |
Output is correct |
11 |
Correct |
24 ms |
48216 KB |
Output is correct |
12 |
Correct |
29 ms |
47956 KB |
Output is correct |
13 |
Correct |
20 ms |
47960 KB |
Output is correct |
14 |
Correct |
22 ms |
48476 KB |
Output is correct |
15 |
Correct |
21 ms |
48476 KB |
Output is correct |
16 |
Correct |
20 ms |
48216 KB |
Output is correct |
17 |
Correct |
22 ms |
48732 KB |
Output is correct |
18 |
Correct |
19 ms |
48272 KB |
Output is correct |
19 |
Correct |
19 ms |
48220 KB |
Output is correct |
20 |
Correct |
19 ms |
48216 KB |
Output is correct |
21 |
Correct |
19 ms |
48092 KB |
Output is correct |
22 |
Correct |
19 ms |
48408 KB |
Output is correct |
23 |
Correct |
19 ms |
48476 KB |
Output is correct |
24 |
Correct |
35 ms |
48728 KB |
Output is correct |
25 |
Correct |
20 ms |
48472 KB |
Output is correct |
26 |
Correct |
20 ms |
48432 KB |
Output is correct |
27 |
Correct |
19 ms |
48476 KB |
Output is correct |
28 |
Correct |
21 ms |
49376 KB |
Output is correct |
29 |
Correct |
37 ms |
51288 KB |
Output is correct |
30 |
Correct |
23 ms |
48988 KB |
Output is correct |
31 |
Correct |
27 ms |
49752 KB |
Output is correct |
32 |
Correct |
28 ms |
49348 KB |
Output is correct |
33 |
Correct |
43 ms |
54328 KB |
Output is correct |
34 |
Correct |
52 ms |
54156 KB |
Output is correct |
35 |
Correct |
56 ms |
53920 KB |
Output is correct |
36 |
Correct |
26 ms |
48988 KB |
Output is correct |
37 |
Correct |
87 ms |
58024 KB |
Output is correct |
38 |
Correct |
78 ms |
57828 KB |
Output is correct |
39 |
Correct |
83 ms |
57540 KB |
Output is correct |
40 |
Correct |
82 ms |
57444 KB |
Output is correct |
41 |
Correct |
76 ms |
57544 KB |
Output is correct |
42 |
Correct |
26 ms |
49068 KB |
Output is correct |
43 |
Correct |
28 ms |
48980 KB |
Output is correct |
44 |
Correct |
24 ms |
48864 KB |
Output is correct |
45 |
Correct |
156 ms |
73840 KB |
Output is correct |
46 |
Correct |
156 ms |
73804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47964 KB |
Output is correct |
2 |
Correct |
20 ms |
47956 KB |
Output is correct |
3 |
Correct |
27 ms |
47964 KB |
Output is correct |
4 |
Correct |
29 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
47960 KB |
Output is correct |
6 |
Correct |
22 ms |
47964 KB |
Output is correct |
7 |
Correct |
21 ms |
47924 KB |
Output is correct |
8 |
Correct |
22 ms |
48020 KB |
Output is correct |
9 |
Correct |
24 ms |
48156 KB |
Output is correct |
10 |
Correct |
23 ms |
48240 KB |
Output is correct |
11 |
Correct |
24 ms |
48224 KB |
Output is correct |
12 |
Correct |
27 ms |
48064 KB |
Output is correct |
13 |
Correct |
22 ms |
47964 KB |
Output is correct |
14 |
Correct |
24 ms |
48384 KB |
Output is correct |
15 |
Correct |
24 ms |
48476 KB |
Output is correct |
16 |
Correct |
22 ms |
48220 KB |
Output is correct |
17 |
Correct |
27 ms |
48764 KB |
Output is correct |
18 |
Correct |
25 ms |
48524 KB |
Output is correct |
19 |
Correct |
22 ms |
48376 KB |
Output is correct |
20 |
Correct |
22 ms |
48220 KB |
Output is correct |
21 |
Correct |
21 ms |
48324 KB |
Output is correct |
22 |
Correct |
20 ms |
48216 KB |
Output is correct |
23 |
Correct |
22 ms |
48476 KB |
Output is correct |
24 |
Correct |
25 ms |
48860 KB |
Output is correct |
25 |
Correct |
23 ms |
48360 KB |
Output is correct |
26 |
Correct |
21 ms |
48476 KB |
Output is correct |
27 |
Correct |
29 ms |
48368 KB |
Output is correct |
28 |
Correct |
33 ms |
49200 KB |
Output is correct |
29 |
Correct |
32 ms |
51284 KB |
Output is correct |
30 |
Correct |
25 ms |
48988 KB |
Output is correct |
31 |
Correct |
26 ms |
49672 KB |
Output is correct |
32 |
Correct |
27 ms |
49240 KB |
Output is correct |
33 |
Correct |
55 ms |
54108 KB |
Output is correct |
34 |
Correct |
47 ms |
54100 KB |
Output is correct |
35 |
Correct |
52 ms |
53780 KB |
Output is correct |
36 |
Correct |
23 ms |
48984 KB |
Output is correct |
37 |
Correct |
128 ms |
57936 KB |
Output is correct |
38 |
Correct |
97 ms |
57696 KB |
Output is correct |
39 |
Correct |
101 ms |
57640 KB |
Output is correct |
40 |
Correct |
115 ms |
57680 KB |
Output is correct |
41 |
Correct |
100 ms |
57684 KB |
Output is correct |
42 |
Correct |
30 ms |
49072 KB |
Output is correct |
43 |
Correct |
25 ms |
48988 KB |
Output is correct |
44 |
Correct |
24 ms |
48872 KB |
Output is correct |
45 |
Correct |
162 ms |
73752 KB |
Output is correct |
46 |
Correct |
180 ms |
73816 KB |
Output is correct |
47 |
Correct |
305 ms |
87376 KB |
Output is correct |
48 |
Correct |
55 ms |
60444 KB |
Output is correct |
49 |
Correct |
38 ms |
55892 KB |
Output is correct |
50 |
Correct |
38 ms |
55924 KB |
Output is correct |
51 |
Correct |
148 ms |
65444 KB |
Output is correct |
52 |
Correct |
140 ms |
66764 KB |
Output is correct |
53 |
Correct |
46 ms |
53540 KB |
Output is correct |
54 |
Correct |
24 ms |
49792 KB |
Output is correct |
55 |
Correct |
27 ms |
51036 KB |
Output is correct |
56 |
Correct |
31 ms |
51016 KB |
Output is correct |
57 |
Correct |
139 ms |
70428 KB |
Output is correct |
58 |
Correct |
49 ms |
52304 KB |
Output is correct |
59 |
Correct |
55 ms |
53840 KB |
Output is correct |
60 |
Correct |
59 ms |
55120 KB |
Output is correct |
61 |
Correct |
55 ms |
54408 KB |
Output is correct |
62 |
Correct |
100 ms |
73928 KB |
Output is correct |
63 |
Execution timed out |
2450 ms |
262144 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |