#include <bits/stdc++.h>
using namespace std;
#define N 30005
#define ll long long int
#define sz(x) (int)x.size()
#define ff first
#define ss second
ll T, n, m, b[N], p[N], vis[N];
vector <int> v[N];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
vector <ll> a(n,1e9);
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
v[b[i]].push_back(i);
}
set <int> s;
a[b[0]] = 0;
for(auto i : v[b[0]]){
s.insert(i);
}
ll ans = 1e9;
while(sz(s) > 0){
ans = min(ans,a[b[1]]);
int x = *s.begin();
s.erase(s.begin());
for(int i = b[x]-p[x]; i >= 0; i -= p[x]){
a[i] = (a[i+p[x]] + 1);
if(vis[i] == 0){
vis[i] = 1;
for(auto j : v[i]){
s.insert(j);
}
}
}
for(int i = b[x]+p[x]; i < n; i += p[x]){
a[i] = (a[i-p[x]] + 1);
if(vis[i] == 0){
vis[i] = 1;
for(auto j : v[i]){
s.insert(j);
}
}
}
ans = min(ans,a[b[1]]);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
0 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
0 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1128 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
0 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |