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;
}
思路:给定的序列中,对于最小的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;
}
相关文章推荐
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence(构造,思路)
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence(构造)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Dima and Sequence CodeForces - 272B (思维题)
- CodeForces - 894C-Marco and GCD Sequence
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #384 B. Chloe and the sequence
- Codeforces 894C - Marco and GCD Sequence 【GCD+思维】
- Codeforces Round #384 B. Chloe and the sequence
- Chloe and the sequence CodeForces - 743B
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- Codeforces Round #384 B. Chloe and the sequence
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #384 B. Chloe and the sequence
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #384 B. Chloe and the sequence