codeforce_894C_給你原序列所有連續子序列的GCD從小到大,求原序列_構造

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 every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd 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.

Sample Input
Input
4
2 4 6 12
Output
3
4 6 12
Input
2
2 3
Output
-1

mean
給你原序列所有連續子序列的GCD從小到大,求原序列_構造

ans

因爲GCD(a)=a所以這個序列中的數都在原序列出現過,所以這個序列最小GCD=第一個數
如果輸入序列的某幾個數的公約數不在輸入序列裏面,這個答案就不對了,
比如 1 6 8,6和8可以求出公約數2,怎麼能避免相鄰數又生出新的公約數呢?就要插入某個數打斷這種可能,

#include<bits/stdc++.h>
#define ll long long
const ll mod=(ll)1000000007;
using namespace std;
int gcd(int a,int b)
{
    return b==0? a:gcd(b,a%b);
}

int main()
{
    int  m,n,i,j;
    while(cin>>n)
    {
        int a[1111],f=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",a+i);
            if(!i)
                f=a[i];
            else
                f=gcd(a[i],f);
        }
        if(f!=a[0])
            puts("-1");
        else
        {
            cout<<2*n<<endl;
            for(i=0;i<n;i++)
            {
                printf("%d %d",a[0],a[i]);
                if(i!=n-1)
                    cout<<" ";
                else puts("");
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章