地址: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. ADO.NET数据库编程

    ADO.NET数据库编程 1.ADO.NET的相关概念. Microsoft的新一代技术,是ADO组件的后继者. 主要目的是在.NET Framework平台存取数据. 提供一致的对象模型,可以存取和 ...

  2. DpQuery.js

    (function (window) { //添加事件的方法通用所有 function addevent(evetname, fn, obj) { if (document.attachEvent) ...

  3. PHP 中 json_encode中文处理、urlencode方法、post中文乱码

    当使用php自带的json_encode对数据进行编码时,中文都会变成unicode,导致不可读.如:对字符串”厦门“进行json_encode后,输出的是"\u53a6\u95e8&quo ...

  4. ChemDraw Pro和ChemBio3D Ultra有什么区别

    作为ChemOffice 14的两个重要组件,ChemDraw Pro和ChemBio3D Ultra都有着自己的特点,ChemDraw Pro以绘制平面结构而ChemBio3D Ultra以绘制立体 ...

  5. Reactjs中的相关机制

    http://blog.csdn.net/cengjingcanghai123/article/details/48480473 https://segmentfault.com/a/11900000 ...

  6. JDB调试之小试牛刀

    用JDK自带工具JDB调试示例程序HelloJDB(d:\jdb\HelloJDB) HelloJDB代码如下: public class HelloJDB { public static void ...

  7. django用户认证系统——注册3

    用户注册就是创建用户对象,将用户的个人信息保存到数据库里.回顾一下 Django 的 MVT 经典开发流程,对用户注册功能来说,首先创建用户模型(M),这一步我们已经完成了.编写注册视图函数(V),并 ...

  8. eslint Rules

    Rules 为了让你对规则有个更好的理解,ESLint 对其进行了分门别类. 所有的规则默认都是禁用的.在配置文件中,使用 "extends": "eslint:reco ...

  9. css3动画效果:1基础

    css动画分两种:过渡效果transition .关键帧动画keyframes 一.过渡效果transition 需触发一个事件(如hover.click)时,才改变其css属性. 过渡效果通常在用户 ...

  10. AEcs6破解版下载

    下载地址 http://pan.baidu.com/share/link?shareid=79184520&uk=1795677788 点击下载