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>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxu = 10000100;
const int maxn = 100100;
bool prime[maxu], exist[maxu];
int primes[maxn], qi[maxn], dp[maxu], n, m, q;
int ind[maxn], tree[4*maxn], maxq, inf, maxx;
vector<int>p[maxu];
void sieve() {
memset(prime,true,sizeof(prime));
prime[0] = prime[1] = false;
for(int i=2;i<maxx;i++) {
if(prime[i]) {
for(int j=i;j<maxx;j+=i) {
if(exist[i])
p[j].pb(ind[i]);
if(j > i) prime[j] = false;
}
}
}
}
void update(int x, int val, int li=0, int ri=n-1, int index=1) {
if(li==ri) tree[index] = val;
else {
int mid = (li+ri)/2;
if(x <= mid) update(x,val,li,mid,2*index);
else if(x>mid) update(x,val,mid+1,ri,2*index+1);
tree[index] = min(tree[2*index], tree[2*index+1]);
}
}
void solve() {
inf = 1000000000;
for(int i=1;i<=maxq;i++) {
for(int j:p[i]) {
update(j, inf);
}
dp[i] = tree[1] + 1;
for(int j:p[i]) {
update(j, dp[i]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=0;i<n;i++) {
cin>>primes[i];
exist[primes[i]] = true;
ind[primes[i]] = i;
maxx = max(maxx, primes[i] + 2);
}
sieve();
for(int i=0;i<q;i++) {
cin>>qi[i];
maxq = max(maxq, qi[i]);
}
solve();
for(int i=0;i<q;i++) {
if(dp[qi[i]] >= inf) cout<<"oo\n";
else cout<<dp[qi[i]]<<"\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |