#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN=30001;
const int mod=998244353;
const int mxlogN=40;
const int mxK=26;
const int inf=1e9;
const int K=600;
int vis[10000000];
bool done[10000000];
int a[mxN], b[mxN],n,m;
vector<int> have, who2[mxN];
vector<pair<int,int>> who[mxN];
int pos(int x)
{
return lower_bound(have.begin(),have.end(),x)-have.begin();
}
struct st
{
int x,p,i;
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i<m; i++) cin >> a[i] >> b[i];
for(int i=0; i<m; i++)
{
who[a[i]].push_back({a[i],b[i]});
who2[b[i]].push_back(a[i]%b[i]);
}
for(int i=1; i<mxN; i++)
{
sort(who2[i].begin(),who2[i].end());
who2[i].erase(unique(who2[i].begin(),who2[i].end()),who2[i].end());
for(int k=0; k*i<n; k++) for(int j:who2[i]) if(j+k*i<n) have.push_back(i*n+j+k*i);
}
for(int i=0; i<n; i++) for(auto& par:who[i]) par.first=pos(par.second*n+par.first);
deque<st> q; q.push_back({a[0],b[0],pos(b[0]*n+a[0])});
int sz=have.size();
for(int i=0; i<sz; i++) vis[i]=inf;
vis[pos(b[0]*n+a[0])]=0;
while(q.size())
{
auto cur=q.front(); q.pop_front();
int x=cur.x,p=cur.p,i=cur.i;
if(x==a[1])
{
cout << vis[i] << "\n";
return 0;
}
if(done[i]) continue;
done[i]=1;
while(who[x].size())
{
auto cur2=who[x].back(); who[x].pop_back();
int nw=cur2.first;
if(vis[i]<vis[nw])
{
vis[nw]=vis[i];
q.push_front({x,cur2.second,nw});
}
}
if(x-p>=0)
{
int nw=i-who2[p].size();
if(vis[i]+1<vis[nw])
{
vis[nw]=vis[i]+1;
q.push_back({x-p,p,nw});
}
}
if(x+p<n)
{
int nw=i+who2[p].size();
if(vis[i]+1<vis[nw])
{
vis[nw]=vis[i]+1;
q.push_back({x+p,p,nw});
}
}
}
cout << "-1" << "\n";
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1888 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
1 ms |
1876 KB |
Output is correct |
19 |
Correct |
1 ms |
1748 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
1 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
2 ms |
1876 KB |
Output is correct |
26 |
Correct |
2 ms |
1876 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
5 ms |
3020 KB |
Output is correct |
34 |
Correct |
4 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
1 ms |
1748 KB |
Output is correct |
20 |
Correct |
3 ms |
1748 KB |
Output is correct |
21 |
Correct |
1 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
2 ms |
1876 KB |
Output is correct |
26 |
Correct |
2 ms |
1876 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
2 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
5 ms |
2892 KB |
Output is correct |
34 |
Correct |
4 ms |
2892 KB |
Output is correct |
35 |
Correct |
12 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
2132 KB |
Output is correct |
37 |
Correct |
9 ms |
4040 KB |
Output is correct |
38 |
Correct |
15 ms |
4252 KB |
Output is correct |
39 |
Correct |
14 ms |
4052 KB |
Output is correct |
40 |
Correct |
13 ms |
4176 KB |
Output is correct |
41 |
Correct |
12 ms |
4176 KB |
Output is correct |
42 |
Correct |
6 ms |
2516 KB |
Output is correct |
43 |
Correct |
6 ms |
2516 KB |
Output is correct |
44 |
Correct |
5 ms |
2388 KB |
Output is correct |
45 |
Correct |
29 ms |
7028 KB |
Output is correct |
46 |
Correct |
20 ms |
7112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
1 ms |
1876 KB |
Output is correct |
19 |
Correct |
1 ms |
1748 KB |
Output is correct |
20 |
Correct |
1 ms |
1876 KB |
Output is correct |
21 |
Correct |
2 ms |
1860 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
4 ms |
2004 KB |
Output is correct |
25 |
Correct |
2 ms |
1876 KB |
Output is correct |
26 |
Correct |
3 ms |
1908 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
2 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2492 KB |
Output is correct |
30 |
Correct |
2 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
5 ms |
3020 KB |
Output is correct |
34 |
Correct |
4 ms |
2928 KB |
Output is correct |
35 |
Correct |
11 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
2132 KB |
Output is correct |
37 |
Correct |
9 ms |
4040 KB |
Output is correct |
38 |
Correct |
13 ms |
4300 KB |
Output is correct |
39 |
Correct |
12 ms |
4052 KB |
Output is correct |
40 |
Correct |
15 ms |
4176 KB |
Output is correct |
41 |
Correct |
13 ms |
4164 KB |
Output is correct |
42 |
Correct |
6 ms |
2516 KB |
Output is correct |
43 |
Correct |
7 ms |
2516 KB |
Output is correct |
44 |
Correct |
6 ms |
2440 KB |
Output is correct |
45 |
Correct |
24 ms |
7064 KB |
Output is correct |
46 |
Correct |
19 ms |
7036 KB |
Output is correct |
47 |
Correct |
21 ms |
9032 KB |
Output is correct |
48 |
Correct |
14 ms |
4820 KB |
Output is correct |
49 |
Correct |
11 ms |
4048 KB |
Output is correct |
50 |
Correct |
9 ms |
3784 KB |
Output is correct |
51 |
Correct |
21 ms |
6012 KB |
Output is correct |
52 |
Correct |
19 ms |
6336 KB |
Output is correct |
53 |
Correct |
13 ms |
3724 KB |
Output is correct |
54 |
Correct |
3 ms |
2132 KB |
Output is correct |
55 |
Correct |
3 ms |
2264 KB |
Output is correct |
56 |
Correct |
14 ms |
3412 KB |
Output is correct |
57 |
Correct |
6 ms |
5444 KB |
Output is correct |
58 |
Correct |
9 ms |
3044 KB |
Output is correct |
59 |
Correct |
12 ms |
3284 KB |
Output is correct |
60 |
Correct |
8 ms |
3532 KB |
Output is correct |
61 |
Correct |
8 ms |
3584 KB |
Output is correct |
62 |
Correct |
29 ms |
7564 KB |
Output is correct |
63 |
Correct |
102 ms |
22804 KB |
Output is correct |
64 |
Correct |
124 ms |
27416 KB |
Output is correct |
65 |
Correct |
158 ms |
38296 KB |
Output is correct |
66 |
Correct |
271 ms |
66376 KB |
Output is correct |
67 |
Correct |
171 ms |
64064 KB |
Output is correct |