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 greatest common divisor.

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
  1. 4
    2 4 6 12
output
  1. 3
    4 6 12
input
  1. 2
    2 3
output
  1. -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不是自己,就是最小的数

【代码】:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long LL;
  5. const int maxn = ;
  6.  
  7. int a[maxn];
  8. int main()
  9. {
  10. int n;
  11. scanf("%d",&n);
  12. for(int i=;i<=n;i++) scanf("%d",a+i);
  13. if(n==)
  14. {
  15. printf("1\n%d",a[]);
  16. return ;
  17. }
  18.  
  19. int ok=;
  20.  
  21. for(int i=;i<=n;i++)
  22. if(a[i]%a[]!=)
  23. {
  24. ok=;
  25. break;
  26. }
  27. //2 3
  28. if(!ok)//如果最小元素不是给定集合的GCD,则答案为-1
  29. {
  30. printf("-1");
  31. return ;
  32. }
  33.  
  34. printf("%d\n",(n-)*);//否则,我们可以在集合的两个连续元素之间插入最小元素。序列长度为2n-1,满足约束条件。
  35. for(int i=;i<=n;i++) printf("%d %d ",a[],a[i]);
  36. return ;
  37. }

数学构造

Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】的更多相关文章

  1. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  2. Codeforces Round #447 (Div. 2) 题解 【ABCDE】

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

  3. Codeforces Round #447 (Div. 2)

    我感觉这场CF还是比较毒的,虽然我上分了... Problem A  QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...

  4. 【Codeforces Round #447 (Div. 2) C】Marco and GCD Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把gcd(a[1..n])放在输入的n个数之间. [代码] /* 1.Shoud it use long long ? 2.Have ...

  5. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学

    题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...

  6. Codeforces Round #447 (Div. 2) 题解

    A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...

  7. Codeforces Round #447 (Div. 2) C 构造

    现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...

  8. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】

    B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...

  9. Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】

    A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. Javascript进阶:对象实例属性和方法

    Ecmascript中,Object类型是所有它的实例的基础.换句话说,Object类型所具有的任何属性和方法也同样存在于更具体的对象中. Object的每个实例都具有以下属性和方法,这些都能方便于我 ...

  2. Python 字符串换行的几种方式

    第一种: x0 = '<?xml version="1.0"?>' \ '<ol>' \ ' <li><a href="/pyt ...

  3. Yapi的坑

    前一段时间,研究WEB Api相关的工具. YApi 可以内网部署,内心十分的欢喜啊.而且gitHub上推荐超过4000星,成绩很优异嘛.然而通过最终的尝试,我还是打算放弃他,投入Postman的怀抱 ...

  4. 常用模块(datatime)

    import datetime,time# dt = datetime.datetime.now() # 获取当前时间的时间对象# dt = datetime.date.fromtimestamp(t ...

  5. Python网络编程(http协议,IO多路复用、select内核监听)

    前言: 什么是IO? 分为IO设备和IO接口两个部分 如Linux系统,I/O操作可以有多种方式 比如DIO(DirectI/O) AIO(AsynchronousI/O异步I/O) Memory-M ...

  6. 附录:python and numpy

    http://cs231n.github.io/python-numpy-tutorial/ Python is a great general-purpose programming languag ...

  7. python作业:HAproxy配置文件操作(第三周)

    一.作业需求: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend ...

  8. HDU 4750 Count The Pairs (离线并查集)

    按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...

  9. 【bzoj1856】[Scoi2010]字符串 Catalan数

    题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足 ...

  10. ubuntu系统更换源

    一:问题概述 ubuntu,我们在使用apt新装软件的时候,会使用官方的网站去下载软件,但是会因为国内的转接点太多,而导致下载的速度非常慢 ,我们可以通过换成一些中间的节点来进行下载,比如阿里源,中科 ...