# include <bits/stdc++.h>
using namespace std;
const int N = 30005;
int n, m, b[N], p[N], a[N], c[N], ans = 1e9;
int main(){
cin >> n >> m;
for(int i = 0; i < n; i ++)
a[i] = c[i] = 1e9;
for(int i = 1; i <= m; i ++){
cin >> b[i] >> p[i];
}
if(n > 10 || m > 3) return 0;
a[b[1]] = 0;
for(int i = b[1]; i + p[1] < n; i += p[1]){
a[i + p[1]] = a[i] + 1;
}
for(int i = b[1]; i - p[1] >= 0; i -= p[1]){
a[i - p[1]] = a[i] + 1;
}
if(m > 2){
c[b[3]] = 0;
for(int i = b[3]; i + p[3] < n; i += p[3]){
c[i + p[3]] = c[i] + 1;
}
for(int i = b[3]; i - p[3] >= 0; i -= p[3]){
c[i - p[3]] = c[i] + 1;
}
}
ans = a[b[2]];
if(m > 2)
if(c[b[2]] && a[b[3]]){
ans = min(ans, a[b[3]] + c[b[2]]);
}
if(ans == 1e9)
cout << -1 << endl;
else
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
568 KB |
Output is correct |
6 |
Correct |
2 ms |
748 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
748 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
3 |
Correct |
2 ms |
748 KB |
Output is correct |
4 |
Correct |
2 ms |
748 KB |
Output is correct |
5 |
Correct |
2 ms |
748 KB |
Output is correct |
6 |
Correct |
2 ms |
748 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Incorrect |
2 ms |
776 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
776 KB |
Output is correct |
2 |
Correct |
2 ms |
776 KB |
Output is correct |
3 |
Correct |
2 ms |
776 KB |
Output is correct |
4 |
Correct |
2 ms |
776 KB |
Output is correct |
5 |
Correct |
2 ms |
776 KB |
Output is correct |
6 |
Correct |
2 ms |
776 KB |
Output is correct |
7 |
Correct |
2 ms |
776 KB |
Output is correct |
8 |
Incorrect |
2 ms |
776 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
776 KB |
Output is correct |
2 |
Correct |
2 ms |
776 KB |
Output is correct |
3 |
Correct |
2 ms |
776 KB |
Output is correct |
4 |
Correct |
2 ms |
776 KB |
Output is correct |
5 |
Correct |
2 ms |
832 KB |
Output is correct |
6 |
Correct |
2 ms |
832 KB |
Output is correct |
7 |
Correct |
2 ms |
832 KB |
Output is correct |
8 |
Incorrect |
2 ms |
832 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
832 KB |
Output is correct |
2 |
Correct |
2 ms |
832 KB |
Output is correct |
3 |
Correct |
2 ms |
832 KB |
Output is correct |
4 |
Correct |
2 ms |
832 KB |
Output is correct |
5 |
Correct |
3 ms |
832 KB |
Output is correct |
6 |
Correct |
2 ms |
832 KB |
Output is correct |
7 |
Correct |
2 ms |
832 KB |
Output is correct |
8 |
Incorrect |
2 ms |
832 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |