博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】
阅读量:7249 次
发布时间:2019-06-29

本文共 2933 字,大约阅读时间需要 9 分钟。

C. Marco and GCD Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.

When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every1 ≤ i ≤ j ≤ n and put it into a set Sgcd here means the .

Note that even if a number is put into the set S twice or more, it only appears once in the set.

Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

Input

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S.

The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.

Output

If there is no solution, print a single line containing -1.

Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.

In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence.

We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106.

If there are multiple solutions, print any of them.

Examples
input
4 2 4 6 12
output
3 4 6 12
input
2 2 3
output
-1
Note

In the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.

【题意】:给你S数列。让你再构造一个数列,使得该数列内gcd(ai, ai + 1, ..., aj) 都出现在S。

【分析】:如果最小元素不是给定集合的GCD,则答案为-1,否则,我们可以在集合的两个连续元素之间插入最小元素。序列长度为2n-1,满足约束条件。

//要求所有之间的gcd都在集合中,所以答案的所有元素的gcd必须在集合中。 此外,gcd(a,b)<= min(a,b),所以答案中所有元素的gcd必须是集合中最小的数字,所以每个数字必须将其分开。那么只需在原数列的相邻两个数ai,ai+1插入原序列最小的数就这样可以保证gcd不是自己,就是最小的数。 

【代码】:

#include 
using namespace std;typedef long long LL;const int maxn = 1010;int a[maxn];int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); if(n==1) { printf("1\n%d",a[1]); return 0; } int ok=1; for(int i=2;i<=n;i++) if(a[i]%a[1]!=0) { ok=0; break; } //2 3 if(!ok)//如果最小元素不是给定集合的GCD,则答案为-1 { printf("-1"); return 0; } printf("%d\n",(n-1)*2);//否则,我们可以在集合的两个连续元素之间插入最小元素。序列长度为2n-1,满足约束条件。 for(int i=2;i<=n;i++) printf("%d %d ",a[1],a[i]); return 0;}
数学构造

 

转载于:https://www.cnblogs.com/Roni-i/p/7866592.html

你可能感兴趣的文章
mysql数据库授权
查看>>
Microstation
查看>>
深入浅出的英语口语700句zz
查看>>
linux编译安装php
查看>>
再谈奶牛问题
查看>>
第一个java程序------hello world!
查看>>
C#学习安排表
查看>>
在LINUX上创建GIT服务器【转】
查看>>
Linux内核跟踪之trace框架分析【转】
查看>>
XCode v9.6.2017.0830
查看>>
ES不设置副本是非常脆弱的,整个文章告诉了你为什么
查看>>
设置nmon 每天自动收集性能信息
查看>>
python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】...
查看>>
JVM的内存分配与垃圾回收策略
查看>>
分布式设计与开发(二)------几种必须了解的分布式算法
查看>>
IT高管和易筋经的故事
查看>>
ASP.NET 2.0新控件、管理外观、布局及其它用户体验
查看>>
Javascript分号,加还是不加?
查看>>
[Ubuntu Setup] Ubuntu 14.10 LTS 中文输入法的安装
查看>>
Selenium获取当前窗口句柄与切换回原窗口句柄
查看>>