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 ...
随机推荐
- asp mvc 路由
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Workflo ...
- [scu 4423] Necklace
4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...
- 以CTE表达式实现MSSQL的字符串分割函数
ALTER FUNCTION [dbo].[Split] (@sep varchar(2), @s varchar(512))RETURNS tableASRETURN ( WITH P ...
- centos 6.5 32位 编译安装Mysql
groupadd mysql #添加mysql组 useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统 ...
- 你真的精通Java吗?
简历和自我介绍上经常能够读到“精通Java”这样的话,有人和我说,精通Java的人太多了,精通Java已经不能算亮点.不能给自己加分了.可是事实真是这样吗? 对于语言的学习,我有一种观点,一是纵向,即 ...
- Javscript高级
Javscript高级: 函数内部属性 arguments: arguments(实参:包含所有传入方法的参数)特殊用法: arguments中有一个属性callee,是一个指针,指向拥有这个argu ...
- vi中如何去除高亮显示的单词
在vi命令行模式下输入:set nohlsearch 单词不高亮显示:set hlsearch 单词高亮显示: 如果再次打开仍然显示,则可以随便搜索一个不存在的单词,然后保存,重新打开高亮不在显示:
- [原创]使用命令行工具提升cocos2d-x开发效率(二)之CocosBuilder篇
如果你正在使用CocosBuilder或者是其他基于CocosBuilder源码改装而成的工具为你的游戏搭建场景或者UI,那你一定要看看这篇文章:) 你是否已经厌倦了无聊的手工publish操作? ...
- python网络编程(六)---web客户端访问
1.获取web页面 urllib2 支持任何协议的工作---不仅仅是http,还包括FTP,Gopher. import urllib2 req=urllib2.Request('http://www ...
- Storm因机器断电等,启动supervisor异常
Storm因机器断电等,启动supervisor错误 因机器断电或其他异常导致的supervisor意外终止,再次启动时报错: 2014-08-13 10:36:03 b.s.event [ERROR ...