#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define td(v) v.begin(),v.end()
#define sz(v) (int)v.size()
//#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define right sefudermano
typedef long long ll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
if(b==0LL) return 1LL;
if(b==1LL) return mod(a,m);
ll k = mod(exp(a,b/2,m),m);
if(b&1LL){
return mod(a*mod(k*k,m),m);
}
else return mod(k*k,m);
}
int n,m;
const int N = 3e4 + 10;
int b[N],p[N];
const int sq = 200;
int mark[N];
int dist[N][sq + 3];
vi aqui[N];
int mn[N];
const int inf = 1e9;
int rev[N];
vector<pii> pq[N*sq];
#define pwr(x) (x>sq ? sq + 1 : x)
int32_t main(){
fastio;
cin >> n >> m;
for(int i=0;i<m;i++){
cin >> b[i] >> p[i];
aqui[b[i]].pb(i);
}
for(int i=0;i<N;i++){
mn[i] = inf;
for(int j=0;j<sq+2;j++)dist[i][j] = inf;
}
dist[b[0]][pwr(p[0])] =0;
mn[b[0]] = 0;
pq[0].pb(pii(b[0],0));
for(int D=0;!pq[D].empty();D++){
while(!pq[D].empty()){
pii c = pq[D].back();
pq[D].pop_back();
int d = D;
int cur = c.ff;
if(mn[cur] > d)mn[cur] = d;
if(p[c.ss] <= sq){
aqui[cur].pb(c.ss);
}
for(int id : aqui[cur]){
int x = p[id];
if(x > sq){
if(dist[cur][sq+1]==inf)dist[cur][sq+1] = d;
if(mark[id])continue;
mark[id] = 1;
int t=0;
for(int l = cur-x;l>=0;l-=x){
t++;
pq[d+t].pb(pii(l,id));
}
t=0;
for(int l = cur + x;l<n;l+=x){
t++;
pq[d+t].pb(pii(l,id));
}
}else{
dist[cur][x] = min(dist[cur][x],d);
if(cur - x >=0 and dist[cur-x][x] == inf){
dist[cur-x][x] = d + 1;
pq[d+1].pb(pii(cur - x,id));
}
if(cur + x < n and dist[cur + x][x] == inf){
dist[cur + x][x] = d + 1;
pq[d+1].pb(pii(cur + x,id));
}
}
}
aqui[cur].clear();
}
}
int res = mn[b[1]];
for(int i=1;i<=sq+1;i++)res = min(res,dist[b[1]][i]);
if(res == inf)cout<<-1<<endl;
else cout << res << endl;
// Math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
165884 KB |
Output is correct |
2 |
Correct |
106 ms |
166008 KB |
Output is correct |
3 |
Correct |
114 ms |
165964 KB |
Output is correct |
4 |
Correct |
103 ms |
165880 KB |
Output is correct |
5 |
Correct |
104 ms |
165880 KB |
Output is correct |
6 |
Correct |
106 ms |
165880 KB |
Output is correct |
7 |
Correct |
106 ms |
166008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
166008 KB |
Output is correct |
2 |
Correct |
115 ms |
166008 KB |
Output is correct |
3 |
Correct |
104 ms |
165880 KB |
Output is correct |
4 |
Correct |
104 ms |
165880 KB |
Output is correct |
5 |
Correct |
110 ms |
166008 KB |
Output is correct |
6 |
Correct |
105 ms |
165880 KB |
Output is correct |
7 |
Correct |
110 ms |
166104 KB |
Output is correct |
8 |
Correct |
112 ms |
166012 KB |
Output is correct |
9 |
Correct |
117 ms |
166008 KB |
Output is correct |
10 |
Correct |
108 ms |
165928 KB |
Output is correct |
11 |
Correct |
106 ms |
166008 KB |
Output is correct |
12 |
Correct |
105 ms |
166008 KB |
Output is correct |
13 |
Correct |
103 ms |
166008 KB |
Output is correct |
14 |
Correct |
104 ms |
166008 KB |
Output is correct |
15 |
Correct |
108 ms |
165988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
166008 KB |
Output is correct |
2 |
Correct |
104 ms |
165880 KB |
Output is correct |
3 |
Correct |
105 ms |
165880 KB |
Output is correct |
4 |
Correct |
119 ms |
165880 KB |
Output is correct |
5 |
Correct |
110 ms |
166112 KB |
Output is correct |
6 |
Correct |
103 ms |
166008 KB |
Output is correct |
7 |
Correct |
104 ms |
166008 KB |
Output is correct |
8 |
Correct |
104 ms |
166008 KB |
Output is correct |
9 |
Correct |
117 ms |
166064 KB |
Output is correct |
10 |
Correct |
102 ms |
166008 KB |
Output is correct |
11 |
Correct |
109 ms |
166008 KB |
Output is correct |
12 |
Correct |
105 ms |
166008 KB |
Output is correct |
13 |
Correct |
102 ms |
166008 KB |
Output is correct |
14 |
Correct |
111 ms |
165968 KB |
Output is correct |
15 |
Correct |
104 ms |
166008 KB |
Output is correct |
16 |
Correct |
105 ms |
166008 KB |
Output is correct |
17 |
Correct |
107 ms |
166136 KB |
Output is correct |
18 |
Correct |
104 ms |
166008 KB |
Output is correct |
19 |
Correct |
106 ms |
166008 KB |
Output is correct |
20 |
Correct |
105 ms |
166136 KB |
Output is correct |
21 |
Correct |
104 ms |
166008 KB |
Output is correct |
22 |
Correct |
104 ms |
166008 KB |
Output is correct |
23 |
Correct |
117 ms |
166144 KB |
Output is correct |
24 |
Correct |
105 ms |
166136 KB |
Output is correct |
25 |
Correct |
105 ms |
166136 KB |
Output is correct |
26 |
Correct |
103 ms |
166268 KB |
Output is correct |
27 |
Correct |
111 ms |
166364 KB |
Output is correct |
28 |
Correct |
106 ms |
166612 KB |
Output is correct |
29 |
Correct |
109 ms |
166904 KB |
Output is correct |
30 |
Correct |
104 ms |
166264 KB |
Output is correct |
31 |
Correct |
116 ms |
166520 KB |
Output is correct |
32 |
Correct |
104 ms |
166268 KB |
Output is correct |
33 |
Correct |
114 ms |
167160 KB |
Output is correct |
34 |
Correct |
118 ms |
167208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
166136 KB |
Output is correct |
2 |
Correct |
116 ms |
166120 KB |
Output is correct |
3 |
Correct |
102 ms |
166008 KB |
Output is correct |
4 |
Correct |
104 ms |
165880 KB |
Output is correct |
5 |
Correct |
113 ms |
166008 KB |
Output is correct |
6 |
Correct |
116 ms |
166008 KB |
Output is correct |
7 |
Correct |
102 ms |
165880 KB |
Output is correct |
8 |
Correct |
104 ms |
166008 KB |
Output is correct |
9 |
Correct |
102 ms |
166008 KB |
Output is correct |
10 |
Correct |
115 ms |
166008 KB |
Output is correct |
11 |
Correct |
121 ms |
166008 KB |
Output is correct |
12 |
Correct |
109 ms |
166008 KB |
Output is correct |
13 |
Correct |
104 ms |
166008 KB |
Output is correct |
14 |
Correct |
106 ms |
166008 KB |
Output is correct |
15 |
Correct |
102 ms |
166008 KB |
Output is correct |
16 |
Correct |
104 ms |
166008 KB |
Output is correct |
17 |
Correct |
106 ms |
166136 KB |
Output is correct |
18 |
Correct |
103 ms |
166008 KB |
Output is correct |
19 |
Correct |
105 ms |
166008 KB |
Output is correct |
20 |
Correct |
113 ms |
166136 KB |
Output is correct |
21 |
Correct |
103 ms |
166008 KB |
Output is correct |
22 |
Correct |
103 ms |
166008 KB |
Output is correct |
23 |
Correct |
103 ms |
166120 KB |
Output is correct |
24 |
Correct |
106 ms |
166136 KB |
Output is correct |
25 |
Correct |
106 ms |
166136 KB |
Output is correct |
26 |
Correct |
115 ms |
166264 KB |
Output is correct |
27 |
Correct |
118 ms |
166264 KB |
Output is correct |
28 |
Correct |
121 ms |
166776 KB |
Output is correct |
29 |
Correct |
127 ms |
166780 KB |
Output is correct |
30 |
Correct |
109 ms |
166264 KB |
Output is correct |
31 |
Correct |
106 ms |
166392 KB |
Output is correct |
32 |
Correct |
117 ms |
166264 KB |
Output is correct |
33 |
Correct |
115 ms |
167160 KB |
Output is correct |
34 |
Correct |
109 ms |
167160 KB |
Output is correct |
35 |
Correct |
116 ms |
167288 KB |
Output is correct |
36 |
Correct |
119 ms |
166240 KB |
Output is correct |
37 |
Correct |
141 ms |
167796 KB |
Output is correct |
38 |
Correct |
131 ms |
167796 KB |
Output is correct |
39 |
Correct |
138 ms |
167668 KB |
Output is correct |
40 |
Correct |
130 ms |
167740 KB |
Output is correct |
41 |
Correct |
121 ms |
167800 KB |
Output is correct |
42 |
Correct |
108 ms |
166524 KB |
Output is correct |
43 |
Correct |
111 ms |
166520 KB |
Output is correct |
44 |
Correct |
110 ms |
166520 KB |
Output is correct |
45 |
Correct |
145 ms |
170548 KB |
Output is correct |
46 |
Correct |
129 ms |
170356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
165940 KB |
Output is correct |
2 |
Correct |
106 ms |
165880 KB |
Output is correct |
3 |
Correct |
106 ms |
165880 KB |
Output is correct |
4 |
Correct |
111 ms |
165932 KB |
Output is correct |
5 |
Correct |
102 ms |
165880 KB |
Output is correct |
6 |
Correct |
122 ms |
166008 KB |
Output is correct |
7 |
Correct |
103 ms |
166008 KB |
Output is correct |
8 |
Correct |
105 ms |
166008 KB |
Output is correct |
9 |
Correct |
120 ms |
165880 KB |
Output is correct |
10 |
Correct |
102 ms |
166008 KB |
Output is correct |
11 |
Correct |
104 ms |
166008 KB |
Output is correct |
12 |
Correct |
114 ms |
166008 KB |
Output is correct |
13 |
Correct |
117 ms |
166008 KB |
Output is correct |
14 |
Correct |
104 ms |
166008 KB |
Output is correct |
15 |
Correct |
112 ms |
166136 KB |
Output is correct |
16 |
Correct |
115 ms |
166008 KB |
Output is correct |
17 |
Correct |
104 ms |
166136 KB |
Output is correct |
18 |
Correct |
103 ms |
166008 KB |
Output is correct |
19 |
Correct |
109 ms |
166008 KB |
Output is correct |
20 |
Correct |
104 ms |
166136 KB |
Output is correct |
21 |
Correct |
105 ms |
166008 KB |
Output is correct |
22 |
Correct |
104 ms |
166008 KB |
Output is correct |
23 |
Correct |
103 ms |
166136 KB |
Output is correct |
24 |
Correct |
107 ms |
166136 KB |
Output is correct |
25 |
Correct |
108 ms |
166136 KB |
Output is correct |
26 |
Correct |
107 ms |
166264 KB |
Output is correct |
27 |
Correct |
110 ms |
166136 KB |
Output is correct |
28 |
Correct |
110 ms |
166648 KB |
Output is correct |
29 |
Correct |
110 ms |
166776 KB |
Output is correct |
30 |
Correct |
105 ms |
166264 KB |
Output is correct |
31 |
Correct |
110 ms |
166392 KB |
Output is correct |
32 |
Correct |
107 ms |
166264 KB |
Output is correct |
33 |
Correct |
113 ms |
167160 KB |
Output is correct |
34 |
Correct |
111 ms |
167160 KB |
Output is correct |
35 |
Correct |
112 ms |
167160 KB |
Output is correct |
36 |
Correct |
109 ms |
166136 KB |
Output is correct |
37 |
Correct |
117 ms |
167796 KB |
Output is correct |
38 |
Correct |
117 ms |
167796 KB |
Output is correct |
39 |
Correct |
117 ms |
167668 KB |
Output is correct |
40 |
Correct |
134 ms |
167776 KB |
Output is correct |
41 |
Correct |
122 ms |
167800 KB |
Output is correct |
42 |
Correct |
121 ms |
166520 KB |
Output is correct |
43 |
Correct |
122 ms |
166520 KB |
Output is correct |
44 |
Correct |
108 ms |
166392 KB |
Output is correct |
45 |
Correct |
131 ms |
170420 KB |
Output is correct |
46 |
Correct |
135 ms |
170484 KB |
Output is correct |
47 |
Correct |
164 ms |
173412 KB |
Output is correct |
48 |
Correct |
113 ms |
166648 KB |
Output is correct |
49 |
Correct |
113 ms |
166520 KB |
Output is correct |
50 |
Correct |
106 ms |
166392 KB |
Output is correct |
51 |
Correct |
143 ms |
170592 KB |
Output is correct |
52 |
Correct |
151 ms |
170744 KB |
Output is correct |
53 |
Correct |
140 ms |
168440 KB |
Output is correct |
54 |
Correct |
118 ms |
166952 KB |
Output is correct |
55 |
Correct |
113 ms |
167416 KB |
Output is correct |
56 |
Correct |
128 ms |
168056 KB |
Output is correct |
57 |
Correct |
152 ms |
171640 KB |
Output is correct |
58 |
Correct |
125 ms |
169208 KB |
Output is correct |
59 |
Correct |
121 ms |
170048 KB |
Output is correct |
60 |
Correct |
144 ms |
170188 KB |
Output is correct |
61 |
Correct |
136 ms |
170104 KB |
Output is correct |
62 |
Correct |
178 ms |
181496 KB |
Output is correct |
63 |
Correct |
197 ms |
187948 KB |
Output is correct |
64 |
Correct |
211 ms |
192176 KB |
Output is correct |
65 |
Correct |
320 ms |
202404 KB |
Output is correct |
66 |
Correct |
716 ms |
229492 KB |
Output is correct |
67 |
Correct |
672 ms |
229108 KB |
Output is correct |