您的位置:首页 > 其它

CodeForces 632B Co-prime Array 【GCD(互质)】

2016-08-22 20:59 344 查看

Co-prime Array

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
 
Description

You are given an array of n elements, you must make it a co-prime array in as few moves as possible.

In each move you can insert any positive integral number you want not greater than 109 in any place in the array.

An array is co-prime if any two adjacent numbers of it are co-prime.

In the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of elements in the given array.

The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.

Output

Print integer k on the first line — the least number of elements needed to add to the array a to make it co-prime.

The second line should contain n + k integers aj — the elements of the array a after adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array
a by adding k elements to it.

If there are multiple answers you can print any one of them.

Sample Input

3

2 7 28
Output

1
2 7 9 28

给你一个数列,让你将它转化成互质数列,即任意相邻的数都互质(最大公约数是1),通过在不互质的两个数之间加一个数;

如果两个相邻的不互质,直接在中间加1就可以了(1与任何数的最大公约数都是1,即与任意数互质>1);

失误:写的时候没有看到还要输出添加了多少数,直接输出了

代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;

int a[1000+44],b[1111];

int Gcd(int x,int y)
{
return y==0?x:Gcd(y,x%y);//x写反了
}

int main()
{
int i,N;
while(~scanf("%d",&N))
{
for(i=1;i<=N;++i) scanf("%d",&a[i]);
i=1;
int cnt=0;
while(i<N)
{
b[++cnt]=a[i];
if(Gcd(a[i],a[i+1])!=1)
{
b[++cnt]=1;
}
++i;
}
b[++cnt]=a[i];
printf("%d\n",cnt-N);//需要先算出有多少对非互质的数
for(i=1;i<=cnt;++i)
{
if(i>1) printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: