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. 正则表达式备忘(基于JavaScript)

    基于JS学习的正则表达式 备忘 e.g.匹配以0开头的三位或四位区号,以-分格的7或8位电话号码var reg1 = /^0\d{2,3}\-\d{7,8}$/;或var reg1 = new Reg ...

  2. Gateway

    网关在传输层上以实现网络互连,是最复杂的网络互连设备,仅用于两个高层协议不同的网络互连.网关的结构也和路由器类似,不同的是互连层.网关既可以用于广域网互连,也可以用于局域网互连. 网关是一种充当转换重 ...

  3. 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  4. Python 3 mysql 数据类型

    Python 3 mysql 数据类型 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/m ...

  5. Docker 单机网络

    Docker Network相关命令 root@ubuntu:~# docker network --help Usage: docker network COMMAND Manage network ...

  6. 算法(Algorithms)第4版 练习 1.3.4

    主要思路: 遇到左括号则一直压栈,遇到右括号时则从栈中弹出一个元素. 如果此时栈为空,则返回false. 如果这个元素与右括号不匹配,则返回false. 重复此过程,最后判断栈是否为空,若为空则返回t ...

  7. jquery详解图片平滑滚动

    jquery详解图片平滑滚动 随便写了个DOM,没有美观性,见谅 原理: 1.定义两组ul列表放图,第一个ul放5张图,第二个ul为空 2.为什么要用两个ul?因为要用到jQuery的克隆方法clon ...

  8. webpack 教程资源目录

    初级教程 webpack-howto 作者:Pete HuntWebpack 入门指迷 作者:题叶 webpack-demos 作者:ruanyf一小时包教会 —— webpack 入门指南 作者:V ...

  9. linux使用酷我在线听音乐

    一般linux系统自带音频播放器只能管理本地音乐,无法在线听歌.在线音乐如百度音乐盒,下载歌曲需要登录,比较麻烦.在github里有一个酷我音乐的开源项目,可以安装在linux系统下.链接地址:htt ...

  10. Filter/replace - VBA

    Auto filter: ActiveSheet.Range("A:F").AutoFilter Field:=3, Criteria1:="*Agent*" ...