地址:http://codeforces.com/contest/660/problem/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 adding kelements 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,即gcd(a,b)=1;

  如果相邻两个数不是互质的话,插个1就好了(比赛时没想到1,插得是1—100内某一质数,(因为x<10^9,所以100内的质数绝对可以,因为乘积大于最大取值范围了)

  代码:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; int a[];
int b[];
int prime[] = { ,,,,,,,,,,,,,,,, }; int is_coprime(int a, int b)
{
int t = ;
while (t)
{
if (b == )
return ;
t = a %b;
a = b;
b = t;
}
return ;
} int main(void)
{
int n, k;
while (scanf("%d", &n) == )
{
k = ;
memset(b, , sizeof(b));
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i<n; i++)
{
if (is_coprime(a[i], a[i + ]))
{
b[k++] = a[i];
}
else
{
b[k++] = a[i];
for (int j = ; j <= ; j++)
if (is_coprime(a[i], prime[j]) && is_coprime(a[i + ], prime[j]))
{
b[k++] = prime[j];
break;
}
}
}
b[k] = a[n];
cout << k - n << endl;
for (int i = ; i<k; i++)
printf("%d ", b[i]);
printf("%d\n", b[k]);
}
return ;
}

Educational Codeforces Round 11A. Co-prime Array 数学的更多相关文章

  1. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  2. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

  3. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

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

  6. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  7. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  9. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

随机推荐

  1. 在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计、SQL语句、java等层面的解决方案。

    在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计.SQL语句.java等层面的解决方案. 解答: 1)数据库设计方面: a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 whe ...

  2. redis php 实例二

    前面一篇博客主要是string类型,list类型和set类型,下面hash类型和zset类型 1,hset 描述:将哈希表key中的域field的值设为value.如果key不存在,一个新的哈希表被创 ...

  3. js autoComplate

    在实际的项目中,能用别人写好的插件实现相关功能是最好不过,为了节约时间成本,因为有的项目比较紧急,没充分时间让你自己来写,即便写了,你还要花大量时间调试兼容性.但是出于学习的目的,你可以利用闲暇时间, ...

  4. 蓝桥杯 第四届C/C++预赛真题(3) 第39级台阶(递归)

    题目标题: 第39级台阶 小明刚刚看完电影<第39级台阶>,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级! 站在台阶前,他突然又想着一个问题: 如果我每一步只能迈上1个或2个台阶 ...

  5. c# 可变数目参数params实例

    一般来说,参数个数都是固定的,定义为集群类型的参数可以实现可变数目参数的目的,但是.NET提供了更灵活的机制来实现可变数目参数,这就是使用params修饰符 一般来说,参数个数都是固定的,定义为集群类 ...

  6. Lenna图-莱娜·瑟德贝里

    莱娜·瑟德贝里(瑞典文:Lena Soderberg),1951年3月31日出生于瑞典,在1972年11月期的<花花公子>杂志中,她化名为莱娜·舍布洛姆,成为了当期的玩伴女郎. 她的中间折 ...

  7. RequireJS禁止缓存

    通过配置文件可以禁止加载缓存的JS文件, 这个在开发过程中非常有用具体做法如下 require.config({ paths: { "E":"/Scripts/MyMod ...

  8. Amazon【EC2】如何启动一个新的EC2实例

    1.1 控制台可直接搜索ec2 1.2 创建实例 aws没有新建实例这个说法,新建就是启动实例 1.3 选择镜像 在搜索框中搜索自己想要的镜像.这里有收费的也有免费的 1.4 选择实例类型 选择自己想 ...

  9. manacher算法处理最长的回文子串(二)

    在上篇<manacher算法处理最长的回文子串(一)>解释了manacher算法的原理,接着给该算法,该程序在leetcode的最长回文子串中通过.首先manacher算法维护3个变量.一 ...

  10. CAS SSO单点登录框架介绍

    1.了解单点登录  SSO 主要特点是: SSO 应用之间使用 Web 协议(如 HTTPS) ,并且只有一个登录入口. SSO 的体系中有下面三种角色: 1) User(多个) 2) Web 应用( ...