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. Qt编程'""hello world

    #include<QApplication>#include<QLabel>int main(int argc,char*argv[]){QApplicatin app(arg ...

  2. EasyUI-datagrid 对于展示数据进行处理(formatter)

    一:声明datagrid列,在列中添加formatter属性,并指定js方法 columns = [[ { title: '编号', field: 'Id', width: 100, sortable ...

  3. Win10无法安装提示磁盘布局不受UEFI固件支持怎样解决

    微软在推出Win10系统以后,就向Win7和Win8.1系统用户提供了免费升级Win10系统的推送,但是用户在安装Win10系统的时候,却有一部分用户反映,遇到提示“无法安装Windows,因为这台电 ...

  4. hdu 5596 GTW likes gt

    题目链接: hdu 5596 题意不难懂(虽然我还是看了好久)大概就是说 n 个人排成一列,分成两组, 第 i 秒时第 i 个人会消灭掉前面比他 b[i] 值低的且和他不同组的人,c[i] 表示第 c ...

  5. netperf安装及使用

    一.简介 Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf根据 ...

  6. windows server 2003(64位)上利用iis6部署32位应用

    如果直接部署,会出现如下问题: 试图加载格式不正确的程序. (Exception from HRESULT: 0x8007000B) 解决办法 1.命令行键入: cscript.exe %SYSTEM ...

  7. python的字符串内建函数

    http://www.ziqiangxuetang.com/python/python-strings.html

  8. 关于favicon.ico,shortcut icon,icon

    引入一篇文章.关于favicon.ico二三事. http://www.cnblogs.com/LoveJenny/archive/2012/05/22/2512683.html 一直对favicon ...

  9. 关于firstChild,firstElementChild和children

    <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementByTa ...

  10. java实现的类和表持久化

    //映射的过程: package com.ly.orm; import java.lang.reflect.Field; import java.util.ArrayList; import java ...