A. GCD Table
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 

题目大意:给出你一个n,然后后边n*n个数,是n个数序列的gcd表的值(无序),输出这n个数

对角线上的元素就是ans[i],而且在所在行和列中最大,

首先可以确定的是最大的元素一定是ans[i]之一,这让人想到到了排序。

经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是ans[i]之一。

ac代码

93 ms 5576 KB
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
#define LL __int64
int cmp(LL a,LL b)
{
return a>b;
}
LL a[350500],ans[355000];
LL gcd(LL a,LL b)
{
int t;
if(a<b)
{
t=b;
b=a;
a=t;
}
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sz=n*n;
int i;
map<LL,LL>mp;
for(i=0;i<sz;i++)
scanf("%I64dd",&a[i]);
sort(a,a+sz,cmp);
int c=0,j=0;
ans[c++]=a[j++];
for(i=1;i<n;i++)
{
while(j<sz&&mp[a[j]])
{
mp[a[j]]--;
j++;
}
if(j==sz)
break;
for(int k=c-1;k>=0;k--)
mp[gcd(ans[k],a[j])]+=2;
ans[c++]=a[j++];
}
for(i=c-1;i>=0;i--)
{
if(i==c-1)
printf("%I64d",ans[i]);
else
printf(" %I64d",ans[i]);
}
}
}

  

Codeforces Round #323 (Div. 1) A. 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. 2) C.GCD Table

    C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...

  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. 浅谈C#Socket

    好不容易把socket通信搞懂一点,比较喜欢做笔记,嘿嘿~ 希望共同学习,共同进步! socket通信是C#中非常基础的一个知识点,我这里用到的是基于Tcp协议的socket通信.Tcp会有三次握手连 ...

  2. android持久化技术

    Android系统提供了3种持久化技术,所谓持久化技术是指将内存中的书籍保存在存储设备中. 1.文件存储 2.sharedPreference存储 3.数据库存储 除此之外,还可以将数据保存在SD卡中

  3. Python_Day6_Pyhotn常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  4. 行内js函数调用

    <ul> <li onclick=abc(this);><a href="javascript:void(0);">12234588</a ...

  5. Loading加载小插件,用户可以选择html格式或canvas格式,自定义loading图片,canvas色彩搭配可根据个人喜好

    ;(function($) { $.Loading = function(options) { //暴漏插件默认值 $.Loading.defaults = { speed: 200, //弹出层淡出 ...

  6. 关于malloc的一些想法

    之前一段时间翻阅过一些内存分配的资料,这次终于能将其整理记录下来了. c标准库里有两个耳熟能详的函数,用于对堆空间的内存进行分配和释放,它们分别是: malloc.负责分配一个指定大小的一块内存给调用 ...

  7. 可靠UDP

    tcp为我们做了什么事情? 总得来说,tcp做了这几件事: 通过序列号和基于确认的超时重传机制,为上层提供了可靠的字节流服务: 通过滑动窗口.拥塞窗口提供了流量控制: 默认情况下,为了有效利用带宽,t ...

  8. 腾讯云TCCE培训认证 精彩的第一次

    版权声明:本文由阁主的小跟班  原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/922888001482910380 来源: ...

  9. AES,RSA对称加密和非对称加密

    1.关于RSA加密机制:是非对称加密方式,两个钥,公钥和私钥,公钥用于加密数据,可以分享给其他用户,私钥可以用于解密用公钥加密的数据,关于安全问题是公钥泄露不会影响安全问题,公钥与私钥是一一对应的关系 ...

  10. SpringMVC之controller篇

    概述 继 spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...