地址: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. 第二百二十三节,jQuery EasyUI,ComboBox(下拉列表框)组件

    jQuery EasyUI,ComboBox(下拉列表框)组件,可以远程加载数据的下拉列表组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 C ...

  2. 简单说明什么是递归?什么情况会使用?并使用java实现一个简单的递归程序。

    解答: 1)递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象. 2)递归算法一般用于解决三类问题: a.数据的定义是按递归定义的.(Fib ...

  3. 卡友友刷MPOS注册开通流程!

    1.下载友刷APP:打开微信扫描机器背面二维码—点击右上角游览器打开 2. 注册-身份证认证-结算卡绑定:用本人手机号注册完成后,顺着进行身份认证.以及储蓄结算卡绑定.具体看下图: 3.绑定机器:选择 ...

  4. 复合文档(Compound Document)读写栗子

    复合文件是把磁盘文件系统的管理方式移植到文件中---复合文件. 复合文档是由 Windows 系统通过 COM 提供的, 它能完成像 Windows 目录结构一样复杂的文件结构的存取:提示一下 Win ...

  5. plsql参数

    PL/SQL中对out,in out参数使用的? 默认形参会复制一份实参的副本,然后在内部传递,修改等,发生异常,不会赋值给实参,控制权交还调用环境,而实参值不变,还是调用前的值.而使用了NOCOPY ...

  6. Android版微信小代码(转)

    以下代码仅适用于Android版微信: //switchtabpos:让微信tab更贴合Android Design 如果你并不喜欢微信Android版和iOS端同用一套UI,现在有一个小方法可以实现 ...

  7. 数据库设计——E-R图

    一,复习下基础 1.弱实体 这样的实体须要依赖还有一个实体.若还有一个实体不存在了.它也随之消失.相对而言,它依赖的那个实体成为强实体. 2,特殊化 在数据库设计的过程中,我们会发现.一个实体能够依照 ...

  8. NATS源代码之logger目录

    nats的logger目录文件如下 log.go syslog.go syslog_windows.go 基于golang语言的logger包实现日志功能. Golang的log包短小精悍,可以非常轻 ...

  9. 【BZOJ3261】最大异或和 Trie树+贪心

    [BZOJ3261]最大异或和 Description 给定一个非负整数序列 {a},初始长度为 N.       有   M个操作,有以下两种操作类型:1 .A x:添加操作,表示在序列末尾添加一个 ...

  10. mac下面安装多个JDK

    JDK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,J ...