Codeforces 894C - Marco and GCD Sequence 【GCD+思维】


C. Marco and GCD Sequence


time limit per test  1 second

memory limit per test      256 megabytes


In a dream Marcomet an elderly man with a pair of black glasses. The man told him the key toimmortality 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 somelength n, but forgot theexact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest commondivisor.

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

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

Input

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

The second linecontains 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 strictlyincreasing order, that means s1 < s2 < ... < sm.

Output

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

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

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

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

If there are multiple solutions, printany 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 showthat there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.

 

【题意】


原来有一个序列,但是你已经把它忘了,只知道它的每一个子区间的最大公倍数所组成的集合,且这个集合中的数按照从小到大的顺序给出要求你还原出一种符合要求的原序列。


【思路】


首先我们需要明确一点,集合中最小的数一定是其他数的一个因子,因为它肯定是原序列所有数取gcd后的结果。由此我们可以判断能否还原出原序列。


然后我们构造原序列的方法便是先取出这个最小的数,然后依次跟剩余(n-1)个数组成二元组<a[0],a[i]>,然后输出即可。


这样的构造方案,如果区间内只有一个数,那么一定涵盖了集合中所有的数,否则他们的gcd一定为a[0],满足题意。


【PS】 注意n==1情况的特判。


#include <cstdio>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 20005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n;
int a[maxn];

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    if(n==1) return printf("1 %d\n",a[0]),0;
    int flag=0;
    for(int i=1;i<n;i++)
    {
        if(a[i]%a[0])
        {
            flag=1;
            break;
        }
    }
    if(flag) return puts("-1"),0;
    printf("%d\n",2*n-2);
    for(int i=1;i<n;i++)
    {
        printf("%d %d ",a[0],a[i]);
    }
    puts("");
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值