#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,m;
cin>>n>>m;
pii a[m];
vector<pair<int,bool>> adj[n*m+n];
for(int i=0;i<m;i++){
int b,p;
cin>>b>>p;
a[i]={b,p};
for(int x=b%p;x<n;x++){
adj[n*i+x].pb({n*m+x,0});
}
adj[n*m+b].pb({n*i+b,0});
for(int x=b%p;x+p<n;x++){
adj[n*i+x].pb({n*i+x+p,1});
adj[n*i+x+p].pb({n*i+x,1});
}
}
int dist[n*m+n];
for(int i=0;i<n*m+n;i++)dist[i]=1e9;
dist[a[0].fs]=0;
deque<int> dq;
dq.push_back(a[0].fs);
while(!dq.empty()){
int v=dq.front();
dq.pop_front();
for(auto[u,w]:adj[v]){
if(dist[v]+w<dist[u]){
dist[u]=dist[v]+w;
if(w)dq.push_back(u);
else dq.push_front(u);
}
}
}
int ans=dist[n+a[1].fs];
if(ans==1e9)ans=-1;
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
404 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
13 ms |
11356 KB |
Output is correct |
12 |
Correct |
28 ms |
15180 KB |
Output is correct |
13 |
Correct |
29 ms |
15192 KB |
Output is correct |
14 |
Correct |
13 ms |
11356 KB |
Output is correct |
15 |
Correct |
14 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
15 ms |
11184 KB |
Output is correct |
12 |
Correct |
26 ms |
15292 KB |
Output is correct |
13 |
Correct |
25 ms |
15196 KB |
Output is correct |
14 |
Correct |
12 ms |
11356 KB |
Output is correct |
15 |
Correct |
13 ms |
11240 KB |
Output is correct |
16 |
Correct |
11 ms |
9560 KB |
Output is correct |
17 |
Correct |
103 ms |
78936 KB |
Output is correct |
18 |
Correct |
102 ms |
92688 KB |
Output is correct |
19 |
Correct |
72 ms |
62200 KB |
Output is correct |
20 |
Runtime error |
397 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
13 ms |
11248 KB |
Output is correct |
12 |
Correct |
23 ms |
15196 KB |
Output is correct |
13 |
Correct |
27 ms |
15184 KB |
Output is correct |
14 |
Correct |
14 ms |
11160 KB |
Output is correct |
15 |
Correct |
14 ms |
11352 KB |
Output is correct |
16 |
Correct |
11 ms |
9676 KB |
Output is correct |
17 |
Correct |
104 ms |
78932 KB |
Output is correct |
18 |
Correct |
109 ms |
92884 KB |
Output is correct |
19 |
Correct |
69 ms |
61988 KB |
Output is correct |
20 |
Runtime error |
381 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
13 ms |
11356 KB |
Output is correct |
12 |
Correct |
25 ms |
15192 KB |
Output is correct |
13 |
Correct |
24 ms |
15196 KB |
Output is correct |
14 |
Correct |
12 ms |
11352 KB |
Output is correct |
15 |
Correct |
14 ms |
11232 KB |
Output is correct |
16 |
Correct |
11 ms |
9560 KB |
Output is correct |
17 |
Correct |
108 ms |
78932 KB |
Output is correct |
18 |
Correct |
95 ms |
92752 KB |
Output is correct |
19 |
Correct |
71 ms |
61992 KB |
Output is correct |
20 |
Runtime error |
419 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |