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.

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.

Sample test(s)
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 

思路:

  设数列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;

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
#include<algorithm>
#define N 505
using namespace std;int n;
map<int, int, greater<int> >mp;//key按照由大到小排序 int gcd(int a, int b){return b==0 ? a : gcd(b, a%b);
}int ans[N];int main(){cin>>n;int nn = n*n;for(int i=0; i<nn; ++i){int x;cin>>x;mp[x]++;}int len = 0;for(map<int, int, greater<int> >::iterator it=mp.begin(); it!=mp.end();){if(it->second == 0){//不为0,说明这个数还是array中的数字++it;continue;    }--it->second;for(int i=0; i<len; ++i){int gcdn = gcd(it->first, ans[i]);mp[gcdn]-=2;}ans[len++] = it->first;}    for(int i=0; i<n; ++i){if(i!=0) cout<<" ";cout<<ans[i];}cout<<endl;return 0;
}

Codeforces Round #323 (Div. 2) C.GCD Table相关推荐

  1. Codeforces Round #323 (Div. 2) C. GCD Table

    C. GCD Table 题目链接: http://codeforces.com/contest/583/problem/C 题意是给你n个数,可以构成n*n的GCD矩阵,矩阵里的元素顺序可以是任意的 ...

  2. Codeforces Round #323 (Div. 2): C. GCD Table(思维题)

    题意: 给你一个长度为n的序列a[1]~a[n], 之后用这个序列生成一个n*n的矩阵,其中矩阵第i行第i列的值为a[i],第i行第j列(j!=i)的值为Gcd(a[i], a[j]),现在给你一个矩 ...

  3. Codeforces Round #323 (Div. 2)

    被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...

  4. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

  5. Codeforces Round #256 (Div. 2) D Multiplication Table

    Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the mu ...

  6. Codeforces Round #323 (Div. 1) B. Once Again... 暴力

    B. Once Again... Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/582/probl ...

  7. Codeforces Round #705 (Div. 2) D. GCD of an Array 质因子分解 + stl维护

    传送门 题意: 给nnn个数,让后qqq个操作,每次选择一个位置的数使其乘xxx,每次询问后输出所有数的gcdgcdgcd. 思路: 这个题思路算是比较好想的.首先就是一个计算方式即计算所有数的gcd ...

  8. 重复T次的LIS的dp Codeforces Round #323 (Div. 2) D

    http://codeforces.com/contest/583/problem/D 原题:You are given an array of positive integers a1, a2, . ...

  9. Codeforces Round #323 (Div. 2) A. Asphalting Roads

    A. Asphalting Roads time limit per test 1 second memory limit per test 256 megabytes input standard ...

最新文章

  1. 移动开发框架,第【二】弹:Hammer.js 移动设备触摸手势js库
  2. All in one TFS 项目建立
  3. markdown输出为pdf没有图片怎么办?
  4. Oracle 创建 split 和 splitstr 函数
  5. 鸿蒙tv系统安装,运行第一个鸿蒙tv应用
  6. 软件项目组织管理(九)项目人力资源管理
  7. 功能Java示例 第7部分–将失败也视为数据
  8. 第一百三十五期:如何模拟一次阿里双11秒杀场景的实现?程序员必看
  9. 编译速度谁“最快”?25岁的 C++Builder 还能打
  10. 印章管控私有化部署,助力政企办公开启新纪元
  11. CFA要学哪些课程?零基础怎么学CFA呢?
  12. PRINCE2认证好在哪?
  13. C++深入学习笔记—基础篇—个人银行账户管理程序
  14. 云流化是什么?对软件企业有什么好处?
  15. Markdown语法大全
  16. 【python numpy库的使用】向量的加减法
  17. 温暖的奢侈,当手机遇到机器人
  18. 隐函数存在定理3的证明Jacobi_20160503
  19. 【Oracle Hint】Oracle Hint学习笔记【一】
  20. UE4中蓝图实现输入框输入关键字搜索对应东西

热门文章

  1. 用框架的你,可能早已忽略了这些事件API
  2. Python学习笔记__10.4章 进程VS线程
  3. Zabbix 3.0 安装
  4. linux下vtune使用
  5. Linux操作系统基础解析之(五)——grep命令家族及正则表达式初识
  6. code point,code unit
  7. android:versionCode和android:versionName 用途
  8. Linux 释放缓存的方法
  9. .net知识和学习方法系列(十四)TraceListener的应用
  10. python 切片_全面解读Python高级特性切片