Codeforces 798C gcd思路题


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


题意:n个数,n<=1e5,操作:把ai,ai+1 替换成 a[i]-a[i+1],a[i]+a[i+1],问gcd(a1,a2..an)>1的最少操作次数 

本题最大的难度在于和奇偶性相结合。两个奇数相加减会变成偶数,一个奇数和偶数相加减还是奇数。

若需要执行操作,那么必定会全部变为偶数才行。(就算2个奇数3,9,一个偶数2,想让奇加偶变为两个奇数从而gcd(a1,a2,a3)>1是行不通的,因为3+2后必定不会再与9不互质)


2个相邻奇数变成2个偶数需要1步,相邻的1个奇数和1个偶数变成2个偶数需要2步,所以先变2个相邻奇数的,再变一个奇数一个偶数的。


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<sstream>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int a[100010];
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int n;
    while(cin>>n)
    {
        scanf("%d",&a[1]);
        int x=a[1];
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a[i]);
            x=gcd(x,a[i]);
        }
        if(x>1)
        {
            cout<<"YES"<<endl<<"0"<<endl;
            continue;
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]%2==1&&a[i+1]%2==1)
            {
                int t1=a[i]-a[i+1];
                int t2=a[i]+a[i+1];
                a[i]=t1;
                a[i+1]=t2;
                sum+=1;
            }
        }
        for(int i=1;i<=n;i++)
            if(a[i]%2==1)
        {
            if(i==1)
            {
                int t1=-2*a[i+1];
                int t2=2*a[i];
                a[i]=t1;
                a[i+1]=t2;
                sum+=2;
            }
            else
            {
                int t1=-2*a[i];
                int t2=2*a[i-1];
                a[i-1]=t1;
                a[i]=t2;
                sum+=2;
            }
        }
        cout<<"YES"<<endl<<sum<<endl;
    }


    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值