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 ...
随机推荐
- Django视图,与数据库交互并返回数据
环境:python 2.7.13 数据库:sqlite3(Django自带) 在学习Django的时候,遇到了困难.大概就是取到数据库数据后一直不能转成json数据.最后终于自己琢磨解决了. 要点就 ...
- H5页面中尝试调起APP
安卓版本5.0以上 IOS版本10.0以上 采用事件触发的方式处理唤醒APP 市面上常见的功能 这种功能现如今应该非常普遍了,淘宝H5,知乎H5等等... 点击后会调起APP或者打开下载页面或者直接进 ...
- ASP.NET Excel导入Sql Server数据库(转)
先看界面图 实现的基本思想: 1,先使用FileUpload控件fuload将Excel文件上传到服务器上得某一个文件夹. 2,使用OleDb将已经上传到服务器上的Excel文件读出来,这里将Exce ...
- 新CCIE笔记-IP网络基础
南京捷式泰CCIE重修笔记:更完善更系统的全新笔记 新增内容: 总结.关联知识点.行业小建议 各种认证证书: RHCE VCP OCP MCSEPMP ITIL CCA CCIE CCNP CCNA ...
- 转:【Java集合源码剖析】Hashtable源码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/36191279 Hashtable简介 Hashtable同样是基于哈希表实现的,同样每个元 ...
- Tomca软件介绍和安装
Web开发入门 软件的结构: C/S (Client - Server 客户端-服务器端) 典型应用:QQ软件 ,飞秋,红蜘蛛. 特点: 1)必须下载特定的客户端程序. 2)服务器端升级,客户端升级 ...
- 【集美大学1411_助教博客】团队作业5——测试与发布(Alpha版本)
同学们好像都进入了状态,任务都完成的不错,测试与发布是一个软件的非常重要的环节,每年双11前夕是阿里巴巴加班最严重的时期,这是因为他们在不断的测试,因为他们不想在双11到来之时有任何差池.所以无论你的 ...
- 201521123093 java 第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123005《java程序设计》第四周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. ·继承(是什么,意义) -父类(被继承的类) -子类(继承父类) -多态(解决重复代码的问题 ...
- 201521123013 《Java程序设计》第10周学习总结
1. 本章学习总结 2. 书面作业 Q1.finally题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? finally块中的异常必须在fina ...