Submission #71435

#TimeUsernameProblemLanguageResultExecution timeMemory
71435someone_aaBrunhilda’s Birthday (BOI13_brunhilda)C++17
1.11 / 100
1088 ms263168 KiB
#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<maxq;i++) { if(prime[i]) { for(int j=i;j<maxq;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); } for(int i=0;i<q;i++) { cin>>qi[i]; maxq = max(maxq, qi[i] + 2); } solve(); sieve(); 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...