【Codeforces Round 323 (Div 2)C】【观察找规律 STL map】GCD Table 从GCD矩阵中找出所有原始元素

本文主要是介绍【Codeforces Round 323 (Div 2)C】【观察找规律 STL map】GCD Table 从GCD矩阵中找出所有原始元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C. GCD Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

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.

Sample test(s)
input
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2
output
4 3 6 2
input
1
42
output
42 
input
2
1 1 1 1
output
1 1 


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=505,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int a[N][N];
int b[N*N];
int n,nn;
map<int,int>mop;
map<int,int>::iterator it;
int gcd(int x,int y)
{return y==0?x:gcd(y,x%y);
}
int main()
{while(~scanf("%d",&n)){mop.clear();for(int i=n*n;i;--i){scanf("%d",&b[i]);++mop[b[i]];}int p=0;map<int,int>::iterator tmp;for(it=mop.begin();it!=mop.end();it++)if(it->second&1){++p;a[p][p]=it->first;--it->second;for(int i=1;i<p;i++){int x=gcd(a[i][i],a[p][p]);a[i][p]=a[p][i]=x;mop[x]-=2;}}while(p<n){while(1){it=--mop.end();if(it->second==0)mop.erase(it);else break;}int x=it->first;++p;a[p][p]=x;--mop[x];for(int i=1;i<p;i++){int x=gcd(a[i][i],a[p][p]);a[i][p]=a[p][i]=x;mop[x]-=2;}}for(int i=1;i<=n;i++)printf("%d ",a[i][i]);puts("");}return 0;
}
/*
【trick&&吐槽】
做题没思路,从样例下手。【题意】
给你n([1,500])个数a[],b[i][j]表示gcd(a[i],a[j])
b[][]有n*n个数,我们把它打乱顺序,全部告诉你。
让你输出所有的a[]。【类型】
贪心 构造【分析】
这题很有趣。
一开始想很难想,完全不知道思考的方向。
这时候我们要怎么办?没错!观察样例!
我们发现,这个矩阵肯定是关于主对角线对称的。
于是我们先找到出现次数为奇数的数,这些数一定是位于主对角线上的,同时对应着一个a[]。
然后我们同时生成所有的gcd。
然而这样并不一定能把这个矩阵完全还原。
剩下的数的出现次数都可能为偶数。
这时我们发现,数值最大的数(还没被取),肯定在矩阵的主对角线。于是就放上去。
重复这个操作,就可以AC这道题啦!【时间复杂度&&优化】
O(n^2logn)【数据】
4
4 4 4 4 2 2 2 2
2 2 2 2 2 2 2 2*/


这篇关于【Codeforces Round 323 (Div 2)C】【观察找规律 STL map】GCD Table 从GCD矩阵中找出所有原始元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610496

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列