# 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 = 1; i <= m; i ++){
cin >> b[i] >> p[i];
}
if(m == 2) while(1){};
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){
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;
}
}
if(a[b[2]])
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
428 KB |
Output is correct |
2 |
Execution timed out |
1085 ms |
428 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Execution timed out |
1073 ms |
480 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
532 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
532 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
532 KB |
Output is correct |
2 |
Execution timed out |
1080 ms |
532 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |