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
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的
@)1%KBO0HM418$J94$1R.jpg)
代码:
#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 暴力的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)
对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...
- 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 ...
- 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 ...
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
D. Once Again... You a ...
- 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 ...
- Codeforces Round #323 (Div. 2)
被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...
随机推荐
- CentOS升级git
1.首先查看下当前的版本 [root@localhost ~]# git --versiongit version 1.8.2.1 2.尝试进行升级 [root@localhost ~]# yum u ...
- Hadoop课程介绍
一.课程简介 1. Hadoop是什么? Apache Hadoop是一款支持数据密集型分布式应用并以Apache 2.0许可协议发布的开源软件框架.它支持在商品硬件构建的大型集群上运行的应用程序.H ...
- centos 如何用 rsyslog 搭建本地日志服务
一.问题背景 最近项目遇到一个问题,服务器响应很慢,team中的两个有经验的工程师找了一台服务器分析了一下,发现问题出在磁盘写入过于频繁.这里大概介绍一下背景,我们的服务器上面主要是跑各种PHP接口, ...
- hdu 1166(树状数组 或 线段树)
线段树 (本题无需建树,少了很多) #include<cstdio> #include<cstring> int sum[5000005],rt,data,lb,rb,n,m; ...
- bzoj1857
三分的入门题,如果从AB上一点走到D是一个单峰函数从AB开始向CD传送带走的那个点也是一个单峰函数显然三分套三分 const eps=1e-5; var ax,ay,bx,by,cx,cy,dx,dy ...
- Java和.NET在开发中的不同盘点
我是用VS2008和VS2010开发.NET程序,通过MyEclipse8.5开发JAVA程序,下面从IDE.语言.插件的不同点来做下简单的说明.但由于经验知识还有限,本篇文章只能从比较表面的以及自己 ...
- 多线程程序设计学习(11)Two-phapse-Termination pattern
Two-phapse-Termination[A终止B线程] 一:Two-phapse-Termination的参与者--->A线程--->B线程 二:Two-phapse-Termina ...
- 嵌入式Linux USB WIFI驱动的移植
硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI: RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...
- ArcEngine查询、添加、删除要素的方法
原文 ArcEngine查询.添加.删除要素的方法 1.查找数据 1).利用FeaturCursor进行空间查询 //空间查询 ISpatialFilter spatialFilter = new S ...
- 【暑假】[深入动态规划]UVAlive 3983 Robotruck
UVAlive 3983 Robotruck 题目: Robotruck Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format ...