码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 798C Mike and gcd problem

时间:2017-05-05 21:44:05      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:with   its   for   long   type   约数   数字   sts   out   

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

解题思路:

题意为:输入给定数字,求他们的大于1的最大公约数,如果没有则进行操作

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

由上可知,不论两个数字为什么,最多操作两次就有公约数2

那么有三种情况:1.都为偶数,不需要进行操作。2.一个为奇数一个为偶数,需要操作两次,3.都为奇数,只需操作一次;

然后随便打下就行了

实现代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

int main()
{
    ll m,a[100005],i,ans,num;
   cin>>m;
   num = 0;
   for(i=1;i<=m;i++)
    cin>>a[i];
   ans = gcd(abs(a[1]),abs(a[2]));
   for(i=3;i<=m;i++)
    ans = gcd(ans,abs(a[i]));
   if(ans>1)
    cout<<"YES"<<endl<<"0"<<endl;
   else
   {
       for(i=1;i<m;i++)

           if(a[i]%2&&a[i+1]%2)
           {
               a[i]=0;a[i+1]=0;num++;
           }
        for(i=1;i<m;i++)
           if((a[i]%2==0&&a[i+1]%2)||(a[i]%2&&a[i+1]%2==0))
            {
               a[i]=0;a[i+1]=0;num+=2;
            }

       cout<<"YES"<<endl<<num<<endl;
   }
   return 0;
}

 

codeforces 798C Mike and gcd problem

标签:with   its   for   long   type   约数   数字   sts   out   

原文地址:http://www.cnblogs.com/kls123/p/6814862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!