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
Input
3
2 7 28
Output
1
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
Input
3
2 7 28
Output
1
2 7 9 28
题意:给你一个序列,你可以在其中任意添加数字,问你怎样添加可以使两个相邻的数之间互质(尽量少步骤)
思路:首先两个数互质,那么他们的最大公约数应该为1.然后在两数之间添加数字的时候可以直接添加1.
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cstring>
using namespace std;
int lcm(int x,int y)
{
return x%y==0?y:lcm(y,x%y);
}
int main()
{
int n;
int a[2001],b[2001];
while(~scanf("%d",&n))
{
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int k=0;
for(int i=1;i<n;i++)
{
if(lcm(a[i],a[i-1])==1)
{
continue;
}
else
{
k++;
b[i]=1;
}
}
printf("%d\n",k);
printf("%d",a[0]);
for(int i=1;i<n;i++)
{
if(b[i])
printf(" %d",b[i]);
printf(" %d",a[i]);
}
printf("\n");
}
return 0;
}