CF1176D Recover it

#include <bits/stdc++.h> using namespace std; const int maxn=3e6+5; typedef long long ll; int lst[maxn]; int prim[maxn]; int cnt[maxn]; void sieve(){ for(int i=0;i<maxn;i++) lst[i]=i; for(int i=2;i<maxn;i++){ if(lst[i]!=i){ lst[i]=i/lst[i]; continue; } for(ll j=i*1ll*i;j<maxn;j+=i) lst[j]=min(lst[j],i); } int cur=0; for(int i=2;i<maxn;i++) if(lst[i]==i) prim[i]=++cur; } int main(){ //freopen("datain.txt","r",stdin); ios::sync_with_stdio(0); cin.tie(0); int n;cin>>n; for(int i=0;i<2*n;i++){ int x; cin>>x; cnt[x]++; } sieve(); vector<int> res; for(int i=maxn-1;i>=0;i--){ while(cnt[i]>0){ if(lst[i]==i){ cnt[prim[i]]--; res.push_back(prim[i]); } else{ cnt[lst[i]]--; res.push_back(i); } cnt[i]--; } } for(auto v:res) cout<<v<<' '; cout<<endl; }

猜你喜欢

转载自www.cnblogs.com/hanker99/p/11012998.html