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).
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 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.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
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;
}