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
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 <bits/stdc++.h>

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

数学构造

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. asp.net 常用几种下载方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  2. 有关ViewPager的使用及解决Android下ViewPager和PagerAdapter中调用notifyDataSetChanged失效的问题

    ViewPager是android-support-v4.jar包中的一个系统控件,继承自ViewGroup,专门用以实现左右滑动切换View的效果,使用时需要首先在Project->prope ...

  3. 《Cracking the Coding Interview》——第17章:普通题——题目6

    2014-04-28 22:49 题目:给定一个整数数组.如果你将其中一个子数组排序,那么整个数组都变得有序.找出所有这样子数组里最短的一个. 解法:线性时间,常数空间内可以解决,思想类似于动态规划. ...

  4. mysql用root账户建立用户和赋予权限

    1.创建用户 create user guest_test@localhost identified by "root";-- 创建名为guest_test的用户 2.赋予权限 - ...

  5. springboot示例参考网站

    https://blog.csdn.net/u013248535/article/details/55100979/

  6. [译]14-spring 集合元素的注入

    前面的文章已经介绍了如何往bean里面注入原始类型和引用类型.我们使用bean元素的contructor-arg或property子 元素的value属性注入java原始类型;同理,我们可以使用bea ...

  7. IPMITool driver

    官网链接: https://docs.openstack.org/ironic/latest/admin/drivers/ipmitool.html IPMITool driver 概述IPMI(In ...

  8. install ironic-inspector

    安装相应的包和组件 yum install openstack-ironic-inspector python-ironic-inspector-client -y 创建user openstack ...

  9. Ubuntu下禁用笔记本自带键盘

    想要禁用笔记本自带键盘(Ubuntu)只要2条命令. 1. 打开终端,输入: xinput list ⎡ Virtual core pointer id=2 [master pointer (3)] ...

  10. .export*读取图片

    *读取图片 read_image(Image,'D:/MyFile/halcon/数字识别/1.jpg define PHYS_FLASH2_1 0xBC000000 /* Image2 Bank # ...