Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心
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 x and 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*n的 gcd表格
现在乱序给你这n*n的 gcd表格 要求你输出这n个数
题解:因为两个数字的最大公因数一定小于这两个数,所以n*n中最大的两个数字一定在主对角线上,然后排除掉他们的最大公因数后找剩余数字最大的数字,继续排除已知数字的最大公因数,直到找出n个数字为止。这种构造方法的有效性可以证明贪心策略的正确性,注意去掉已知的数字的最大公因数每次-2
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
#define ll __int64
#define mod 1e9+7
#define PI acos(-1.0)
using namespace std;
int n;
int a[];
map<int,int>mp;
vector<int> ve;
int gcd(int aa,int bb)
{
if(bb==)
return aa;
else
return gcd(bb,aa%bb);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n*n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
}
sort(a+,a+n*n+);
for(int i=n*n;i;i--)
{
if(!mp[a[i]])
continue;
mp[a[i]]--;
for(int j=;j<ve.size();j++)
mp[gcd(ve[j],a[i])]-=;
ve.push_back(a[i]);
}
for(int i=;i<=n-;i++)
cout<<ve[i]<<" ";
cout<<endl;
}
Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心的更多相关文章
- Codeforces Round #691 (Div. 2) C. Row GCD (数学)
题意:给你两个数组\(a\)和\(b\),对于\(j=1,...,m\),找出\(a_1+b_j,...,a_n+b_j\)的\(gcd\). 题解:我们很容易的得出\(gcd\)的一个性质:\(gc ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 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 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 #554 (Div. 2)-C(gcd应用)
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...
- Codeforces Round #347 (Div.2)_A. Complicated GCD
题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...
随机推荐
- OO终章
一,第四单元架构设计 第一次作业:只有类图 1,重置MyClass,MyOperation等类,为使里面只有必要数据(name,id,visibility等).或方便组织数据(如MyClass作为其底 ...
- IBM MQ安装
一.下载MQ 可以去官方网站下载,我这次下了一个下载器从官方,然后通过下载器进行MQ的下载. 地址:https://www.ibm.com/developerworks/cn/downloads/ws ...
- Java基础面试题:super.getClass().getName() 执行结果是什么?
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句
package com.swift; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStrea ...
- SummerVocation_Learning--java的多线程实现
java的线程是通过java.lang.Thread类来实现的. 可以通过创建Thread的实例来创建新的线程. 每个线程都是通过某个特定Thread对象所对应的方法run()来完成操作,方法run( ...
- 洛谷P1164小A点菜
这也是一道01背包的题 用的方法比较的巧妙.这个动态规划相当于反过来做的,自己理解就知道了.代码很短 #include<bits/stdc++.h> using namespace std ...
- mysql -u root -p 解释
使用此命令首先确保你的mysql运行环境已经搭建好 这是客户端连接mysql服务器的指令,比较全的写法是下面两种 第一个是全拼,第二个是第一个的缩写 mysql --host=localhost -- ...
- ZendFramework-2.4 源代码 - 关于配置
$applicationConfig = $serviceManager->setService('ApplicationConfig'); // 获取配置 /data/www/www.doma ...
- [转载]Failed to read session data On PHP 7.1
从php5.6升级php7.1,报错 Warning: session_start(): Failed to read session data: user (path: ) Warning: ses ...
- PHP 作用域