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.
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.
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 kelements to it.
If there are multiple answers you can print any one of them.
3
2 7 28
1
2 7 9 28
题意:给出n个数,在其中插入一些数,使得任意两个相邻的数互质。
题解:1和任意一个数互质,所以当找到两个数不满足条件时,在这之间插入一个1即可。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /*A*/ 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 const int maxn=1000+100; 7 int gcd(int a,int b) 8 { 9 return b==0?a:gcd(b,a%b); 10 } 11 12 int main() 13 { 14 int n; 15 int a[maxn]; 16 int insert[maxn]; 17 while(scanf("%d",&n)!=EOF) 18 { 19 memset(a,0,sizeof(a)); 20 memset(insert,0,sizeof(insert)); 21 for(int i=0;i<n;i++) 22 scanf("%d",&a[i]); 23 int k=0; 24 for(int i=0;i<n-1;i++) 25 { 26 int t=gcd(a[i],a[i+1]); 27 if(t!=1) 28 { 29 //insert[i]=getinsert(a[i],a[i+1]); 30 insert[i]=1; 31 k++; 32 } 33 } 34 printf("%d\n%d",k,a[0]); 35 if(insert[0]) 36 printf(" %d",insert[0]); 37 for(int i=1;i<n;i++) 38 { 39 printf(" %d",a[i]); 40 if(insert[i]) 41 printf(" %d",insert[i]); 42 } 43 printf("\n"); 44 } 45 return 0; 46 }