Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门

Description

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.

Sample Input

21 1

21 3
36 2 4

Sample Output

YES1
YES1
YES0

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.

思路

题解:

= =想了很久,虽然快接近思路,但是。。就差那么一点啊。。哎。。

First of all, the answer is always YES.

If  then the answer is 0.

Now suppose that the gcd of the sequence is 1. After we perform one operation on ai and ai + 1, the new gcd d must satisfy d|ai - ai + 1and d|ai + ai + 1  d|2ai and d|2ai + 1. Similarly, because d is the gcd of the new sequence, it must satisfy d|aj, j ≠ i, i + 1.

Using the above observations we can conclude that , so the gcd of the sequence can become at most 2 times bigger after an operation. This means that in order to make the gcd of the sequence bigger than 1 we need to make all numbers even. Now the problem is reduced to the following problem:

Given a sequence v1, v2, ... , vn of zero or one,in one move we can change numbers vi, vi + 1 with 2 numbers equal to . Find the minimal number of moves to make the whole sequence equal to 0.

It can be proved that it is optimal to solve the task for consecutive ones independently so we divide the array into the minimal number of subarrays full of ones, if their lengths are s1, s2, ... , st,the answer is .

Complexity is .

#include<bits/stdc++.h>
using namespace std;

int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}

int main()
{
	int n,res = 0,resgcd = 0,cnt = 0,tmp;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%d",&tmp);
		resgcd = gcd(resgcd,tmp);
		if (tmp&1)	cnt++;
		else	res += (cnt/2) + 2*(cnt&1),cnt = 0;
	}
	res += (cnt/2) +2*(cnt&1);
	printf("YES\n");
	if (resgcd > 1)	printf("0\n");
	else	printf("%d\n",res);
	return 0;
}

  

时间: 2024-10-15 17:19:21

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)的相关文章

Codeforces Round #410 (Div. 2)-A - Mike and palindrome

题目简述:给定一个字符串判断是否能够恰好改变一个字符使其变成回文(回文:eg.abcba或abccba). 注意:1,是恰好改变一个字符,恰好! 2,字符串例如"abc"这样的奇数个数的而且是完整回文的字符串也是可以恰好改变一个字符变成回文的(改变最中间的那个字符,中间的那个字符改成什么都是回文) 我当时就卡死在这个坑. 我的代码是用栈做的虽然有点蠢...当时我都不知道for居然可以放两个入口变量,那样的话i++,j--就成了,我着急直接用栈,我居然不知道栈没有迭代器,也没注意上述注意

[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings

1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 using namespace std; 4 const int maxn=55; 5 string s[maxn]; 6 int main() 7 { 8 int n; 9 cin>>n; 10 for(int i=0;i<n;i++){ 11 cin>>s[i]; 12 } 13 int ans=inf,tem; 14 for(int i=0;i<s

Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt与k的组合数) ans=ans+(len*C(cnt,k))%mod; #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorith

暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

题目传送门 1 /* 2 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 5e2 + 10; 12 const int

set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

题目传送门 1 /* 2 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 3 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 4 查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错! 5 详细解释:http://blog.csdn.net/u010660276/article/details/46045777 6 其实还有其他解法,先掌握这种:) 7 */ 8 #include <cstd

数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

题目传送门 1 /* 2 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 3 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 4 详细解释:http://blog.csdn.net/u014357885/article/details/46044287 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #in

字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

题目传送门 1 /* 2 字符串处理:回文串是串联的,一个一个判断 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 1e3 + 10; 12 const int INF = 0x3f3

Codeforces Round #305 (Div. 1) B. Mike and Feet

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is h1 and height of Abol is