地址: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. 腾讯课堂1:使用Jmeter内置的录制功能进行录制

    1.设置http代理服务器 打开火狐——点击选项——高级——网络——设置  设置完成点击确定 2.查看端口是否被占用的命令 netstat -ano 3.排除模式 .*\.gif .*\.css .* ...

  2. 向量类Vector

    Java.util.Vector提供了向量(Vector)类以实现类似动态数组的功能.在Java语言中.正如在一开始就提到过,是没有指针概念的,但如果能正确灵活地使用指针又确实可以大大提高程序的质量, ...

  3. 组合使用QT的资源管理高级功能简化开发过程

    使用 QT 进行团队开发的时候,常常碰到一个问题,就是如何共同管理资源?甚至一个人进行开发的时候如何简化资源的维护,避免无谓的消耗? 如果可以做到在开发的时候,大家把美工做的图片(往往是程序员先自己随 ...

  4. 在 App Store 三年學到的 13 件事(下)

    博文转载至 http://blog.csdn.net/iunion/article/details/18959801     Steven Shen,曾經寫過一本書,也翻過一本書,開發 iOS app ...

  5. ng-template

    一,锚点链接 定义锚点: <a href="#login" class="btn btn-primary">Login</a> < ...

  6. 【BZOJ2259】[Oibh]新型计算机 最短路

    [BZOJ2259][Oibh]新型计算机 Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计 ...

  7. vmware key

    VMware vRealize Suite 2017 Enterprise   N04CL-09H9H-J89DJ-0KCH6-90N0J VMware vRealize Operations Man ...

  8. 170206、sping注解@autowired和@resource的区别

    新年第一天上班,新的一年,我们17加油!!! @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了 ...

  9. 160720、SSM-Shiro使用详解

    前言 相比有做过企业级开发的童鞋应该都有做过权限安全之类的功能吧,最先开始我采用的是建用户表,角色表,权限表,之后在拦截器中对每一个请求进行拦截,再到数据库中进行查询看当前用户是否有该权限,这样的设计 ...

  10. delphi xe----操作mongoDB驱动,TMongoWire(Delphi MongoDB Driver)

    所有例子来自:https://github.com/stijnsanders/TMongoWire Delphi MongoDB的驱动 一个Delphi的驱动程序来访问mongoDB的服务器.用jso ...