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;}
- Codeforces Round#323 div.2 C
- Codeforces Round #323 (Div. 2) C gcd
- Codeforces Round #323 (Div. 2)C
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Codeforces Round #323 (Div. 2)
- Codeforces Round #323 (Div. 2) C.GCD Table
- Codeforces Round #323 (Div. 2)C. GCD Table
- Codeforces Round #323 (Div. 2) C. GCD Table
- Codeforces Round #105 (Div. 2) C
- Codeforces Round 134 div 2 C题
- Codeforces Round #137 (Div. 2), problem: (C)
- Codeforces Round #153 (Div. 2) C题
- Codeforces Round #158 (Div. 2) C题
- Codeforces Round #162 (Div. 2) C
- Codeforces Round #166 (Div. 2) c. Secret
- Codeforces Round#170(Div 2)C
- Codeforces Round #173 (Div. 2) Problem C
- Codeforces Round #192 (Div. 2) C. Purification
- 机房重构——数据库设计
- 微信小程序Failed to load resource: net::ERR_NAME_NOT_RESOLVED问题解决办法
- Bzoj 1001: [BeiJing2006]狼抓兔子(最小割)
- 《Head First-设计模式》--装饰者模式C++实现
- IOS中图形图像处理第一部分:位图图像原图修改
- Codeforces Round #323 (Div. 2)C
- 删除链表中指定的所有元素
- 编程题#5:细菌实验分组 C语言
- mybatis 在if 里判断字符串
- Uncaught ReferenceError: Invalid left-hand side expression in postfix operation(…)
- 我哦都骑行路线
- 有一个排序二叉树,数据类型是int型,如何找出中间大的元素。
- git——分区管理
- DAO模式