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\) 次操作,每一次操作给出 \ ...
随机推荐
- FBV和CBV区别
FBV和CBV区别 def dispatch(self, request, *args, **kwargs): # 做分发的 if request.meth ...
- ASCII和万国码
ASCII和万国码 什么是ASCII 计算机的起初是使用内存中的0101来表示数和机器码.如何用内存中的bit来表示文本一直困扰着人们,毕竟人类主要的信息展示是文字,而不是苦涩的0101.后来ASCI ...
- VCSA 6.5 升级 VCSA 6.7
VCSA 6.7已于4月17日正式发布,文件名为VMware-VCSA-all-6.7.0-8217866.iso,国内百度网盘已有下载链接,请自行搜索. 下载后解压,运行\VMware-VCSA-a ...
- WPF 自定义属性
做了一个自定义控件和一个自定义Grid,里面的元素可以随着绑定属性变化: 效果图(一定滑块): 关键代码: 1.自定义属性代码: public class MyGrid : Grid { public ...
- ELK配置过程初次安装使用心得--elasticsearch5.4版--及logstash
安装所遇到的问题:http://www.bubuko.com/infodetail-1889252.html 一,先创建用户和组groupadd es useradd -g es es passwd ...
- 1.Redis的应用场景
转自:http://www.runoob.com/redis/redis-tutorial.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在 ...
- xe DateTimePicker.Date bug
xe6 bug xe7 ok DateTimePicker1->DateTime.DateString(); DateTimePicker1->DateTime.DateTimeStrin ...
- FDMemTable 数据集
c++builder FDMemTable 内存表 内存数据表:现在应该首选 TFDMemTable 了(之前是 TClientDataSet) FDMemTable->CloneCursor( ...
- ListView 操作
TListItem *Item; ; i < ; i++) { Item = ListView1->Items->Add(); Item->Caption = i; } Lis ...
- rssh RSA(非对称密钥)
rssh ,非对称密钥,分为密钥和公钥 ,密钥在对面机器,需要进入的文件中,公钥是放在本地机器上 import paramiko private_key = paramiko.RSAKey.from_ ...