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.

Example

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.

题意:n个数,n<=1e5,操作:把a[i],a[i+1] 替换成 a[i]-a[i+1],a[i]+a[i+1],问至少要多少次操作才能让整个a数组的最大公约数gcd大于1.

由题目给出操作可知:当gcd(a,b)<=1时,进行操作为:

初始:a  b

第一步:a-b  a+b

第二步:-2b  2a

即两个数最多2步操作就能满足GCD==2。

对于两个偶数,要进行0步操作;对于两个奇数,要进行1步操作;对于一个奇数一个偶数,要进行2步操作。

先把所有“2个奇数成对”的情况计数+1并把两个奇数更新为偶数,然后在重新判断所有“1个奇数1个偶数成对”的情况计数+2。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200050],n,num=0;
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
ll ans=gcd(abs(a[1]),abs(a[2]));
for(int i=3;i<=n;i++)\
ans=gcd(ans,abs(a[i]));
if(ans>1) cout<<"YES"<<endl<<0<<endl;
else
{
for(int i=1;i<n;i++)
if(a[i]%2&&a[i+1]%2)
a[i]=0,a[i+1]=0,num++;
for(int i=1;i<n;i++)
if((a[i]%2&&a[i+1]%2==0)||(a[i]%2==0&&a[i]%2))
a[i]=0,a[i+1]=0,num+=2;
cout<<"YES"<<endl<<num<<endl;
}
return 0;
}

CodeForce-798C Mike and gcd problem(贪心)的更多相关文章

  1. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

  2. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  3. codeforces 798c Mike And Gcd Problem

    题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...

  4. codeforces 798C.Mike and gcd problem 解题报告

    题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...

  5. CF798 C. Mike and gcd problem

    /* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...

  6. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  7. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  8. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

  9. #410div2C. Mike and gcd problem

    C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  10. Mike and gcd problem CodeForces - 798C (贪心思维+数论)

    题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...

随机推荐

  1. 害...原来阿里面试Redis最常问的是它呀

    一提到Redis缓存,我们不得不了解的三个问题就是:缓存雪崩.缓存击穿和缓存穿透.这三个问题一旦发生,会导致大量的请求直接请求到数据库层.如果并发压力大,就会导致数据库崩溃.那p0级的故障是没跑了. ...

  2. netty系列之:对聊天进行加密

    目录 简介 PKI标准 各类证书的后缀和转换 netty中启动SSL server netty中启动SSL client 总结 简介 在之前的文章中,我们讲到了怎么使用netty建立聊天室,但是这样的 ...

  3. 授予mysql的其他用户数据库的使用权限

    场景:不同的开发人员有不同的数据库的权限:也可适用于外包公司不同的开发权限. root用户登录数据库,命令行执行下面语句即可. grant select,delete,update,create,dr ...

  4. Python语言系列-10-数据库

    MySQL 基础环境准备 readme.txt 作者:Alnk(李成果) 版本:v1.0 安装mysql数据库 略 创建student库 # mysql> create database stu ...

  5. GIT-版本管理-初阶使用

    目前我用的比较多的git指令 git clone -b [branch-name]: 拉取远程指定分支代码 git status: 查看工作区修改状态 在将修改添加进暂存区的时候需要git add . ...

  6. 如果服务器数据更新了,CDN的数据是怎么及时更新的

    A:cdn一般用来存静态资源.拿网站来说,当用户访问网站时静态资源从cdn加载.cdn向后段源服务器请求资源并缓存,这个请求过程是周期性的,自动的,称为回源. 当你更新了一个文件,现在正巧还没到cdn ...

  7. 对集合使用Comparator

    1 import java.util.Comparator; 2 import java.util.PriorityQueue; 3 4 /** 5 * 对集合使用Comparator,不改变对象的自 ...

  8. Java的GUI组件的布局管理器

    1 import java.awt.BorderLayout; 2 import java.awt.FlowLayout; 3 import java.awt.Font; 4 import java. ...

  9. windows下mysql5.7.17配置

    1.官网下载mysql5.7.17 64位 https://dev.mysql.com/downloads/mysql/ 2.安装完解压到E盘主目录下,改文件名为mysql 3.配置环境变量 我的电脑 ...

  10. Promise.all()

    语法:Promise.all(iterable); 参数:iterable 一个可迭代对象,如 Array 或 String. 返回值:如果传入的参数是一个空的可迭代对象,则返回一个已完成(alrea ...