#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;
}
}
}
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]);
}
}
deque<int> q;
for(int i = 1;i <= it;i++){
d[i] = 1e9;
}
d[b[0]] = 0;
q.push_front(b[0]);
while(!q.empty()){
int v = q[0];
q.pop_front();
for(int to:g[v]){
int w = (sgn(v) == sgn(to));
if(d[to] > d[v] + w){
d[to] = d[v] + w;
if(w == 0){
q.push_front(to);
}else{
q.push_back(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 |
49 ms |
123984 KB |
Output is correct |
2 |
Correct |
49 ms |
123728 KB |
Output is correct |
3 |
Correct |
51 ms |
123880 KB |
Output is correct |
4 |
Correct |
50 ms |
123732 KB |
Output is correct |
5 |
Correct |
48 ms |
123740 KB |
Output is correct |
6 |
Correct |
51 ms |
123740 KB |
Output is correct |
7 |
Correct |
50 ms |
123732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
123740 KB |
Output is correct |
2 |
Correct |
52 ms |
123728 KB |
Output is correct |
3 |
Correct |
50 ms |
123728 KB |
Output is correct |
4 |
Correct |
49 ms |
123732 KB |
Output is correct |
5 |
Correct |
49 ms |
123812 KB |
Output is correct |
6 |
Correct |
50 ms |
123728 KB |
Output is correct |
7 |
Correct |
49 ms |
123676 KB |
Output is correct |
8 |
Correct |
50 ms |
123724 KB |
Output is correct |
9 |
Correct |
51 ms |
123740 KB |
Output is correct |
10 |
Correct |
49 ms |
123728 KB |
Output is correct |
11 |
Correct |
50 ms |
123992 KB |
Output is correct |
12 |
Correct |
49 ms |
123728 KB |
Output is correct |
13 |
Correct |
49 ms |
123808 KB |
Output is correct |
14 |
Correct |
50 ms |
123984 KB |
Output is correct |
15 |
Correct |
49 ms |
123988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
123728 KB |
Output is correct |
2 |
Correct |
48 ms |
123732 KB |
Output is correct |
3 |
Correct |
49 ms |
123860 KB |
Output is correct |
4 |
Correct |
49 ms |
123732 KB |
Output is correct |
5 |
Correct |
52 ms |
123740 KB |
Output is correct |
6 |
Correct |
49 ms |
123740 KB |
Output is correct |
7 |
Correct |
48 ms |
123740 KB |
Output is correct |
8 |
Correct |
57 ms |
123732 KB |
Output is correct |
9 |
Correct |
50 ms |
123728 KB |
Output is correct |
10 |
Correct |
49 ms |
123728 KB |
Output is correct |
11 |
Correct |
50 ms |
123988 KB |
Output is correct |
12 |
Correct |
48 ms |
123732 KB |
Output is correct |
13 |
Correct |
50 ms |
123700 KB |
Output is correct |
14 |
Correct |
53 ms |
123996 KB |
Output is correct |
15 |
Correct |
49 ms |
123984 KB |
Output is correct |
16 |
Correct |
49 ms |
123988 KB |
Output is correct |
17 |
Correct |
54 ms |
124244 KB |
Output is correct |
18 |
Correct |
51 ms |
123948 KB |
Output is correct |
19 |
Correct |
51 ms |
123988 KB |
Output is correct |
20 |
Correct |
53 ms |
123988 KB |
Output is correct |
21 |
Correct |
50 ms |
123732 KB |
Output is correct |
22 |
Correct |
50 ms |
123884 KB |
Output is correct |
23 |
Correct |
50 ms |
123940 KB |
Output is correct |
24 |
Correct |
54 ms |
124244 KB |
Output is correct |
25 |
Correct |
50 ms |
124240 KB |
Output is correct |
26 |
Correct |
51 ms |
123984 KB |
Output is correct |
27 |
Correct |
50 ms |
123940 KB |
Output is correct |
28 |
Incorrect |
53 ms |
124752 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
123740 KB |
Output is correct |
2 |
Correct |
51 ms |
123880 KB |
Output is correct |
3 |
Correct |
49 ms |
123728 KB |
Output is correct |
4 |
Correct |
48 ms |
123624 KB |
Output is correct |
5 |
Correct |
49 ms |
123732 KB |
Output is correct |
6 |
Correct |
49 ms |
123772 KB |
Output is correct |
7 |
Correct |
49 ms |
123732 KB |
Output is correct |
8 |
Correct |
50 ms |
123764 KB |
Output is correct |
9 |
Correct |
52 ms |
123752 KB |
Output is correct |
10 |
Correct |
50 ms |
123728 KB |
Output is correct |
11 |
Correct |
50 ms |
123988 KB |
Output is correct |
12 |
Correct |
50 ms |
123732 KB |
Output is correct |
13 |
Correct |
49 ms |
123668 KB |
Output is correct |
14 |
Correct |
48 ms |
123988 KB |
Output is correct |
15 |
Correct |
55 ms |
123984 KB |
Output is correct |
16 |
Correct |
50 ms |
123988 KB |
Output is correct |
17 |
Correct |
50 ms |
124240 KB |
Output is correct |
18 |
Correct |
52 ms |
123996 KB |
Output is correct |
19 |
Correct |
50 ms |
123984 KB |
Output is correct |
20 |
Correct |
50 ms |
123984 KB |
Output is correct |
21 |
Correct |
49 ms |
123960 KB |
Output is correct |
22 |
Correct |
50 ms |
124136 KB |
Output is correct |
23 |
Correct |
50 ms |
123984 KB |
Output is correct |
24 |
Correct |
52 ms |
124244 KB |
Output is correct |
25 |
Correct |
49 ms |
124244 KB |
Output is correct |
26 |
Correct |
50 ms |
123996 KB |
Output is correct |
27 |
Correct |
51 ms |
124004 KB |
Output is correct |
28 |
Incorrect |
51 ms |
124756 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
123732 KB |
Output is correct |
2 |
Correct |
49 ms |
123740 KB |
Output is correct |
3 |
Correct |
52 ms |
123736 KB |
Output is correct |
4 |
Correct |
53 ms |
123732 KB |
Output is correct |
5 |
Correct |
49 ms |
123732 KB |
Output is correct |
6 |
Correct |
48 ms |
123652 KB |
Output is correct |
7 |
Correct |
48 ms |
123728 KB |
Output is correct |
8 |
Correct |
50 ms |
123732 KB |
Output is correct |
9 |
Correct |
50 ms |
123728 KB |
Output is correct |
10 |
Correct |
50 ms |
123732 KB |
Output is correct |
11 |
Correct |
49 ms |
123992 KB |
Output is correct |
12 |
Correct |
51 ms |
123884 KB |
Output is correct |
13 |
Correct |
49 ms |
123732 KB |
Output is correct |
14 |
Correct |
51 ms |
123984 KB |
Output is correct |
15 |
Correct |
49 ms |
123988 KB |
Output is correct |
16 |
Correct |
54 ms |
123972 KB |
Output is correct |
17 |
Correct |
52 ms |
124244 KB |
Output is correct |
18 |
Correct |
50 ms |
124180 KB |
Output is correct |
19 |
Correct |
50 ms |
123996 KB |
Output is correct |
20 |
Correct |
49 ms |
123984 KB |
Output is correct |
21 |
Correct |
50 ms |
123848 KB |
Output is correct |
22 |
Correct |
50 ms |
124124 KB |
Output is correct |
23 |
Correct |
52 ms |
123988 KB |
Output is correct |
24 |
Correct |
51 ms |
124240 KB |
Output is correct |
25 |
Correct |
50 ms |
124252 KB |
Output is correct |
26 |
Correct |
50 ms |
123992 KB |
Output is correct |
27 |
Correct |
50 ms |
123988 KB |
Output is correct |
28 |
Incorrect |
52 ms |
124752 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |