#include<bits/stdc++.h>
#define ll int
#define ff first
#define ss second
using namespace std;
ll n,m,i,j,dist[30005],b,p,x,y,R;
vector<ll> v[30005];
bitset<30005> vis;
set<pair<ll,ll>> q;
int main(){
cin >> n >> m;
for(i=0;i<m;i++){
cin >> b >> p;
v[b].push_back(p);
if(i==0) q.insert({0,b});
if(i==1) R=b;
}
while(!q.empty()){
pair<ll,ll> z=*q.begin();
x=z.ss;
y=z.ff;
q.erase(q.begin());
for(ll i:v[x]){
p=0;
for(b=x+i;b<n;b+=i){
p++;
if(!vis[b]) q.insert({y+p,b});
vis[b]=1;
dist[b]=y+p;
}
p=0;
for(b=x-i;b>=0;b-=i){
p++;
if(!vis[b]) q.insert({y+p,b});
vis[b]=1;
dist[b]=y+p;
}
}
}
if(vis[R]) cout << dist[R];
else cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Incorrect |
1 ms |
944 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |