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

时间:2022-12-29 07:59:20

C. GCD Table

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/583/problem/C

Description

The GCD table G of size n × n for an array of positive integers a of length n is defined by formula

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

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 Codeforces Round #323 (Div. 2) C. GCD Table 暴力. For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows:

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

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 Input

4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2

Sample Output

4 3 6 2

HINT

题意

由n个数,可以构成n*n的gcd矩阵(如图

给你n*n个数,表示这个n*n矩阵的元素(是任意顺序的

然后让你找到这n个数,满足这个gcd矩阵

题解:

首先,我们可以分析出几个结论

1.最大的数,一定是答案

2.出现次数为奇数的,一定是答案         //然而这个条件并没有什么用……

做法,我们就选择最大的数,然后让这个数和前面取到的数,都会产生两个gcd,于是就在数列中把这两个gcd删去就好了

注意,每两个数之间,一定会产生两个gcd的

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

代码:

#include<iostream>
#include<stdio.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std; #define maxn 620 int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int a[maxn*maxn];
map<int,int> H;
vector<int> ans;
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n*n;i++)
{
scanf("%d",&a[i]);
H[a[i]]++;
}
sort(a+,a+n*n+);
for(int i=n*n;i;i--)
{
if(!H[a[i]])
continue;
H[a[i]]--;
for(int j=;j<ans.size();j++)
H[gcd(ans[j],a[i])]-=;
ans.push_back(a[i]);
}
for(int i=;i<=n-;i++)
cout<<ans[i]<<" ";
cout<<endl;
}

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

  1. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; C&period; GCD Table map

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

  2. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; C&period;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 ...

  3. Codeforces Round &num;323 &lpar;Div&period; 1&rpar; A&period; GCD Table

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

  4. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; C GCD Table 582A (贪心)

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

  5. Codeforces Round &num;323 &lpar;Div&period; 1&rpar; B&period; Once Again&period;&period;&period; 暴力

    B. Once Again... Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/582/probl ...

  6. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; B 贪心,暴力

    B. Robot's Task time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; D&period; Once Again&period;&period;&period; 暴力&plus;最长非递减子序列

                                                                                  D. Once Again... You a ...

  8. Codeforces Round &num;323 &lpar;Div&period; 2&rpar; C 无敌gcd 数学&sol;贪心

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

  9. Codeforces Round &num;323 &lpar;Div&period; 2&rpar;

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

随机推荐

  1. C&num;中out和ref之间的区别【转】

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  2. SRM 146 DIV1 800

    Problem Statement      The purpose of a roundabout is to control the flow of traffic at a busy inter ...

  3. windows 装 Crypto&period;Cipher

    python 2.7 先去官网下载,https://www.dlitz.net/software/pycrypto/ 回来装这个,http://download.csdn.net/download/d ...

  4. Eclipse 反编译器

    Help-->Install New SoftWare 贴上反编译地址:http://opensource.cpupk.com/decompiler/update/ 选择add,一路向北,起飞.

  5. jenkins服务器安装

    http://www.360doc.com/content/13/0412/09/10504424_277718090.shtml

  6. python之路-pip安装

    pip类似RedHat里面的yum,安装Python包非常方便   安装pip方法: 1.安装环境:ubuntu-14.04.2 sudo apt-get install python-pip pyt ...

  7. enode框架step by step之框架的物理部署思路

    enode框架step by step之框架的物理部署思路   enode框架系列step by step文章系列索引: enode框架step by step之开篇 enode框架step by s ...

  8. MySQL Group Replication 动态添加成员节点

    前提: MySQL GR 3节点(node1.node2.node3)部署成功,模式定为多主模式,单主模式也是一样的处理. 在线修改已有GR节点配置 分别登陆node1.node2.node3,执行以 ...

  9. java继承与覆写小练习

    最近学习java到了继承的部分,写个小程序用以巩固. import java.util.Scanner;//导入输入包public class testfather { public static v ...

  10. Unity的UI究竟为什么可以合批

    1.UI/Default代码研究首先,我想到的是,既然是对图集纹理进行采样,而且又不能统一更改材质的纹理UV值,我们通常写的shader都是直接根据模型UV值对主纹理进行采样,那会不会是shader中 ...