Codeforces Round #323 (Div. 2)C

给定一个正整数数组的GCD表格,需要恢复原数组。输入包含数组长度和GCD表格,通过离散化和GCD特性找出数组元素,输出满足条件的任意一种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值