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. Linux环境Perl链接MS Sql Server数据库

    1.下载相关软件 unixODBC.freetds和DBD-ODBC ①.Linux系统的ODBC unixODBC-2.3.4.tar.gz ( http://www.unixodbc.org) ② ...

  2. IOS百度地图之--->第一篇《环境配置与基本使用》

    Ios 百度地图SDK简易使用说明:http://developer.baidu.com/map/index.php?title=iossdk 先道歉:对于原来上传的Demo我很抱歉,什么都没有,也没 ...

  3. WPF: 本地化(Localization) 实现

    本文将讨论一种较为方便的本地化方法. 由于在项目中要实现本地化,所以在网上查找相关的解决方案.通过一系列调研,发现实现本地化的方法主要有以下三种: 通过编译项目以设置 x:Uid 并使用 LocBam ...

  4. Java字节流在Android中的使用

    引言:项目开发有时会使用上传文件到服务器,再从服务器取数据显示到本地这一过程:或者输入一段文字,再把文字显示出来.这个过程都用到了IO流. IO流分为字符流(Reader\Writer)和字节流(In ...

  5. Hibernate的映射文件中基于主键的双向1对1的关联

    1.Hibernate中采用基于主键的映射策略是,有一端(任意一端)的主键生成策略要是foreign,根据对方的主键来生成自己的主键,它的实体不能拥有自己的主键生成策略,如我的配置文件: <?x ...

  6. java封装的方法

    java封装是由Java是面向对象程序设计语言的性质决定的,面向对象程序设计语言的三大特性之一就是封装.封装其实就是包装的意思,从专业的角度来看,就是把对象的所有组成部分组合在一起,保护私有属性. 如 ...

  7. dubbo的简单实现

    一 是什么 一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多,dubbo使前后端分离,完成负载均衡. dubbo架构图 节点角色说明: Provider: 暴露服务的服务提供方 ...

  8. 关于SQL的一些小知识

    在代码中调用存储过程的时,必须先测试存储过程,存储过程测试成功之后再去java中去调用!!@!@#!@!@! 以后自己写的存储过程写一个本地保存一个.!~~~!!(这个很关键) 以后在代码中的SQL都 ...

  9. XJOI1652Matrix67的情书

    Matrix67的情书 恺撒大帝曾经使用过这样一种加密术:对于明文中的每个字母,恺撒大帝会用它后面的第t个字母代替.例如,当t=3时,字母A将变成C,字母B将变成D,--,字母Y将变成A,字母Z将变成 ...

  10. JS中的bind的实现以及使用

    在讨论bind()方法之前我们先来看一道题目: var altwrite = document.write; altwrite("hello"); //1.以上代码有什么问题 // ...