#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
#define B begin()
#define E end()
using namespace std;
int a[1001000];
int b[1001000]; int n,m;
vector<pair<int,int>>v[1001000];
vector<pair<int,int>>w[1000010];
vector<int>no[1000100];
int f(int i,int j,int l,int r,int T)
{
//cout<<" "<<i<<" "<<j<<" "<<l<<" "<<r<<endl;
if(T>m)
return 0;
if(j==a[2])
{
return 0;
}
int mn=1e17;
for(auto k:no[j])
{
if(k!=i)
mn=min(mn,f(k,j,0,0,T+1));
}
int g=0;
if(r==0){
for(int k=j+b[i];k<=n;k+=b[i])
{
g++;
mn=min(mn, f(i,k,1,0,T+1) )+g;
}
}
g=0;
if(l==0){
for(int k=j-b[i];k>=0;k-=b[i])
{
g++;
mn=min(mn,f(i,k,0,1,T+1) )+g;
}
}
return mn;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>a[i]>>b[i];
no[a[i]].pb(i);
v[i].pb({ a[i], 0 });
int g=0;
w[a[i]].pb( {i,0 } );
for(int j=a[i]-b[i];j>=0;j-=b[i])
{
g++;
w[j].pb({ i,g });
v[i].pb({ j,g });
}
g=0;
for(int j=a[i]+b[i];j<=n;j+=b[i])
{
g++;
w[j].pb({ i,g });
v[i].pb({ j,g });
}
sort(v[i].B,v[i].E);
}
/*
for(int i=1;i<=m;i++)
{
cout<<" "<<i<<" : ";
for(auto j:v[i])
cout<<"( "<<j.F<<", "<<j.S<<")"<<" ";
cout<<endl;
}
*/
cout<<f(1,a[1],0,0,0)<<endl;
}
//dp[i][j]= min jums to reach rock a[2] if the news in doge i and in rock j
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
70724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
70732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
70768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |