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

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 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的

代码:

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

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

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

  4. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

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

  5. Codeforces Round #323 (Div. 1) B. Once Again... 暴力

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

  6. Codeforces Round #323 (Div. 2) B 贪心,暴力

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

  7. Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

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

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

  9. Codeforces Round #323 (Div. 2)

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

随机推荐

  1. Monitor vs WaitHandle

    http://stackoverflow.com/questions/1355398/monitor-vs-waithandle-based-thread-sync A problem with Mo ...

  2. OneNote快捷键

    转载自:http://onenoter.com/2013/04/5792 记录笔记和设置笔记格式 键入和编辑笔记 若要执行此操作 按 打开一个新的 OneNote 窗口. Ctrl+M 打开一个小的 ...

  3. UVa 120 (构造) Stacks of Flapjacks

    这题求解的过程和选择排序非常相似. 反转的过程中分为无序(在前面)和有序(在后面)两个部分,一开始视为全部为无序. 在无序部分中找到最大的元素,先把它翻到最前面,然后再反转到无序部分的最后面.这样该元 ...

  4. Scrum 体验活动笔记

    2014-03-10  Isoftstone library 1.识别角色(用户),形象 :名称.痛处.属性.需求 2.编写故事 story以验证需求是否正确:我们假设(客户)  进行验证结果... ...

  5. HTML5桌面通知:notification api

    1. 为什么需要HTML5的桌面通知 传统的桌面通知可以写一个div放到页面右下角自动弹出来,并通过轮询等等其他方式去获取消息并推送给用户.这种方式有个弊端就是:当我在使用京东 进行购物的时候,我是不 ...

  6. Entity Framework 并发处理

    什么是并发? 并发分悲观并发和乐观并发. 悲观并发:比如有两个用户A,B,同时登录系统修改一个文档,如果A先进入修改,则系统会把该文档锁住,B就没办法打开了,只有等A修改完,完全退出的时候B才能进入修 ...

  7. ASP.NET MVC 开篇

    MVC(Model-View-Controller,模型—视图—控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Controller) ...

  8. 在.net中用Connection对象数据源的架构信息

    可得到数据库中的,表,视图,等信息   string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + fi ...

  9. NGINX(五)模块

    nginx模块分为以下几类: NGX_CORE_MODULE //核心模块 NGX_HTTP_MODULE //HTTP处理模块 NGX_EVENT_MODULE //事件处理模块 NGX_MAIL_ ...

  10. HDU5692 Snacks DFS+线段树

    分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时 只会影响的子树的和,最优值也是子树最大的值 #include <cstdio> #include < ...