您的位置:首页 > 产品设计 > UI/UE

codeforces C. Marco and GCD Sequence

2017-11-29 22:46 316 查看
题意:按照升序给定一个序列,每个数都是一个要求的序列的子序列的gcd,问这个要求的序列可能是什么,不存在输出-1。

思路:给定的序列中,对于最小的gcd,如果不是其他元素的gcd,则没有解。

对于有解的情况,只需要将最小的gcd插入每两个数之间,这样的话,新得到的序列的子序列的gcd要么是a[0],要么是它本身。

#include<cstdio>
#include<cstring>
#include<iostream>
#define maxn 1005
#define LL long long
#define mod 1000000007
using namespace std;
LL gcd(LL a,LL b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
ios::sync_with_stdio(false);
int n;
int a[maxn];
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int cur;
cur=a[0];
for(int i=1;i<n;i++)
{
cur=gcd(cur,a[i]);
}
if(cur!=a[0])
{
cout << -1 << endl;
continue;
}
cout << n*2 << endl;
for(int i=0;i<n;i++)
{
if(i!=n-1)
cout << a[0] << ' ' << a[i] << ' ';
else
cout << a[0] << ' ' << a[i];
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: