Codeforces Round #410 (Div. 2)C. Mike and gcd problem
2 seconds
256 megabytes
standard input
standard output
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e.
.
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.
is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
In the first example you can simply make one move to obtain sequence [0, 2] with
.
In the second example the gcd of the sequence is already greater than 1.
题解:我们发现一个位置经过两次操作a[i]变成-2a[i+1],a[i+1]变成2a[i],所以当gcd为1时我们可以把他们都变为偶数,所以我们把所有的数都变为偶数
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+;
int a[maxn],n;
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
int tmp=a[];
for(int i=;i<n;i++)
{
tmp=__gcd(tmp,a[i]);
}
if(tmp!=)
{
puts("YES\n0");
}
else
{
int ans=;
for(int i=;i<n;i++)
{
if(a[i]%==)continue;
else if(i==n-)
{
ans+=;
}
else
{
if(a[i+]%!=)ans++;
else ans+=;
i++;
}
}
printf("YES\n%d\n",ans);
} }
Codeforces Round #410 (Div. 2)C. Mike and gcd problem的更多相关文章
- 【推导】Codeforces Round #410 (Div. 2) C. Mike and gcd problem
如果一开始就满足题意,不用变换. 否则,如果对一对ai,ai+1用此变换,设新的gcd为d,则有(ai - ai+1)mod d = 0,(ai + ai+1)mod d = 0 变化一下就是2 ai ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #410 (Div. 2) A. Mike and palindrome
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #410 (Div. 2) B. Mike and strings
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
随机推荐
- Tensorflow 线性回归预测房价实例
在本节中将通过一个预测房屋价格的实例来讲解利用线性回归预测房屋价格,以及在tensorflow中如何实现 Tensorflow 线性回归预测房价实例 1.1. 准备工作 1.2. 归一化数据 1.3. ...
- sed修炼系列(三):sed高级应用之实现窗口滑动技术
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- 车牌识别OCR—易泊时代智慧城市解决方案模块
牌识别(License Plate Recognition,LPR) 是视频图像识别技术在智能交通领域中的一个模块.车牌识别运用OCR技术,将视频流或图片中的汽车牌照从复杂的应用场景中提取并识别出来, ...
- memcached常用命令
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt104 一.Memcache面向对象的常用接口包括:Memcache::con ...
- 向eclipse的JavaWeb项目中导入jar包--备忘录
一: 在你所需的jar包网站下载对应的jar包.如org.apache.commons.lang.jar. 二:复制粘贴到该JavaWeb的WEB-INF目录下的lib目录下,如: 三:右键 ...
- 第2阶段——编写uboot之编译测试以及改进(3)
编译测试: 1.将写好的uboot复制到linux下面 2.make编译,然后将错误的地方修改,生成boot.bin (编译出错的解决方案:http://www.cnblogs.com/lifexy/ ...
- 团队作业4----第一次项目冲刺(Alpha版本)4.29
a.站立式会议照片 会议内容: 会议讨论有一些内容要放在beta版本,所以燃尽图可能有些未完成.初级阶段我们只打算先写java代码的统计和显示.这是最基本的任务.在繁忙的任务中,不需要全部按照燃尽图. ...
- java201521123118《java程序设计》第5周总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1. 代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过 ...
- 201521123070 《JAVA程序设计》第5周学习总结
1. 本章学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. http://naotu.baidu.com/file/02b01f465e125c5942648a03358273b0 2. ...
- Java课程设计-定时器
Java课程设计--定时器 1.团队课程设计博客链接 团队博客地址 2.个人负责模块或任务说明 框架构建 时间设置面板,倒计时面板 按钮设置 3.自己的代码提交记录截图 4.自己负责模块或任务详细说明 ...