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.

题解:当$\gcd (x,y)! = 1$时,直接得出答案。

   当$\gcd (x,y) =  = 1$,令$d = \gcd (x - y,x + y)$,

   所以,$d|(x - y),d|(x + y)$

   由信安数基课本P4,$a|b,a|c \to a|tb + sc$,

   $d|2x,d|2y$$\to d|\gcd (2x,2y) \to d|2\gcd (x,y) \to d|2$,$d =  = 1or2$因为若再继续下去,必须满足此等式,故不必继续。

   可以看出最后的d一定整除偶数,所以n个数必须都为偶数.

   所以此题即变为,把n个数变为偶数的最小步数。

   当$a[i]\% 2 =  = 1 ,a[i + 1]\% 2 =  = 1$,步数增加1,

   当a[i]和a[i+1]有一个为偶数时,步数增加2.

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
ll n,a[],t=;
ll gcd(ll a,ll b){
while(b){
ll temp=b;
b=a%b;
a=temp;
}
return a;
}
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
t=gcd(t,a[i]);
}
for(int i=;i<n;i++){
a[i]%=;
}
if(t!=){
cout<<"YES\n0\n";
return ;
}
ll ans=;
for(int i=;i<n;i++){
if(a[i]){
ans++;
if(!a[i+]){
ans++;
}
a[i]=a[i+]=;
}
}
cout<<"YES\n"<<ans<<endl; }

#410div2C. Mike and gcd problem的更多相关文章

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

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

  2. CF798 C. Mike and gcd problem

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

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

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

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

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

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

  7. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  8. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

  9. codeforces798C - Mike and gcd problem (数论+思维)

    原题链接:http://codeforces.com/contest/798/problem/C 题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的.存在这 ...

随机推荐

  1. SpringBoot学习笔记(4):与前端交互的日期格式

    SpringBoot学习笔记(4):与前端交互的日期格式 后端模型Date字段解析String 我们从前端传回来表单的数据,当涉及时间.日期等值时,后端的模型需将其转换为对应的Date类型等. 我们可 ...

  2. GIT笔记:将项目发布到GITHUB

    GIT笔记:将项目发布到GITHUB 本机配置 1.在项目目录初始化GIT $ git init 2.用命令git add告诉Git,把文件添加到仓库 $ git add . // 这里是所有文件,用 ...

  3. Idea 包名按树形结构展示

    Idea默认包名展示如图: 感觉这样展示,在包下面建包的时候不方便,可以在 设置按钮 里面去掉 Flatten Packages 和 Compact Empty Middle Packages,设置如 ...

  4. MS-SQL charindex的用法

    select * from table_a where charindex('a',id)>0 or charindex('b',id)>0 table_a 表中 id字段中含有" ...

  5. 数据分析第三篇:Numpy知识点

    Numpy 将字符型数据转为datetime import numpy as np f = np.array([','2019-01-01','2019-01-02 01:01:01']) # 把f数 ...

  6. Java Applet:练习TextField类和TextArea类的文本事件

    出自: Java语言与面向程序程序设计(第二版) 第七章第五节P223 1. [代码]java代码 /** *这个程序主要来练习文本事件,当在文本框(TextField)中输入文字时,会在文本域(Te ...

  7. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  8. sqlserver 实现数据库全文检索

    --在执行该脚本程序之前启动sql server的全文搜索服务,即microsoft search服务 use huarui_db --打开数据库 go --检查huarui_db是否支持全文索引,如 ...

  9. 双系统重装win7和ubuntu修复win7引导方法介绍(来源百度经验)

    很多朋友喜欢为电脑安装win7和ubuntu双系统,当我们重装双系统时,可能会出现win7引导不见的情况,接下来就告诉大家双系统重装win7和ubuntu修复win7引导的方法. 1.win7和ubu ...

  10. 造成segmentation fault的可能原因分析

    一 造成segment fault,产生core dump的可能原因 1.内存访问越界 a) 由于使用错误的下标,导致数组访问越界 b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串 ...