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\) 次操作,每一次操作给出 \ ...
随机推荐
- python进度条
#!/usr/bin/env python# -*- coding:utf-8 -*- import urllib url = "http://www.163.com/" #htm ...
- 9. PD逆向工程--由数据库转为模型(ER图)
步骤: 1. 在控制面板-->管理工具(如果没找到管理工具,查看方式改为大图标)-->数据源(ODBC)-->用户DSN -->用户数据源下添加一个数据源(这里根据情况添加数据 ...
- 2. java获取下周日-下周六的时间
String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...
- PHP设计模式:类自动载入、PSR-0规范、链式操作、11种面向对象设计模式实现和使用、OOP的基本原则和自动加载配置
一.类自动载入 SPL函数 (standard php librarys) 类自动载入,尽管 __autoload() 函数也能自动加载类和接口,但更建议使用 spl_autoload_registe ...
- ajax 实现跨域
ajax本身是不可以跨域的,通过产生一个script标签来实现跨域.因为script标签的src属性是没有跨域的限制的. 其实设置了dataType: 'jsonp'后,$.ajax方法就和ajax ...
- SQL 2012 分页取数据
,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...
- XE6 c++builder 设置 font size GetPropInfo SetOrdProp
PPropInfo ppi; PTypeInfo pti; TTypeKinds ttk; TRttiContext context; TRttiType *rttiType TObject* obj ...
- webstorm使用
1. 打开toolbar 2. 安装vue.js 3. 设置new vue file模板 <template> <div> <header-component/> ...
- Spring boot profile 多环境配置
1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml 默认使用application.properties的配置 ...
- python中join()函数、list()函数补充的用法
---恢复内容开始--- Python join() 方法用于将序列中的元素(必须是str) 以指定的字符 连接生成一个新的字符串. list=[','a','b','c'] print(''.joi ...