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== ? a : gcd(b, a%b);
} int ans[N]; int main(){
cin>>n;
int nn = n*n;
for(int i=; i<nn; ++i){
int x;
cin>>x;
mp[x]++;
}
int len = ;
for(map<int, int, greater<int> >::iterator it=mp.begin(); it!=mp.end();){
if(it->second == ){//不为0,说明这个数还是array中的数字
++it;
continue;
}
--it->second;
for(int i=; i<len; ++i){
int gcdn = gcd(it->first, ans[i]);
mp[gcdn]-=;
}
ans[len++] = it->first;
}
for(int i=; i<n; ++i){
if(i!=) cout<<" ";
cout<<ans[i];
}
cout<<endl;
return ;
}

Codeforces Round #323 (Div. 2) C.GCD Table的更多相关文章

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

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

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

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  3. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...

  5. Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心

    C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #323 (Div. 2)

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

  7. Codeforces Round #140 (Div. 1) D. The table 构造

    D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...

  8. 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 ...

  9. Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)

    转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...

随机推荐

  1. 第一章 DeepLab的创作动机

    这一段时间一直在做深度学习方面的研究,目前市场上的深度学习工具主要分为两大块.一块是基于Python语言的theano:另一块是可以在多个语言上使用并能够在GPU和CPU之间随意切换的Caffe.但是 ...

  2. 【枚举】POJ 3279

    直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...

  3. 纯CCS绘制三角形箭头图案

    用CSS绘制三角形箭头.使用纯CSS,你只需要很少的代码就可以创作出各种浏览器都兼容的三角形箭头! CSS代码: /* create an arrow that points up */ div.ar ...

  4. [IOS] 'Double' is not convertible to 'CGFloat'

    在做一个对象旋转的时候,要求转动的弧度角, 这个地方报错,如题的错误,其实是类型转换的问题,swift不能静静的做类型转换,一定要显式的转换 typeTableView?.transform=CGAf ...

  5. [Android] 时间Time Date 以及Location中gettime

    import android.text.format.Time; 还有一个是Date Location中的gettime, 这几个每个默认的格式都不一样,直接输出字符串各自得到了不同 比如按照获取当前 ...

  6. 第16周界面设计PSP总结

    计划:需1周完整完成 需求分析:作为一个观众,我希望能够了解每一场的比分结果,随时跟进比赛进程 生成设计文档:暂无 设计复审:暂无与组员进行设计复审 代码规范:Visual Studio2010 具体 ...

  7. 把token带到 http头部 或者验证一下referer

    提交地址:http://baozoumanhua.com/users/8311358提交数据:-----------------------------195704664324Content-Disp ...

  8. sass学习总结

    SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护.个人简单总结了下比较常用的的一些东西. $ 开头定义变量名 是个全局变量   在{ $ ...

  9. html 5 实现拖放效果

    在html5中要实现拖放操作,相对于以前通过鼠标操作实现,要简单得多,数据安全性也更有保障.只需要以下几步即可. 给被拖拽元素添加draggable属性,如果是文件拖放. 在拖拽元素的dragstar ...

  10. java 多线程 继承Thread和实现Runnable的区别

    1)继承Thread: public class ThreadTest extends Thread { private int count; private String name; public ...