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. Linux下LVM

    http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/22/3093405.html

  2. Python_Day6_Pyhotn常用模块学习

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

  3. struts 标签的使用<转>

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...

  4. Ajax些成绩批量录入

    1.jsp,ajax的循环调用,必须要递归,否则会出错. <%@ page language="java" import="java.util.*" pa ...

  5. PHP控制div块大小和颜色的例子

    网站为了设计的更好看,会有很多的样式,而用php来控制样式很常见,无聊写了一个可以用于列表展示的样式,不喜忽喷. 1.先添加一个style样式控制div默认不换行 <style>div{f ...

  6. Yii读取TXT文件数据插入到数据库

    个人平时会用到的方法,记录一下并分享给需要的朋友,Yii批量添加还需要clone一下model才可以,不然只会插入一条数据. 也可以把文件通过参数的方式调入到方法中. // 读取CVS文件 funct ...

  7. Spring——jar包详解(转)

    Spring——jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spr ...

  8. python解析robot framework的output.xml,并生成html

    一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...

  9. Windows Store App 全球化:运行时响应语言变更

    在应用程序运行过程中,系统的语言.像素.对比度等系统设置可能会发生改变,应用程序应根据系统环境的改变及时做出适当的响应.为了解决这样的问题,可以在应用程序中为系统状态更改事件注册事件处理方法,当语言. ...

  10. SVN使用教程

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...