Codeforces 660A. Co-prime Array 最大公约数
1 second
256 megabytes
standard input
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.
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.
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 k elements to it.
If there are multiple answers you can print any one of them.
3
2 7 28
1
2 7 9 28 题目链接:http://codeforces.com/problemset/problem/660/A
题意:一个数组里面任意相邻的两个数互质,则数组为互质数组。添加最少的数使得这个数组为互质数组。输出添加的数的个数和添加之后
的数组。 思路:1和任意数都互质。
代码:
#include<bits/stdc++.h>
using namespace std;
__int64 a[],sign[];
__int64 gcd(__int64 x,__int64 y)
{
__int64 t;
if(x<y)
{
t=x;
x=y;
y=t;
}
while(y)
{
t=x%y;
x=y;
y=t;
}
return x;
}
int main()
{
int i,n;
int ans=;
memset(sign,,sizeof(sign));
scanf("%d",&n);
a[]=;
for(i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
if(a[i]>=&&gcd(a[i],a[i-])!=)
{
ans++;
sign[i]=;
}
}
cout<<ans<<endl;
for(i=; i<=n; i++)
{
if(sign[i]==) cout<<""<<" ";
if(i!=n) cout<<a[i]<<" ";
}
cout<<a[n]<<endl;
return ;
}
Codeforces 660A. Co-prime Array 最大公约数的更多相关文章
- codeforces 660A A. Co-prime Array(水题)
题目链接: A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- CodeForces 660A Co-prime Array
水题.放个1就可以了.暴力的找数字也是很快的. #include<cstdio> #include<cstring> #include<cmath> #includ ...
- Educational Codeforces Round 11A. Co-prime Array 数学
地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...
- Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...
- 【CodeForces 624D/623B】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- Codeforces 408 E. Curious Array
$ >Codeforces \space 408 E. Curious Array<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(a\) ,\(m\) 次操作,每一次操作给出 \ ...
随机推荐
- centos6.5 64安装ffmpeg过程支持转码mp3
百度了几个文章 大致知道了思路 首先yum源安装是木有的,只能编译安装了. 要安装ffmpeg要先安装一个yasm支持汇编优化(FFmpeg需要) 在安装一个lame,支持mp3的转码 那就是需要3步 ...
- 如何从编程的本质理解JVM内存模型
如何从编程的本质理解JVM内存模型 一般聊JVM内存模型都是把图截出来,然后对着图,解释上面堆.栈之类的概念.这篇将分享下,如何从编程的本质上理解,JVM内存模型是什么样子,为什么是这个样子,不再死记 ...
- mysql5.7.13 使用笔记
社区版下载地址:https://dev.mysql.com/downloads/mysql/ 安装:http://www.linuxidc.com/Linux/2016-04/130414.htm ...
- leetcode459
public class Solution { public bool RepeatedSubstringPattern(string s) { var len = s.Length; ) { ret ...
- leetcode326
public class Solution { public bool IsPowerOfThree(int n) { && ( % n == ); } } https://leetc ...
- Jsch - java SFTP 文件上传下载
使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...
- IExpress 制作安装包 注意事项
被打包的文件名不能超过8个字符,否则iexpress会取前6个字符 + "~1".
- ncodeURIComponent() 函数 vue内容
ncodeURIComponent() 函数 编辑 encodeURIComponent() 函数[1] 作用:可把字符串作为URI 组件进行编码.其返回值URIstring 的副本,其中的某些字符 ...
- Shell常用命令find、grep总结
一.find 命令 find 命令的常用形式可以简化为 find [path...][expression] path:find 命令查找的目录路径,其中 ./ 表示当前目录,/ 表示系统根目录 ex ...
- Haskell语言学习笔记(32)Data.Maybe, Data.Either
Data.Maybe Prelude Data.Maybe> maybe "" show (Just 5) "5" Prelude Data.Maybe& ...