题干:
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 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.
题目大意:
给定一个n个元素的数组,求最少几次操作可以使 gcd( a1, a2,… ,an )>1 。
定义一次操作是:将删掉,并且加入这两个数。
解题报告:
首先可以证明:如果你开始操作了,那最终一定要凑出2的倍数。如果公因子d是非2的数,那必须是初始序列。
证明如下:运用反证法,对于任意的a和b,操作之后变成a+b和a-b,假设这个公因子d不是2,最终推出矛盾,也就是证明出g必须为2.
首先需要保证d|(a+b) 且 d|(a-b)。(设为式①和式②)
设公因子是d,则a=k1*d+c1 , b=k2*d+c2(且0<c1,c2<d)。带入式①和式②,得d|(c1+c2),且d|(c1-c2).(设为式③和式④)
联立,得c1=c2,所以d|(2*c1),所以d肯定能被2整除。
证毕。
所以我们要通过操作把所有的数字都变成偶数。然后就贪心搞一搞就好了,记录一下有多少个连续的奇数。
因为你会发现
对于两个偶数来说,不需要操作,
对于两个奇数,通过一次操作即可。
对于一奇一偶,通过两次操作即可。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,a[MAX],g,cnt,ans;
int main()
{cin>>n;for(int i = 1; i<=n; i++) cin>>a[i],g=__gcd(a[i],g);puts("YES");if(g==1) {for(int i = 1; i<=n; i++) {if(a[i]%2==1) cnt++;else {ans += cnt/2+(cnt%2==1?2:0);cnt=0;} }ans += cnt/2+(cnt%2==1?2:0);}printf("%d\n",ans);return 0 ;
}