#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 300005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
ll n, m, d[MAXN], b0 = 0, b1 = 0;
vector<ll> p[MAXN];
vector<pair<ll, ll>> adj[MAXN];
int main(){
cin >> n >> m;
FOR(i, 1, m){
ll bi, pi; cin >> bi >> pi;
if(i == 1) b0 = bi;
if(i == 2) b1 = bi;
p[bi].push_back(pi);
}
FOR(i, 0, n - 1){
for(ll pi : p[i]){
ll j = i;
while(0 <= j - pi){
j -= pi;
adj[i].push_back({j, (i - j) / pi});
}
j = i;
while(j + pi <= n){
j += pi;
adj[i].push_back({j, (j - i) / pi});
}
}
d[i] = -1;
}
/*cout << "b0, b1 are " << b0 << " and " << b1 << endl;
FOR(i, 0, n - 1){
cout << "edges from " << i << ": " << endl;
for(auto j : adj[i]){
cout << " goes to " << j.first << " in " << j.second << " jumps" << endl;
}
}*/
priority_queue<pair<ll, ll>> pq;
d[b0] = 0, pq.push({0, b0});
while(!pq.empty()){
auto pqi = pq.top(); pqi.first *= -1;
pq.pop();
if(d[pqi.second] < pqi.first) continue;
if(pqi.second == b1) break;
for(auto i : adj[pqi.second]){
if(d[i.first] == -1 || pqi.first + i.second < d[i.first]){
d[i.first] = pqi.first + i.second;
pq.push({-d[i.first], i.first});
}
}
// cout << "HERE " << pqi.second << " w/ d = " << pqi.first << endl;
}
cout << d[b1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
6 ms |
14488 KB |
Output is correct |
4 |
Correct |
7 ms |
14432 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14428 KB |
Output is correct |
7 |
Correct |
7 ms |
14380 KB |
Output is correct |
8 |
Correct |
8 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14552 KB |
Output is correct |
11 |
Correct |
7 ms |
14684 KB |
Output is correct |
12 |
Correct |
11 ms |
18756 KB |
Output is correct |
13 |
Correct |
11 ms |
18764 KB |
Output is correct |
14 |
Correct |
8 ms |
14632 KB |
Output is correct |
15 |
Correct |
8 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
14680 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 KB |
Output is correct |
8 |
Correct |
7 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
6 ms |
14428 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
10 ms |
18636 KB |
Output is correct |
13 |
Correct |
11 ms |
18896 KB |
Output is correct |
14 |
Correct |
6 ms |
14544 KB |
Output is correct |
15 |
Correct |
6 ms |
14680 KB |
Output is correct |
16 |
Correct |
7 ms |
14424 KB |
Output is correct |
17 |
Correct |
8 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
14684 KB |
Output is correct |
19 |
Correct |
7 ms |
14552 KB |
Output is correct |
20 |
Correct |
81 ms |
78596 KB |
Output is correct |
21 |
Correct |
7 ms |
14428 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
6 ms |
14596 KB |
Output is correct |
24 |
Correct |
11 ms |
14684 KB |
Output is correct |
25 |
Correct |
8 ms |
14544 KB |
Output is correct |
26 |
Correct |
83 ms |
80244 KB |
Output is correct |
27 |
Correct |
85 ms |
80332 KB |
Output is correct |
28 |
Correct |
8 ms |
14940 KB |
Output is correct |
29 |
Correct |
8 ms |
15708 KB |
Output is correct |
30 |
Correct |
7 ms |
14940 KB |
Output is correct |
31 |
Correct |
8 ms |
15196 KB |
Output is correct |
32 |
Correct |
8 ms |
15196 KB |
Output is correct |
33 |
Correct |
10 ms |
16968 KB |
Output is correct |
34 |
Correct |
10 ms |
16856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14428 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14536 KB |
Output is correct |
6 |
Correct |
8 ms |
14680 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14680 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Correct |
7 ms |
14548 KB |
Output is correct |
12 |
Correct |
12 ms |
18736 KB |
Output is correct |
13 |
Correct |
12 ms |
19008 KB |
Output is correct |
14 |
Correct |
6 ms |
14680 KB |
Output is correct |
15 |
Correct |
6 ms |
14428 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14440 KB |
Output is correct |
20 |
Correct |
74 ms |
78676 KB |
Output is correct |
21 |
Correct |
6 ms |
14424 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
8 ms |
14544 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
7 ms |
14688 KB |
Output is correct |
26 |
Correct |
84 ms |
80316 KB |
Output is correct |
27 |
Correct |
78 ms |
80248 KB |
Output is correct |
28 |
Correct |
7 ms |
14940 KB |
Output is correct |
29 |
Correct |
10 ms |
15708 KB |
Output is correct |
30 |
Correct |
8 ms |
14944 KB |
Output is correct |
31 |
Correct |
8 ms |
15392 KB |
Output is correct |
32 |
Correct |
7 ms |
14968 KB |
Output is correct |
33 |
Correct |
11 ms |
16852 KB |
Output is correct |
34 |
Correct |
9 ms |
16856 KB |
Output is correct |
35 |
Correct |
18 ms |
18000 KB |
Output is correct |
36 |
Correct |
8 ms |
14940 KB |
Output is correct |
37 |
Correct |
24 ms |
21116 KB |
Output is correct |
38 |
Correct |
21 ms |
19984 KB |
Output is correct |
39 |
Correct |
20 ms |
20060 KB |
Output is correct |
40 |
Correct |
20 ms |
20056 KB |
Output is correct |
41 |
Correct |
19 ms |
19804 KB |
Output is correct |
42 |
Runtime error |
213 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14452 KB |
Output is correct |
4 |
Correct |
5 ms |
14424 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
6 ms |
14516 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
10 ms |
18776 KB |
Output is correct |
13 |
Correct |
10 ms |
18896 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
6 ms |
14604 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
15024 KB |
Output is correct |
18 |
Correct |
7 ms |
14540 KB |
Output is correct |
19 |
Correct |
6 ms |
14428 KB |
Output is correct |
20 |
Correct |
69 ms |
78820 KB |
Output is correct |
21 |
Correct |
6 ms |
14424 KB |
Output is correct |
22 |
Correct |
6 ms |
14684 KB |
Output is correct |
23 |
Correct |
6 ms |
14536 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
6 ms |
14684 KB |
Output is correct |
26 |
Correct |
74 ms |
80348 KB |
Output is correct |
27 |
Correct |
71 ms |
80280 KB |
Output is correct |
28 |
Correct |
7 ms |
15048 KB |
Output is correct |
29 |
Correct |
9 ms |
15708 KB |
Output is correct |
30 |
Correct |
8 ms |
14940 KB |
Output is correct |
31 |
Correct |
7 ms |
15192 KB |
Output is correct |
32 |
Correct |
7 ms |
15048 KB |
Output is correct |
33 |
Correct |
9 ms |
16852 KB |
Output is correct |
34 |
Correct |
10 ms |
17108 KB |
Output is correct |
35 |
Correct |
16 ms |
17952 KB |
Output is correct |
36 |
Correct |
9 ms |
14940 KB |
Output is correct |
37 |
Correct |
18 ms |
21084 KB |
Output is correct |
38 |
Correct |
21 ms |
20056 KB |
Output is correct |
39 |
Correct |
21 ms |
20060 KB |
Output is correct |
40 |
Correct |
20 ms |
20056 KB |
Output is correct |
41 |
Correct |
20 ms |
19800 KB |
Output is correct |
42 |
Runtime error |
225 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |