This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MX = (int)1e7;
int n, q, sieve[MX + 1], dp[MX + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for(int i = 0;i < n;i ++) {
int x; cin >> x;
for(int j = x;j <= MX;j += x) {
sieve[j] = x;
}
sieve[0] = x;
}
int ptr = 0;
for(int i = 1;i <= MX;i ++) {
while(ptr < i && (ptr + sieve[ptr] <= i || dp[ptr] == INT_MAX)) ptr ++;
if(ptr == i) dp[i] = INT_MAX;
else dp[i] = dp[ptr] + 1;
}
while(q --) {
int x;
cin >> x;
if(dp[x] == INT_MAX) {
cout << "oo\n";
} else {
cout << dp[x] << '\n';
}
}
return 0;
}
// OUTPUT in new line.
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |