您的位置:首页 > 其它

分析奇数与偶数之间的关系

2017-04-22 12:52 239 查看
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
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.

刚拿到这道题目简直就是全程懵逼,像这种一眼看上去就是数论的题我是无能为力。但这道题考的却不是艰难的数论知识,而是一种分析问题的能力,就像昨晚学长说的,我们不能只做水题,而是多做那种可以锻炼我们分析能力的题目,这是我们做题的目的。

我们可以发现,对a[i]和a[i+1]执行两遍之后,会变成-2*a[i+1],2*a[i],我们会发现,这两个都变成了偶数,而两个偶数的最大公约数是大于等于2的,符合题目要求。那么我们是不是让所有a[i]变成偶数不就可以了,但这只是我们根据自己的分析瞎想的解题方法。那我们就来验证一下这是不是最优解。因为a[i]和a[i+1]两个变成偶数只需要两步即可,那么要想有更好的方法就必须一步!就这个题目来说,很显然,不可能的。所以,这是最优的方案。代码自然就敲出来了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int re[100005];
int gcd(int x,int y)
{
int a,b,c;
a=abs(x);
b=abs(y);
while(b)
{
c=a%b;
a=b;
b=c;
}

return a;
}

void solve(int x,int y)
{
int temp1=re[x]-re[y];
int temp2=re[x]+re[y];

re[x]=temp1;
re[y]=temp2;
}

int main()
{
cin>>n;
int i,j;
for(i=1;i<=n;i++)
cin>>re[i];

int gcdcommen=gcd(re[1],re[2]);
for(i=3;i<=n;i++)
{
gcdcommen=gcd(gcdcommen,re[i]);
}

cout<<"YES"<<endl;
if(gcdcommen>1)
{
cout<<0<<endl;
return 0;
}
int step=0;
for(i=1;i<n;i++)
{
while(re[i]%2 != 0)
{
solve(i,i+1);
step++;
}
}

if(re
%2 != 0)
{
while(re
%2 != 0)
{
solve(n-1,n);
step++;
}
}

cout<<step<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: