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 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 k elements to it.
If there are multiple answers you can print any one of them.
Sample Input
3
2 7 28
Sample Output
1
2 7 9 28
Hint
题意
给你一个序列,让你插入最小的数,使得任意相邻的两个数都互质
让你输出最后的序列长什么样
题解:
显然插入的数最优就是1嘛,1和任何数都互质
然后我们贪心的去插就好了,能插就插
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn];
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
vector<pair<int,int> >P;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
if(gcd(a[i],a[i+1])!=1)
P.push_back(make_pair(1,i));
}
cout<<P.size()<<endl;
int j = 0;
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
if(j<P.size()&&P[j].second==i)
{
cout<<P[j].first<<" ";
j++;
}
}
cout<<endl;
}
Educational Codeforces Round 11 A. Co-prime Array 水题的更多相关文章
- Educational Codeforces Round 11 B. Seating On Bus 水题
B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...
- Educational Codeforces Round 14 A. Fashion in Berland 水题
A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...
- Educational Codeforces Round 12 A. Buses Between Cities 水题
A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...
- Educational Codeforces Round 4 A. The Text Splitting 水题
A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...
- Codeforces Educational Codeforces Round 3 B. The Best Gift 水题
B. The Best Gift 题目连接: http://www.codeforces.com/contest/609/problem/B Description Emily's birthday ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...
- Educational Codeforces Round 13 D. Iterated Linear Function 水题
D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...
- Educational Codeforces Round 13 C. Joty and Chocolate 水题
C. Joty and Chocolate 题目连接: http://www.codeforces.com/contest/678/problem/C Description Little Joty ...
- Educational Codeforces Round 13 B. The Same Calendar 水题
B. The Same Calendar 题目连接: http://www.codeforces.com/contest/678/problem/B Description The girl Tayl ...
- Educational Codeforces Round 13 A. Johny Likes Numbers 水题
A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...
随机推荐
- 关于linux系统如何实现fork的研究(一)【转】
转自:http://www.aichengxu.com/linux/4157180.htm 引言 fork函数是用于在linux系统中创建进程所使用,而最近看了看一个fork()调用是怎么从应用到gl ...
- Workqueue机制的实现
Workqueue机制中定义了两个重要的数据结构,分析如下: cpu_workqueue_struct结构.该结构将CPU和内核线程进行了绑定.在创建workqueue的过程中,Linux根据当前系统 ...
- 148.Sort List---链表排序(冒泡、归并)
题目链接 题目大意:对链表进行排序,要求时间复杂度是o(nlgn). 法一:冒泡,不交换结点,而交换结点中的数值.超时了.代码如下: public ListNode sortList(ListNode ...
- Codeforces Round #456 (Div. 2)
Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...
- Ubuntu或者Ubuntu server重新设置IP地址
1.打开终端输入: sudo vi /etc/network/interfaces 2.进入编辑页面 改一处,添加5行内容,如下图: 3.修改好后esc :wq进行保存 4.输入: sudo / ...
- Java基础1,入门基础知识
本文知识点(目录): 1.java简介 2.环境的搭建 3.关键字 4.标识符 5.注释 6.常量 7.进制的转换 8.变量 9.数据类型的转换 ...
- C++随笔(2)
在牛客网上刷题,遇到的一些需要注意的题 1.这题需要注意的是strcpy复制的时候什么时候停止 2.这题是关于strlen的,它不统计‘\0',但复制的时候仍会复制. 3.这题是写strcpy函数的, ...
- django rest_framework比较完整的自定义实现样例
里面有自定义的更新策略, 序列化时,考虑nest及显示. 很有参考意义. 然后,前端,可以考虑用angular.js或vue.js实现. 每次以token进行认证. url.py router = D ...
- Python爬虫笔记(一)
个人笔记,仅适合个人使用(大部分摘抄自python修行路) 1.爬虫Response的内容 便是所要获取的页面内容,类型可能是HTML,Json(json数据处理链接)字符串,二进制数据(图片或者视频 ...
- php中var_dump()函数
var_dump() void var_dump ( mixed expression [, mixed expression [, ...]] ) var_dump()方法是判断一个变量的类型与长度 ...