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. 254 shades of grey

    254 shades of grey Description: Why would we want to stop to only 50 shades of grey? Let's see to ho ...

  2. myhuiban会议,期刊,科研人员,计算机类会议大全

    http://www.myhuiban.com/ List of computer science conferences From Wikipedia, the free encyclopedia ...

  3. naotu.baidu.com 非常棒的脑图在线工具

    1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...

  4. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  5. UVa 1644 (筛素数 + 二分) Prime Gap

    题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...

  6. CSS布局中——导航是非常常见的

    导航绝对是页面布局中最常见的,为了不用每次去写,稍微贴个简单的导航模版出来,方便以后使用. <title>CSS菜单</title> <style type=" ...

  7. EF Code First 学习笔记:约定配置

    要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...

  8. 交易的成功 = 60%的资金管理 + 40%出入场信号 zt

    交易的成功 = 60%的资金管理 + 40%出入场信号. 资金管理   = 60%的风险分散 + 40%的适度重或轻仓. 出入场信号 = 60%的出场信号 + 40%的入场信号. 交易的成功 = 36 ...

  9. Print the numbers between 30 to 3000.

    Microsoft Interview Question Developer Program Engineers 看到一个题目比较有意思: Print the numbers between 30 t ...

  10. NBUT1457 Sona 莫队算法

    由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...