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 & %I64uDescription
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;
}
相关文章推荐
- CodeForces 660A Co-prime Array(GCD)
- CodeForces 660A Co-prime Array (互质队列)
- CodeForces 660A Co-prime Array(互质数列)
- CodeForces 645F Cowslip Collections(gcd+欧拉函数)
- CodeForces 585D Present for Vitalik the Philatelist(容斥原理+gcd)
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
- CodeForces 851D 区间修改值gcd
- CodeForces 660A Co-prime Array
- codeforces 338D GCD Table
- Codeforces 592C The Big Race 【GCD && LCM】
- poj3696 快速幂的优化+欧拉函数+gcd的优化+互质
- codeforces 338D - GCD Table 扩展中国剩余定理
- Codeforces 623B:Array GCD
- 【codeforces 798C】Mike and gcd problem
- Codeforces--660A--Co-prime Array(数学水题)
- codeforces 798C - Mike and gcd problem 贪心
- codeforces-9A-A. Die Roll ( C && GCD )
- 【CodeForces】660A - Co-prime Array(水)
- codeforces 660A Co-prime Array
- CodeForces 660A Co-prime Array