CF582A 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个数之间任意两个数的gcd,求这n个数分别是多少.
分析:我们要从这个表中得到一些原数列的信息,显然,最大的那个数肯定是原数列中的数,然后把这个数去掉,并且将这个数和之前得到的数列中的数的gcd去掉,因为是一个矩形,所以一定会出现两次gcd,重复这种操作就能得到原数列中的数.
至于实现,最好用map、hash,如果一个一个找数会浪费很多时间.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map> using namespace std; map <int, int> m; int a[ * ],n,tot,ans[*]; bool cmp(int x, int y)
{
return x > y;
} int gcd(int a, int b)
{
if (!b)
return a;
return gcd(b, a % b);
} int main()
{
scanf("%d", &n);
for (int i = ; i <= n * n; i++)
{
scanf("%d", &a[i]);
m[a[i]]++;
}
sort(a + , a + n * n + ,cmp);
for (int i = ; i <= n * n; i++)
{
if (!m[a[i]])
continue;
m[a[i]]--;
for (int j = ; j <= tot; j++)
m[gcd(a[i], a[j])] -= ;
ans[++tot] = a[i];
}
for (int i = ; i <= tot; i++)
printf("%d ", ans[i]); return ;
}
CF582A GCD Table的更多相关文章
- 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 暴力
C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...
- SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)
4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...
- 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 ...
- SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版
SPOJ4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the result ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- CF 338 D GCD Table(CRT)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...
- Codeforces 338D GCD Table 中国剩余定理
主题链接:点击打开链接 特定n*m矩阵,[i,j]分值为gcd(i,j) 给定一个k长的序列,问能否匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n &am ...
随机推荐
- Intellij IDEA 2017 控制台打印换行
Intellij IDEA 2017 控制台打印的内容超过屏幕宽度了,请问怎么自动换行? 记得重启idea
- Linux常用的20个命令
以下为20个命令 1.ls命令:ls命令式列出目录内容(List Directory Contents)的意思.运行它就是列出文件夹里面的内容,可能是文件也可能是文件夹. root@tecmint:~ ...
- CodeForces 714A
Description Today an outstanding event is going to happen in the forest — hedgehog Filya will come t ...
- 【Linux 命令】iftop安装与简单使用
iftop是linux下的一个流量监控工具,用于查看实时网络流量,反向解析IP,显示端口信息官网:http://www.ex-parrot.com/~pdw/iftop/ 1.安装必须软件包 yum ...
- 网络流最大流(拆点)(附带kuangbin最大流模板)(目测这个题有bug)
题目链接:https://vjudge.net/contest/68128#problem/H 我觉得这个题有bug,如果饮料和食物都为0,但是同时有五个人什么也不需要,按道理来讲,最多受益的人数为5 ...
- Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中
以下代码均非IDE开发,所以都不规范,仅供参考 模拟Servlet本质: 模拟Servlet接口: /* SUN公司制定的JavaEE规范:Servlet规范 Servlet接口是Servlet规范中 ...
- Spring4笔记12--SSH整合3--Spring与Struts2整合
SSH 框架整合技术: 3. Spring与Struts2整合(对比SpringWeb): Spring 与 Struts2 整合的目的有两个: (1)在 Struts2 的 Action 中,即 V ...
- springboot集成mybatis环境搭建以及实现快速开发微服务商品模块基本的增删改查!
之前学习了springboot和mybatis3的一些新特性,初步体会了springboot的强大(真的好快,,,,,),最近趁着复习,参考着以前学习的教程,动手写了一个springboot实战的小例 ...
- 洛谷 P4093: bzoj 4553: [HEOI2016/TJOI2016]序列
题目传送门:洛谷P4093. 题意简述: 给定一个长度为 \(n\) 的序列 \(a\). 同时这个序列还可能发生变化,每一种变化 \((x_i,y_i)\) 对应着 \(a_{x_i}\) 可能变成 ...
- MongoDB(3.6.3)的用户认证初识
Windows 10家庭中文版,MongoDB 3.6.3, 前言 刚刚安装好了MongoDB,启动了服务器-mongod命令,启动了MongoDB shell-mongo命令,不过,全程都没有使用u ...