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
  1. 2
    1 1
output
  1. YES
    1
input
  1. 3
    6 2 4
output
  1. YES
    0
input
  1. 2
    1 3
output
  1. 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.

题目链接:http://codeforces.com/contest/798/problem/C

题意:一次操作中可以选择i (1 ≤ i < n),删除ai, ai + 1添加 ai - ai + 1, ai + ai + 1到相同的位子。尽可能的操作少的数量使得a数组的最大公约数大于1。注意

思路:ai - ai + 1,ai + ai + 1两个数相差2*ai + 1,2*ai + 1为它们之间最大公约数的倍数,即它们之间的最大公约数为2*ai + 1的约数。并且它们的最大公约数不可能为ai + 1的约数,否则ai,ai+1之间的最大公约数大于1,不需要进行操作。那么操作之后的a数组,之间的最大公约数为2。奇偶之间需要2次操作,奇奇之间需要1次操作,偶数直接跳过。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<map>
  7. #include<vector>
  8. #include<queue>
  9. using namespace std;
  10. typedef long long ll;
  11. const int maxn=1e5+,inf=0x3f3f3f3f,mod=1e9+;
  12. const ll MAXN=1e13+;
  13. int n,a[maxn];
  14. int main()
  15. {
  16. scanf("%d",&n);
  17. for(int i=; i<=n; i++) scanf("%d",&a[i]);
  18. int sign=a[];
  19. for(int i=; i<=n; i++) sign=__gcd(sign,a[i]);
  20. if(sign>)
  21. {
  22. cout<<"YES"<<endl<<<<endl;
  23. return ;
  24. }
  25. int ans=;
  26. for(int i=; i<=n; i++) a[i]=a[i]%;
  27. for(int i=; i<=n; i++)
  28. {
  29. if(a[i]==) continue;
  30. if(i==n) ans+=;
  31. if(i+<=n)
  32. {
  33. if(a[i+]) ans+=;
  34. else ans+=;
  35. a[i+]=;
  36. }
  37. }
  38. cout<<"YES"<<endl<<ans<<endl;
  39. return ;
  40. }

gcd

Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1的更多相关文章

  1. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

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

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

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

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

  4. 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. 思 ...

  5. Codeforces 791C. Bear and Different Names 模拟构造

    C. Bear and Different Names time limit per test:1 second memory limit per test:256 megabytes input:s ...

  6. CodeForces 689A Mike and Cellphone (模拟+水题)

    Mike and Cellphone 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/E Description While sw ...

  7. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

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

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

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

随机推荐

  1. 线上问题!----------org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe

    1.问题出现 昨晚项目在上线的时候因为推广的原因,新增的大量请求.在八点的时候. org.apache.catalina.connector.ClientAbortException: java.io ...

  2. maven自动部署Tomcat错误排除

    转自:https://blog.csdn.net/wuha0/article/details/18658113 在Maven与Tomcat配合部署过程中,最常见的错误有三种,折腾了半天,终于找到三种错 ...

  3. java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1) for method ApiService.getMethod

    @FormUrlEncoded @POST("getMethod") Observable<Bean> getMethod(String field); 今天在写Ret ...

  4. React设置宽度的坑

    [React设置宽度的坑] 我们知道通过ref可以获取DOM元素,通过style属性可以给此DOM元素添加样式. 但下面两行的赋值是无效的: this.HomeRootDiv.style.width= ...

  5. mybatis做if 判断 传入值0 建议最好不要使用值0

    mybatis做if 判断 注意:下面这种写法只适用于 id 类型为字符串. <if test="id != null and id != '' ">     id = ...

  6. sql注入(一)

    SELECT * FROM users WHERE user='uname' AND password='pass' SELECT * FROM users WHERE user='name' AND ...

  7. 一、Iterator 迭代器

    需求:如何不适用for循环,依次遍历出数组中每个元素? 设计原理: 代码清单: 接口类 public interface Iterator { boolean hasNext(); Object ne ...

  8. NAT和Proxy的区别

    在internet共享上网技术上,一般有两种方式,一种是proxy代理型,一种是NAT网关型,关于两者的区别与原理,身边很多人都不是很明白,下面我来讲讲我的理解,如有不对的,欢迎指正. 1.先说应用例 ...

  9. 相对熵(KL散度)

    https://blog.csdn.net/weixinhum/article/details/85064685 上一篇文章我们简单介绍了信息熵的概念,知道了信息熵可以表达数据的信息量大小,是信息处理 ...

  10. php中bootstrap框架.popover弹出框,鼠标移动到上面自动显示,离开自动消失

    <div rel="name"></div> <script> $(function(){//显示弹出框 $("[rel=name]& ...