Mike and gcd problem

time limit per test 2 second memory limit per test 256 megabytes
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).

传送门:CF798C

Input

The first line contains a single integer n (2≤n≤100000) — 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.

Sample Input

1
2
2
1 1

Sample Output

1
2
YES
1

题解

这道题出题人很心机啊= =
并不会出现变不到的情况
然后首先扫一遍看公共gcd是否大于1,大于1直接就满足了
等于1,我们开始找规律 奇奇变一次是偶偶 满足 奇偶变两次才能变成偶偶
当然 其实可以这样想 如果是奇奇奇 那么前面变一次成了 偶偶奇 这时候最后一个奇可以和前面的偶数变换两次 成偶偶偶
所以操作数就是看所有连续的奇数
一段中连续奇数个数是偶数就只要变长度/2的次数,是奇数就长度/2+2

AC code:(不包含输入类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;  
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
while(sc.hasNext()){
int n=sc.nextInt();
int[]shu=new int[n];
for(int i=0;i<n;i++) shu[i]=sc.nextInt();
long g=gcd(shu[0],shu[1]);
for(int i=2;i<n;i++){
g=gcd(g,shu[i]);
}
if(g>1){
pw.println("YES");
pw.println(0);
}else{
ArrayList<Integer>list=new ArrayList<Integer>();
boolean flag=true;
int min=0;
for(int i=0;i<n;i++){
if(shu[i]%2!=0){
list.add(shu[i]); //其实只要一个int变量计数就可以了
}else{
if(list.size()%2==0){
min+=list.size()/2;
}else{
min+=list.size()/2+2;
}
list.clear();
}
}
if(list.size()>0){
if(list.size()%2==0){
min+=list.size()/2;
}else{
min+=list.size()/2+2;
}
}
pw.println("YES");
pw.println(min);
}
pw.flush();
}
}
public static long gcd(long a,long b){
return a == 0 ? b : gcd(b % a, a);
}
}