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.
Example
input
3
2 7 28
output
1
2 7 9 28
题解:1跟任何数的因子都为1。
#include<iostream>
#include<cstdio>
using namespace std;
int func1(int x, int y) //取两数最大公因数
{
return y == 0 ? x : func1(y, x%y);
}
int main()
{
int n;
int v[2000];
memset(v, 0, sizeof(v));
scanf("%d", &n);
for (int i = 0; i < 2*n-1; i=i+2) //两数之间空一个位置
{
scanf("%d", &v[i]);
}
int sum = 0;
for (int j=2;j<2*n-1;j=j+2)
{
int key = func1(v[j-2],v[j]);
if (key != 1) //最大公因数不为1,将这两数之间的空位置赋1
{
sum++;
v[j - 1] = 1;
}
}
printf("%d\n", sum);
for (int j = 0; j < 2 * n - 1; j++)
{
if (v[j] != 0)
printf("%d ", v[j]);
}
printf("\n");
return 0;
}