code force 798cMike 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?+?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.

Example

Input

21 1

Output

YES1

Input

36 2 4

Output

YES0

Input

21 3

Output

YES1

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.

第一行一个n,代表n个数字输入,第二行输入数字,你可以对第i个数和第i+1执行一种交换操作,即用ai,?ai?+?1 替代ai?-?ai?+?1,?ai?+?ai?+?1

求最少进行多少次操作,能使该序列的最大公共公因数>1,这里题目是绝对有解的,不需考虑no的情况

对于数字a,b进行操作:  a,b   a-b,a+b  -2b,2a

由此题目就容易了,2是最容易想到的最大公因数,而进行操作又可以令无论奇偶的两个数都变成偶数

那么对数组进行访问

1.假如第i个数和第i+1个数都为偶数,操作次数为0(不产生影响,不用判断)

2.假如第i个数和第i+1个数都为奇数,操作次数为1

3.假如一个奇数一个偶数,操作次数为2

但这里就有一个问题了,2和3之间存在冲突,并且2几乎无法触发。而题目求的是最少操作数量,那么显然2是优于3的,所以在处理时应给2更高的优先级。因此可以对数组进行两次访问,第一次执行操作2,第二次执行操作3。

另外,题目有一个要注意的地方是在交换前最大公共公因数已经符合条件的话,可以直接结束,代码如下

#include<stdio.h>
#define MAX 100000
int gcd(int s1,int s2)
{
 int r;
 while (s2!=0)
    {
  r=s1%s2;
  s1=s2;
     s2=r;
  }
  return s1;
}
int main()
{
 int n,i,j,a[MAX],t,ans;
 while(scanf("%d",&n)!=EOF)
 {
  t=0;
  for(i=1;i<=n;i++)
  {
   scanf("%d",&a[i]);
  }
  ans=gcd(a[1],a[2]);
  for(i=3;i<=n;i++)
  ans=gcd(ans,a[i]);
  if(ans>1)
  printf("YES\n0\n");
  else
  { 
   ans=0;
   for(i=2;i<=n;i++)
   {
    if(a[i-1]%2&&a[i]%2)
    {
     ans++;
     a[i-1]=0;
     a[i]=0;
    }
   }
   for(i=2;i<=n;i++)
   {
    if(a[i-1]%2==0&&a[i]%2||a[i-1]%2&&a[i]%2==0)
    {
     ans+=2;
     a[i-1]=0;
     a[i]=0;
    }
   }
   printf("YES\n%d\n",ans);
  }
 }
}
时间: 2024-12-20 12:45:46

code force 798cMike and gcd problem的相关文章

2.7 编程之美--最大公约数的3种解法[efficient method to solve gcd problem]

[本文链接] http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html [题目] 求两个正整数的最大公约数Greatest Common Divisor (GCD).如果两个正整数都很大,有什么简单的算法吗?例如,给定两个数1 100 100 210 001, 120 200 021,求出其最大公约数. [解法] [1. 辗转相除法] 辗转相除法:f(x,y) = f(y , x % y)(x>y

G - Mike and gcd problem

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 be

codeforces 798C Mike and gcd problem

C.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 beau

CF Round410 C. Mike and gcd problem

C. Mike and gcd problem 一奇一偶需要两次操作,两个奇数需要一次操作. 798D - Mike and distribution In the beginning, it's quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?a

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

C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 8 using namespace std; 9 const

【GCD PROBLEM】CF 16.C——Monitor

SD五一赛中也出了这个问题. 来源:点击打开链接 改成了多组样例. 思路还是很简单的.给出一个最大长和宽,还有推荐比例,求解符合比例的图形中面积最大的情况.这样的话,先对比例进行约分.然后根据比例式的性质,交叉相乘比一下大小,在思维不混乱的情况下,可以看出比例长度大于实际长度的时候,合法结果不存在.y:比例y>x:比例x的时候,保留X部分为最大值,反之保留Y的宽度为最大值. 思维不要乱. #include <iostream> #include <cmath> #includ

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门 Description 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.

Google Code Jam Round 1A 2015 Problem B. Haircut 二分

Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barbers on duty, and they are numbered 1 through B. It always takes the kth barber exactly Mk minutes to cut a customer's hair, and a barber can only cut

798C - Mike and gcd problem

题意 通过将一组序列中 ai与ai+1 变为 ai-ai+1 与ai+ai+1 的操作将这组序列的gcd变成不为1. 看了题解才会写== ,所以叫做补提嘛QWQ,当d|a && d|b 时 d|ax+by ,即 d|ai-ai+1 d|ai+ai+1 时,可得 d|2ai, d|2ai+1 从而新序列的gcd一定为2,所以先求出所有数字的gcd(因为我太菜的原因不知道算gcd的复杂度其实不高,log(max(a,b))这样,如果不等于1,直接输出0. 然后再贪心扫两遍就行. #includ