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.

Examples
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个数之间任意两个数的gcd,求这n个数分别是多少.

分析:我们要从这个表中得到一些原数列的信息,显然,最大的那个数肯定是原数列中的数,然后把这个数去掉,并且将这个数和之前得到的数列中的数的gcd去掉,因为是一个矩形,所以一定会出现两次gcd,重复这种操作就能得到原数列中的数.

至于实现,最好用map、hash,如果一个一个找数会浪费很多时间.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map> using namespace std; map <int, int> m; int a[ * ],n,tot,ans[*]; bool cmp(int x, int y)
{
return x > y;
} int gcd(int a, int b)
{
if (!b)
return a;
return gcd(b, a % b);
} int main()
{
scanf("%d", &n);
for (int i = ; i <= n * n; i++)
{
scanf("%d", &a[i]);
m[a[i]]++;
}
sort(a + , a + n * n + ,cmp);
for (int i = ; i <= n * n; i++)
{
if (!m[a[i]])
continue;
m[a[i]]--;
for (int j = ; j <= tot; j++)
m[gcd(a[i], a[j])] -= ;
ans[++tot] = a[i];
}
for (int i = ; i <= tot; i++)
printf("%d ", ans[i]); return ;
}

CF582A GCD Table的更多相关文章

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

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

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

  4. SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)

    4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...

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

  6. SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1&lt;=a&lt;=n,1&lt;=b&lt;=m))加强版

    SPOJ4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the result ...

  7. codeforces 582A. GCD Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...

  8. CF 338 D GCD Table(CRT)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...

  9. Codeforces 338D GCD Table 中国剩余定理

    主题链接:点击打开链接 特定n*m矩阵,[i,j]分值为gcd(i,j) 给定一个k长的序列,问能否匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n &am ...

随机推荐

  1. [整理]Node入门 » 一本全面的Node.js教程 - Demo实践所遇到的问题

    花了一个上午看完[转载]Node入门 » 一本全面的Node.js教程 根据里面的Demo自己手动实现过程中还是遇到了些问题,特整理在此. <1>.由于node.msi安装包已经自动添加了 ...

  2. [转载]查询json数据结构的8种方式

    http://wangxinghaoaccp.blog.163.com/blog/static/1158102362012111812255980/ 你有没有对“在复杂的JSON数据结构中查找匹配内容 ...

  3. Python核心编程——Chapter15

    正则表达式在脚本语言里是最重要的一部分,这部分的题目真的不容怠慢. 开始这部分的题目的解答! 15.1识别下列字符串:bat,bit,but,hat,hit和hut. >>> imp ...

  4. ASP.NET中的另类控件

    首先看一个aspx文件里的部分内容: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  5. C 语言中指针初始化为字符串常量 不可通过该指针修改其内容

    char b[] = "hello"; 则“hello”存于栈中,因为定义的是一个数组. char *b = "hello"; 则"hello&quo ...

  6. C++中的二级指针和指针引用函数传参

    在函数的使用过程中,我们都明白传值和传引用会使实参的值发生改变.那么能够通过传指针改变指针所指向的地址吗? 在解决这个问题之前,也许我们应该先了解指针非常容易混淆的三个属性: ①.指针变量地址(&am ...

  7. 二次开发中cad字体的总结

    目前手头一个项目,关于制图统一平台的,特别研究了CAD中的字体,总结出来,给需要的朋友,希望少走弯路.1.cad2008中,netload之后,输入注册的命令,提示未知命令解决:将引用中CAD两个dl ...

  8. 新电脑重新安装win10+python3.6+anaconda+tensorflow1.12(gpu版)

    安装了一天的软件,遇到了很多坑,在快泪崩的时候,始终以磨刀不误砍柴工鼓励自己,坚持安好了,话不多说,上干货: 前言:        TensorFlow 有两个版本:CPU 版本和 GPU 版本.GP ...

  9. GridView Postback后出错Operation is not valid due to the current state of the object.

    一.问题起因 最近项目中有一页面第一次search后正常,但是再次点击其它任何按钮都会报错,亦即postback后页面有问题,经检查是由于页面有一GridView且数据量极大,记录大概有上千条,这儿解 ...

  10. mysql 增加字段脚本,以及删除主键约束的脚本,存储过程

    //增加一个库下面所有表的row_id和其他9个字段的存过 DELIMITER $$ USE `erptest`$$ DROP PROCEDURE IF EXISTS `UPTABLE`$$ CREA ...