Codeforces Round #323 (Div. 2) C.GCD Table
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?xand?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.
InputThe 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.
OutputIn 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.
Sample test(s) input 42 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
思路:
設數列X: a11, a12,...., ann;
由于gcd(a,b)<=min(a,b);
ans[N]存放已經選中的數,即array中一定存在的數;?
首先從X中找到最大的一個值aij,然后對ans[N]中的每一個數,得到g = gcd(aij, ans[i]),?
由于table矩陣是對稱的,所以從X中刪除2個值為 g 的數值!
最后將aij放入ans中!不斷重復此過程,知道ans中數字個數為n;
總結
以上是生活随笔為你收集整理的Codeforces Round #323 (Div. 2) C.GCD Table的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zabbix2.2升级到zabbix3.
- 下一篇: js调用高德API获取所在当前城市