-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path3.cpp
58 lines (55 loc) · 1.01 KB
/
3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int N, A[500000], uwu[500000];
vector<int> pf[500005], pr[500005];
int prptr[500005];
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> N;
for(int i = 2; i <= N; i++){
if(pf[i].size()) continue;
for(int j = i; j <= N; j += i){
pf[j].push_back(i);
}
}
for(int i = 0; i < N; i++){
eat >> A[i];
for(int j : pf[A[i]]){
if(pr[j].size()){
uwu[i]++;
}
pr[j].push_back(i);
}
}
priority_queue<pair<int, int>> pq;
for(int i = 0; i < N; i++){
if(uwu[i] == 0){
pq.push({A[i], i});
}
}
vector<int> ans;
while(!pq.empty()){
int val = pq.top().first;
int i = pq.top().second;
pq.pop();
ans.push_back(val);
for(int j : pf[val]){
prptr[j]++;
if(prptr[j] == pr[j].size()) continue;
int k = pr[j][prptr[j]];
uwu[k]--;
if(uwu[k] == 0){
pq.push({A[k], k});
}
}
}
assert(ans.size() == N);
for(int i : ans){
moo << i << ' ';
}
moo << endl;
}