C. Mike and gcd problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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).

Input

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

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.

Examples
input
2
1 1
output
YES
1
input
3
6 2 4
output
YES
0
input
2
1 3
output
YES
1
Note

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.

题意:给一串数,求gcd,如果gcd==1,那么可以通过改变a[i],a[i+1],变成a[i]-a[i+1],a[i]+a[i+1],求最小改变次数

题解: 比赛时没做出来,一直以为要用gcd模拟!(真是越来越蠢,忘了数论题推公式很重要了!)

证明:设d是改变后的gcd,d|a[i]-a[i+1],d|a[i]+a[i+1],得d|2*a[i],d|2*a[i+1],

那么d|gcd(a[0],...2*a[i],2*a[i+1],...,a[n-1]),d|2*gcd(a[0],...a[i],a[i+1],...,a[n-1])

而gcd(a[0],...a[i],a[i+1],...,a[n-1])==1,则d==2;

最后通过把每个数%2计算总和

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; ll n,a[N],ans=; ll gcd(ll a,ll b)
{
return b? gcd(b,a%b):a;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n;
ll ans=,x;
for(int i=;i<n;i++)
{
cin>>a[i];
if(i==)x=a[i];
else x=gcd(x,a[i]);
a[i]%=;
}
if(x>)
{
cout<<"YES"<<endl<<<<endl;
return ;
}
for(int i=;i<n;i++)
{
if(a[i]==)
{
if(i+<n)
{
if(a[i+]==)ans++,a[i+]=;
else if(a[i+]==)ans+=;
a[i]=;
}
else ans+=;
}
}
cout<<"YES"<<endl<<ans<<endl;
return ;
}

Codeforces Round #410 (Div. 2)C题的更多相关文章

  1. Codeforces Round #410 (Div. 2)D题

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  3. Codeforces Round #410 (Div. 2)

    Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...

  4. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  5. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  6. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  7. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  8. Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)

    A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  9. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

随机推荐

  1. php函数的使用

    <?php header("Content-type:text/html; charset=utf-8"); //普通函数 echo "<br/>--- ...

  2. Docker 跟 NodeJs 最佳实践

    Level-1 简单实现 需求:简单的构建一个app应用并且用docker部署.Dockerfile编写为: FROM node:7.3.0 RUN mkdir -p /usr/src/app COP ...

  3. SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J

    我们前面已经讲过了 SLF4J 的两种用法:SLF4J+Log4J  和 SLF4J+Logback,那是在比较理想的情况下,所用组件只使用了 SLF4J 这一种统一日志框架的时候.可是 JCL 一直 ...

  4. 用Java实现将多级文件夹下的所有文件统一放到一个文件夹中

    每次下了电影(男生懂得呦),每部电影都放在一个单独的文件夹里,看的时候很是不方便啊,一直重复着进入文件夹.后退,再进.再退的操作,而手动把这些电影全部复制出来又太繁琐.因此为了解决这个问题,用IO写了 ...

  5. css3 loading

    http://jsbin.com/vonuni/2/edit?html,css,output

  6. PT20150801隆重开班

    PT20150801隆重开班 伴随着秋天的的脚步,带着对梦想的憧憬,POPTEST1508期学员步入正式学习阶段:POPTEST的课程内容吸收了互联网公司先进技术的特点,同时坚持深入浅出的教育特点,完 ...

  7. Android线程池使用终结版

    有一段时间没写博文了,今天抽空总结一下,也希望能通过自己写的这些文章,加深理解的同时能帮 助在技术方面有疑点的朋友搞清楚个所以然来,由于经常会在网上或群里看到有朋友会问线程方面的 东西,就像我一个朋友 ...

  8. DC平衡双向控制解串器 转接IC GM8914:FPD-LINK III转LVTTL芯片

    1 概述 GM8914型DC平衡双向控制解串器,其主要功能是实现将2.8Gbps高速串行数据转换成10或12位并行控制信号,并同步输出一路时钟信号:同时低速通道将芯片控制信息调制到高速差分信号上传输给 ...

  9. 一个GOOD的Idea需要伯乐发觉-致敬错过的IDEA

    类似一个微信电话本

  10. Eclipse中Maven插件部分常用功能命令介绍

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6689010.html Eclipse中安装Maven插件之后,就能很方便的管理Maven项 ...