CF660A -- Co-prime Array

来源:互联网 发布:网上宿迁网络问政 编辑:程序博客网 时间:2024/04/30 00:20
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 addingk elements to it.

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

Sample Input

Input
32 7 28
Output
12 7 9 28

题目大意:给出一个含N个元素的数列,在数列个元素之间可以随意插入任何小于10^9的正整数,每插入一个数算一步,求令这个数列相邻元素互质的最少的操作步数。
好吧,先来看一组奇葩的数据:
刚开始理解的是要大于前一项小于后一项,判断了半天,结果超时。。其实这道题还是So Easy的,毕竟是CF的A题。。既然要让这个数列的元素满足两两互质,那么直接在不互质的两个元素之间插入1不就OK了?!!白白浪费那么多时间。。
代码如下:
#include<cstdio>  #include<cstring>#include<algorithm>  using namespace std; int a[1010],b[1010];int gcd(int x,int y){if(y==0)return x;return gcd(y,x%y);}//求最大公约数 int main(){int n;while(~scanf("%d",&n)){for(int i=0;i<n;i++)scanf("%d",&a[i]);int ans=0;//用来记录步数 int k=0;//记录数列b的个数 for(int i=0;i<n-1;i++){if(gcd(a[i],a[i+1])==1)b[k++]=a[i];//如果互质则直接将该元素赋值给数列b else{ans++;b[k++]=a[i];//不互质的两个数中第一个数仍赋值给b b[k++]=1;//在不互质的第一个数后插入1 }//不互质的第二个数在下一步操作中处理 }b[k++]=a[n-1];//最后一个数,在循环中没考虑到!! printf("%d\n%d",ans,b[0]);for(int i=1;i<n+ans;i++){printf(" %d",b[i]);//输出格式 }printf("\n");}    return 0;  }

0 0
原创粉丝点击