cf 1176D - Recover it!
https://codeforces.com/problemset/problem/1176/D
sort,从大到小遍历判
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10, M = 3e6+10;
bool vis[M];
int p[M], cntp;
int d[M];
int rk[M];
int n, a[M];
int st[M];
int ans[M];
void sieve(int n) {
vis[0] = vis[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) {
p[cntp++] = i;
d[i] = i;
}
for(int j = 0; j < cntp && i <= n / p[j]; j++) {
vis[i * p[j]] = 1;
d[i * p[j]] = min(d[i], p[j]);
if(i % p[j] == 0) break;
}
}
for(int i = 0; i < cntp; i++)
rk[p[i]] = i + 1;
}
int main() {
sieve(M-1);
scanf("%d", &n);
for(int i = 0; i < n + n; i++)
scanf("%d", &a[i]), st[a[i]]++;
sort(a, a+n+n);
int cnt = 0;
for(int i = n+n-1; i >= 0; i--) {
if(!st[a[i]]) continue;
if(vis[a[i]]) {
int dd = a[i] / d[a[i]];
ans[cnt++] = a[i];
st[dd]--;
}
else {
int dd = rk[a[i]];
st[dd] --;
ans[cnt++] = dd;
}
st[a[i]] --;
}
for(int i = 0; i < n; i++) printf("%d ", ans[i]); puts("");
return 0;
}