G - Mike and gcd problem

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 + 1in 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 Abeautiful by performing operations described above, and "NO" (without quotes) otherwise.

If the answer was "YES", output the minimal number of moves needed to make sequenceA beautiful.

Example

Input
21 1

Output
YES1

Input
36 2 4

Output
YES0

Input
21 3

Output
YES1题意:输入n个数 (2 ≤ n ≤ 100 000),操作:把a[i],a[i+1] 替换成 a[i]-a[i+1],a[i]+a[i+1],问最少多少次操作使所有元素的gcd>1。
题解:假设两个数a,b。操作一次a-b,a+b. 操作两次 -2b,2a。gcd=2;所以任意两个数两次操作后gcd一定>1;当两个数是偶数时,需要0次操作当两个是奇数时,需要1次操作当一奇一偶时,需要2次操作先循环一遍两个都是奇数的,然后把这两个数更改为偶数,操作次数+1;在循环一遍一奇一偶成对的,然后把这两个数更改为偶数,操作次数+2;
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
long long gcd(long long a,long long b)
{if(b==0)return a;elsegcd(b,a%b);
}
using namespace std;
int main()
{long long a[100005],ans;int n,i,num=0;cin>>n;for(i=1;i<=n;i++)cin>>a[i];ans=gcd(a[1],a[2]);for(i=3;i<=n;i++)ans=gcd(ans,a[i]);if(ans>1)cout<<"YES"<<endl<<0<<endl;else{for(i=1;i<n;i++){if(a[i]%2&&a[i+1]%2){a[i]=0; a[i+1]=0; num++;}}for(i=1;i<n;i++){if(a[i]%2==0&&a[i+1]%2==1){a[i]=0; a[i+1]=0; num+=2;}else if(a[i]%2==1&&a[i+1]%2==0){a[i]=0; a[i+1]=0; num+=2;}elsecontinue;}cout<<"YES"<<endl<<num<<endl;}
}

转载于:https://www.cnblogs.com/GXXX/p/6814992.html

G - Mike and gcd problem相关推荐

  1. Codeforces 798C:Mike and gcd problem

    Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...

  2. 【Codeforces - 798C】 Mike and gcd problem(思维,贪心)

    题干: Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, .. ...

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

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

  4. Mike and gcd problem(思维)

    Mike has a sequence A = [a1, a2, -, an] of length n. He considers the sequence B = [b1, b2, -, bn] b ...

  5. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  6. 牛客多校第四场【B-Basic Gcd Problem】

    牛客多校第四场[B-Basic Gcd Problem] 题目链接:https://ac.nowcoder.com/acm/contest/5669/B 思路:先要理解公式,多看几个数据基本就会有点想 ...

  7. CodeForces798cMike and gcd problem

    Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., b ...

  8. 牛客多校4 - Harder Gcd Problem(构造+贪心)

    题目链接:点击查看 题目大意:给出一个 n ,表示 1 ~ n 的 n 个数字,现在要求选出尽可能多的两两匹配,使得每组匹配的 gcd 都大于 1,输出最多能有多少组匹配,以及方案 题目分析: 这样的 ...

  9. 2020牛客暑期多校训练营(第四场)H.Harder Gcd Problem(把1到n分为不互质的数对,找最多的对数)

    题目大意:把1到n分为不互质的数对,找最多的对数 思路:先从最大的质因数开始找,因为小的比大的更容易匹配,所以贪心的从大的开始找. 首先要预处理出所以数的最大质因数. 然后根据质因数从大往小找,当质因 ...

最新文章

  1. matlab编程实现基于密度的聚类(DBSCAN)
  2. \\ n和\\ r之间的区别?
  3. mysql集群的配置
  4. C#3.0笔记(五)Lambda表达式
  5. Spring的控制反转以及依赖注入,控制反转使程序具有高拓展性。
  6. axios获取图片显示_vue中使用axios post上传头像/图片并实时显示到页面的方法
  7. 培训完java后想放弃_10个创意修补案例,改造后漂亮极了,看完让人想动手试试...
  8. 音视频编解码的一些源代码
  9. 如何获取option的下标和值_数智化时代下,如何获取企业增长密码?
  10. insert select 过滤掉重复数据
  11. offset must be non-negative and no greater than buffer length (0)(tensorflow.keras)
  12. winpe加载raid_WinPE如何添加RAID驱动|WinPE添加RAID驱动教程
  13. Ubuntu安装、更新显卡驱动
  14. appium学习总结5 - 操作设备
  15. java日志框架JUL、JCL、Slf4j、Log4j、Log4j2、Logback 一网打尽
  16. FL Studio水果2023版本更新下载汉化教程
  17. import java.util.* cannot be resolved或import java.io.*cannot be resolved解决方案
  18. 《正本清源区块链》课程分享裂变运营复盘
  19. 8. Spring Security 5.1之 OAuth 2.0 Login
  20. ArUco钻石标记的检测

热门文章

  1. java空指针避坑指南
  2. 组织敏捷之路上的七点体会
  3. qt 初学 创建一个简单的计算器
  4. 启帆工业机器人综合收入如何_发那科工业机器人ROBOGUIDE如何更方便的查看机器人报警日志...
  5. pagerank算法实现matlab,Matlab 入门及PageRank算法求解.ppt
  6. 关于 To B,我欠粉丝们一个热搜
  7. 【干货下载】大数据分析——如何消除金融不确定性
  8. 新手入门 如何快速找到Python进阶路线?
  9. 教你如何将自己的脚本代码放到服务器上运行
  10. 耳鼻喉专科服务机构“仁树医疗”完成数千万元A轮融资...