Codeforces Round #323 (Div. 1) A. GCD Table
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*n个数,是n个数序列的gcd表的值(无序),输出这n个数
对角线上的元素就是ans[i],而且在所在行和列中最大,
首先可以确定的是最大的元素一定是ans[i]之一,这让人想到到了排序。
经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是ans[i]之一。
ac代码
93 ms | 5576 KB |
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
#define LL __int64
int cmp(LL a,LL b)
{
return a>b;
}
LL a[350500],ans[355000];
LL gcd(LL a,LL b)
{
int t;
if(a<b)
{
t=b;
b=a;
a=t;
}
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sz=n*n;
int i;
map<LL,LL>mp;
for(i=0;i<sz;i++)
scanf("%I64dd",&a[i]);
sort(a,a+sz,cmp);
int c=0,j=0;
ans[c++]=a[j++];
for(i=1;i<n;i++)
{
while(j<sz&&mp[a[j]])
{
mp[a[j]]--;
j++;
}
if(j==sz)
break;
for(int k=c-1;k>=0;k--)
mp[gcd(ans[k],a[j])]+=2;
ans[c++]=a[j++];
}
for(i=c-1;i>=0;i--)
{
if(i==c-1)
printf("%I64d",ans[i]);
else
printf(" %I64d",ans[i]);
}
}
}
Codeforces Round #323 (Div. 1) A. GCD Table的更多相关文章
- 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 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. 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 ...
随机推荐
- myeclipse连接oracle步骤
1.加载ojdbc.jar驱动(路径:E:\myoracle\oracle\product\11.2.0\dbhome_1\jdbc\lib) 2.String url = "jdbc:or ...
- GCD 开发
一.简介 GCD 的全称是 Grand Centre Dispatch 是一个强大的任务编程管理工具.通过GCD你可以同步或者异步地执行block.function. 二.dispatch Queue ...
- PHP 知识结构
- 大数据运算模型 MapReduce 原理
大数据运算模型 MapReduce 原理 2016-01-24 杜亦舒 MapReduce 是一个大数据集合的并行运算模型,由google提出,现在流行的hadoop中也使用了MapReduce作为计 ...
- C++编程小知识随手记
C++编程小知识点: (1)queue和vector类型: 加入元素 : queue是queue.push(),vector是vector.push_back(), 删除元素: queue是queue ...
- mysql授权
grant all privileges on testdb to userA@'%' 然而通过userA登录查看存储过程时仍然无权限查看,通过jdbc连接程序也报错, 之前mysql在windows ...
- 设置git 不提交 修改权限的文件
vim .git/config 打开文件
- css 表格
1.给元素的display属性添加为以下值 table : <table> table-caption :<caption> table-cell : <td> t ...
- JAVA09异常处理之动手动脑问题
动手动脑1:为什么不管是否有异常发生,finally语句块中的语句始终保证被执行? 我们在写代码时,如果finally块中的代码过多会导致字节码条数"膨胀",因为finally中的 ...
- mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存
增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...