Educational Codeforces Round 11(A)思维,数学

A. Co-prime Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Example
input
3
2 7 28
output
1
2 7 9 28



题意:给你一个数组,让你插入一些数字,使得这个数组的相邻的互质数字尽可能的多.



题解:直接枚举a[i],a[i-1],使用gcd函数判断一下他们的公约数,如果公约数不是1,在他们之间插个1就ok了。。。。。




#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(1e5)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;


#ifdef CDZSC  
#define debug(...) fprintf(stderr, __VA_ARGS__)  
#else  
#define debug(...)   
#endif  

int gcd(int a, int b)
{
	return b == 0 ? a : gcd(b, a%b);
}

int a[N];
int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);  
	int _time_jc = clock();
#endif  

#ifdef CDZSC  
	debug("time: %d\n", int(clock() - _time_jc));
#endif  
	int n;
	scanf("%d", &n);
	int last;
	for (int i = 0; i < n; i++)scanf("%d", &a[i]);
	a[n] = 1;
	vector<int>ans;
	for (int i = 0; i < n; i++)
	{
		if (gcd(a[i], a[i + 1]) != 1)
		{
			ans.push_back(a[i]);
			ans.push_back(1);
			
		}
		else
		ans.push_back(a[i]);
	}
	printf("%d\n", ans.size() - n);
	for (int i = 0; i < ans.size(); i++)
	{
		printf("%d ", ans[i]);
	}
	return 0;
}








你可能感兴趣的:(Educational Codeforces Round 11(A)思维,数学)