Codeforces Round #323 (Div. 2)C

来源:互联网 发布:怎么才能做好淘宝店 编辑:程序博客网 时间:2024/05/20 06:39

The GCD table G of size n × n for an array of positive integers a of length n is defined by formula

Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as . For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows:

Given all the numbers of the GCD table G, restore array a.

Input
The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — the elements of the GCD table of G for array a.

All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.

Output
In the single line print n positive integers — the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.

Examples
input
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2
output
4 3 6 2
input
1
42
output
42
input
2
1 1 1 1
output
1 1

思路就是先离散化
然后把%2余1的都找出来,这些一定是对的
然后把已经找出来的所有gcd标记掉
然后剩下的没找出来的都是偶数个的…
从大到小调俩标记掉gcd就可以

#include<iostream>#include<algorithm>#include<string>#include<map>#include<queue>using namespace std;int tu[250100];int st[250100],tt[250100];map<int, int>mp;int gcd(int a, int b){    if (b == 0) return a;    return gcd(b, a%b);}priority_queue<int>rr;int main(){    int n;    cin >> n;    int q;    for (int a = 1;a <= n*n;a++)    {        cin >> st[a];        if (!mp[st[a]])mp[st[a]] = mp.size(),rr.push(st[a]);        tu[mp[st[a]]]++;    }    int js = 0;    int qq = n*n;    for (int a = 1;a <= n*n;a++)    {        int qwq = mp[st[a]];        if (tu[qwq] % 2)        {            tu[qwq]--;            tt[++js] = st[a];        }    }    for (int a = 1;a <= js;a++)    {        for (int b = 1;b <= js;b++)        {            if (a == b)continue;            int qwq = gcd(tt[a], tt[b]);            int qeq = mp[qwq];            tu[qeq]--, qq--;        }    }    while (js != n)    {        if (rr.empty())break;        int a = rr.top();        while (tu[mp[a]] <2)        {            rr.pop();            a = rr.top();        }        int yy = js;        js += 2;        tt[yy + 1] = tt[yy + 2] = a;        for (int c = yy + 1;c <= js;c++)for (int d = 1;d <= yy;d++)tu[mp[gcd(tt[c], tt[d])]] -= 2;        tu[mp[gcd(tt[yy + 1], tt[yy + 1])]] -= 4;        if (js == n)break;    }    int jjx = 0;    sort(tt + 1, tt + js + 1);    for (int a = 1;a <= js;a++)    {        if (!jjx)        {            jjx = 1;            cout << tt[a];        }        else        {            cout << " " << tt[a];        }    }    return 0;}
0 0
原创粉丝点击