//Sorry but iam targeting IOI :))
//NEVER LOSE HOPE
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#define endl "\n"
#define ceil(a,b) (a+(b-1))/b
#define all(v) v.begin(),v.end()
#define Hidden ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
const int N=3e4+10,mod = 1e9+7;
int n,m,f[N],dis[N];
pair<int,int>a[N];
vector<pair<int,int>>adj[N];
set<pair<int,int>>vis;
int main(){
cin>>n>>m;
for(int i=0;i<m;++i){
cin>>a[i].first>>a[i].second;
vis.insert({a[i].first,a[i].second});
}
for(auto i:vis){
for(int j=i.first+i.second;j<n;j+=i.second){
adj[i.first].push_back({j,((j-i.first)/i.second)});
if(vis.find({j,i.second})!=vis.end()) break;
}
}
for(auto i:vis){
for(int j=i.first-i.second;j>=0;j-=i.second){
adj[i.first].push_back({j,((i.first-j)/i.second)});
if(vis.find({j,i.second})!=vis.end()) break;
}
}
priority_queue<pair<int,int>>pq;
pq.push({0,a[0].first});
for(int i=0;i<N;++i) dis[i]=INT_MAX;
dis[a[0].first]=0;
while(!pq.empty()){
pair<int,int>p=pq.top();
pq.pop();
int u=p.second;
for(auto i:adj[u]){
int cost=dis[u]+i.second;
if(cost<dis[i.first]){
dis[i.first]=cost;
pq.push({-cost,i.first});
}
}
}
if(dis[a[1].first]==INT_MAX) cout<<"-1\n";
else cout<<dis[a[1].first]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
4 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1148 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1228 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
4 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
3 ms |
1228 KB |
Output is correct |
27 |
Correct |
3 ms |
1228 KB |
Output is correct |
28 |
Correct |
5 ms |
1440 KB |
Output is correct |
29 |
Correct |
4 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
3 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
2508 KB |
Output is correct |
34 |
Correct |
7 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1228 KB |
Output is correct |
19 |
Correct |
3 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1216 KB |
Output is correct |
21 |
Correct |
3 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
4 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
3 ms |
1228 KB |
Output is correct |
27 |
Correct |
3 ms |
1228 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
4 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
8 ms |
2508 KB |
Output is correct |
34 |
Correct |
7 ms |
2508 KB |
Output is correct |
35 |
Correct |
41 ms |
3540 KB |
Output is correct |
36 |
Correct |
7 ms |
1484 KB |
Output is correct |
37 |
Correct |
50 ms |
4176 KB |
Output is correct |
38 |
Correct |
58 ms |
4492 KB |
Output is correct |
39 |
Correct |
59 ms |
4544 KB |
Output is correct |
40 |
Correct |
60 ms |
4436 KB |
Output is correct |
41 |
Correct |
58 ms |
4420 KB |
Output is correct |
42 |
Correct |
16 ms |
1360 KB |
Output is correct |
43 |
Correct |
16 ms |
1444 KB |
Output is correct |
44 |
Correct |
19 ms |
1484 KB |
Output is correct |
45 |
Correct |
61 ms |
7252 KB |
Output is correct |
46 |
Correct |
48 ms |
7268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1392 KB |
Output is correct |
18 |
Correct |
3 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1228 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
3 ms |
1144 KB |
Output is correct |
27 |
Correct |
3 ms |
1228 KB |
Output is correct |
28 |
Correct |
6 ms |
1356 KB |
Output is correct |
29 |
Correct |
4 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
2508 KB |
Output is correct |
34 |
Correct |
7 ms |
2508 KB |
Output is correct |
35 |
Correct |
41 ms |
3524 KB |
Output is correct |
36 |
Correct |
8 ms |
1552 KB |
Output is correct |
37 |
Correct |
58 ms |
4156 KB |
Output is correct |
38 |
Correct |
58 ms |
4476 KB |
Output is correct |
39 |
Correct |
58 ms |
4556 KB |
Output is correct |
40 |
Correct |
65 ms |
4544 KB |
Output is correct |
41 |
Correct |
59 ms |
4444 KB |
Output is correct |
42 |
Correct |
16 ms |
1460 KB |
Output is correct |
43 |
Correct |
19 ms |
1452 KB |
Output is correct |
44 |
Correct |
17 ms |
1464 KB |
Output is correct |
45 |
Correct |
47 ms |
7192 KB |
Output is correct |
46 |
Correct |
48 ms |
7236 KB |
Output is correct |
47 |
Correct |
171 ms |
11848 KB |
Output is correct |
48 |
Correct |
61 ms |
4928 KB |
Output is correct |
49 |
Correct |
45 ms |
3900 KB |
Output is correct |
50 |
Correct |
35 ms |
3500 KB |
Output is correct |
51 |
Correct |
100 ms |
6952 KB |
Output is correct |
52 |
Correct |
106 ms |
7488 KB |
Output is correct |
53 |
Correct |
45 ms |
4680 KB |
Output is correct |
54 |
Correct |
3 ms |
1612 KB |
Output is correct |
55 |
Correct |
4 ms |
1992 KB |
Output is correct |
56 |
Correct |
32 ms |
3868 KB |
Output is correct |
57 |
Correct |
71 ms |
10400 KB |
Output is correct |
58 |
Correct |
16 ms |
2416 KB |
Output is correct |
59 |
Correct |
21 ms |
2868 KB |
Output is correct |
60 |
Correct |
24 ms |
3560 KB |
Output is correct |
61 |
Correct |
26 ms |
2940 KB |
Output is correct |
62 |
Correct |
104 ms |
8076 KB |
Output is correct |
63 |
Correct |
127 ms |
24344 KB |
Output is correct |
64 |
Correct |
119 ms |
27196 KB |
Output is correct |
65 |
Correct |
161 ms |
37184 KB |
Output is correct |
66 |
Correct |
324 ms |
64240 KB |
Output is correct |
67 |
Correct |
309 ms |
64404 KB |
Output is correct |