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 ...
随机推荐
- Spark将计算结果写入到Mysql中
今天主要来谈谈如何将Spark计算的结果写入到Mysql或者其他的关系型数据库里面.其实方式也很简单,代码如下: package scala import java.sql.{DriverManage ...
- ML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language?),这篇课程的目的是展示出UML ...
- Qt setMargin()和setSpacing() 的含义
mainLayout=newQVBoxLayout(this); mainLayout->setMargin(30); //表示控件与窗体的左右边距 mainLayout->setSpac ...
- Javascript-闰年javascript的判断
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql事务详解
事务的四大特性ACID如下: 原子性:事务中的所有操作,要么全部完成,要么不做任何操作,不能只做部分操作.如果在执行的过程中发了错误,要回滚(Rollback)到事务开始前的状态,就像这个 ...
- python学习第一次
一,安装python 官网下载地址:https://www.python.org/downloads/windows/ 我下载的是最新的: Download Windows x86 web-based ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON BitXor
zw版[转发·台湾nvp系列Delphi例程]HALCON BitXor procedure TForm1.Button1Click(Sender: TObject);var image0, imag ...
- 浅谈EM算法的两个理解角度
http://blog.csdn.net/xmu_jupiter/article/details/50936177 最近在写毕业论文,由于EM算法在我的研究方向中经常用到,所以把相关的资料又拿出来看了 ...
- 公司里面用的iTextSharp(教程)---简介
一.需求: 公司这次要做一个生成PDF的功能,需要设计,刚刚进入公司,组长把任务分配给了我,为了完成这个任务,苦学了许久的iTextSharp.现在记录下实现过程中了了解的一些东东,一起分享哈~~ 二 ...
- pycharm中内看内建函数的定义
鼠标方法在内建函数上,Ctrl+B,看内建函数的定义 如果想要看内置函数的具体实现细节,可以到python的lib目录下C:\Python27\Lib\,或者python的官网上 如果要看非内建的函数 ...