Codeforces Round #323 (Div. 2) C. GCD Table map
题目链接:http://codeforces.com/contest/583/problem/C
2 seconds
256 megabytes
standard input
standard output
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.
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.
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.
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2
4 3 6 2
1
42
42
2
1 1 1 1
1 1
题意:你有n个数,给你n个数相互的gcd,即 gcd(a[i],a[j]) 1<=i<=n ,1<=j<=n;
让你求原来n个数 a[i],随意输出;
思路:首先,发现对称,好像没有什么用;
显然gcd只可能变小对吧,所以我们取最大的那个是必然存在的,放入a组数中,然后继续找下一个;
记得删掉取得数,和取数之间的gcd即可;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define mk make_pair
#define eps 1e-7
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e4+,M=4e6+,inf=;
const ll INF=1e18+,mod=; /// 数组大小
map<int,int>mp;
map<int,int>::iterator it;
vector<int>ans;
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n*n;i++)
{
int x;
scanf("%d",&x);
mp[x]++;
}
for(int i=;i<=n;i++)
{
it=--mp.end();
int z=it->first;
for(int j=;j<ans.size();j++)
{
int g=__gcd(z,ans[j]);
mp[g]--;
mp[g]--;
if(!mp[g])mp.erase(mp.find(g));
}
ans.push_back(z);
mp[z]--;
if(!mp[z])mp.erase(mp.find(z));
}
for(int i=;i<n;i++)
cout<<ans[i]<<" ";
return ;
}
Codeforces Round #323 (Div. 2) C. GCD Table map的更多相关文章
- 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 ...
- 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. 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 ...
- Codeforces Round #140 (Div. 1) D. The table 构造
D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...
- Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
随机推荐
- 通过反射,获取linkedHashMap的最后一个键值对。对map按照值进行排序。
1:通过反射,获取linkedHashMap的最后一个键值对. Map<Integer, Integer> map = new LinkedHashMap<>(); Field ...
- java客户端调用ftp上传下载文件
1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...
- 纯代码实现WordPress上传图片自动重命名的方法
在我们使用 WordPress 发布文章时,经常都需要添加图片.多媒体什么的.然而,大家都知道 WordPress 是舶来物,对于中文用户来说,我们都会把图片命名为中文的,由于 WordPress 机 ...
- Centos 集群配置SSH免登陆脚本
首先编写脚本生成集群服务器列表: hostsList.sh #!/bin/bash preIp="11.11.225." pwd="dyj2017" for i ...
- 置换检验(Permutation Test)学习[转载]
转自:https://www.cnblogs.com/bnuvincent/p/6813785.html http://www.bioinfo-scrounger.com/archives/564 1 ...
- list的*运算使用过程中遇到的问题
目的: 想生成一个[[],[],[]] 这样的列表, 所以就 [[]]*3 这样做了,但是这样做会有问题,这样list中的三个list其实是同一个list. 例如:a=[[]]*3,然后a[0].ap ...
- Qt QDataTime QString 两个类的使用
QDateTime now = QDateTime::currentDateTime(); QString nowStr; nowStr = now.toString("yyyyMMdd_h ...
- 网站建设中常用的JS代码段落
1.屏蔽左右键 这个不介绍了. <script language="JavaScript"> document.oncontextmenu=new Function(& ...
- MFC六大核心机制之五、六:消息映射和命令传递
作为C++程序员,我们总是希望自己程序的所有代码都是自己写出来的,如果使用了其他的一些库,也总是千方百计想弄清楚其中的类和函数的原理,否则就会感觉不踏实.所以,我们对于在进行MFC视窗程序设计时经常要 ...
- cda转MP3
首先启动Windows Media Player播放器(依次单击“开始”——“所有程序”——“附件”——“娱乐”——“Windows Media Player” 在工具栏切换到“翻录”选项,这 ...