CF-798C
C. Mike and gcd problemtime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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).
InputThe 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.
OutputOutput 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.
Examplesinput2
1 1outputYES
1input3
6 2 4outputYES
0input2
1 3outputYES
1NoteIn 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.
题意:
对于给定字符串,我们可将其相邻的两个字符做以下操作:
num[i],num[i+1] -> num[i]-num[i+1],num[i]+num[i+1]
由此可得,变换两次得:-2num[i+1],2num[i]
因为所有数均可转换为偶数,所以结果不可能为“NO”。
当相邻两数均为奇数时,只进行一次变换就可将它们全部变换为偶数;
当相邻数一奇一偶时,只要进行两次就可转换为偶数。
AC代码:
#include<bits/stdc++.h>
using namespace std; long long num[];
int n; int gcd(long long a,long long b){
if(b==){
return abs(a);
}
return gcd(b,a%b);
} int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>num[i];
}
long long ans=;
for(int i=;i<n;i++){
ans=gcd(ans,num[i]);
}
if(ans>){
cout<<"YES"<<endl<<<<endl;
return ;
}
ans=;
for(int i=;i<n-;i++){
if(num[i]&&&num[i+]&){
ans++;
num[i]=;
num[i+]=;
}
}
for(int i=;i<n;i++){
if(num[i]&){
ans+=;
}
}
cout<<"YES"<<endl<<ans<<endl; return ;
}
CF-798C的更多相关文章
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
随机推荐
- linux c语言 select函数使用方法
linux c语言 select函数使用方法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<un ...
- Vue.directive 自定义指令
一.什么是全局API? 全局API并不在构造器里,而是先声明全局变量或者直接在Vue上定义一些新功能,Vue内置了一些全局API,比如我们今天要学习的指令Vue.directive.说的简单些就是,在 ...
- SQL Server 2005中top关键字的用法
1.返回N条记录数 select top n * from <表名> [查询条件] 2.返回总结果集中指定百分比记录数 select top n percent * from <表名 ...
- C#实现(递归和非递归)高速排序和简单排序等一系列排序算法
本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考. ...
- Centos查看端口占用情况
Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令: lsof -i tcp:80 列出所有端口 netstat -ntlp 结束进程: kill 进程代码
- java后台判断发布的图片是否存在
x现在已知一个固定格式的图片,判断图片是否存在例如,http://127.0.0.1/image/201709091300.jpg import java.net.URL;import java.ne ...
- php不使用递归实现无限极分类
无限极分类常用的是递归,但是比较不好理解,其实可以用数据库path,pid两个字段的设计来实现无限分类的功能 1.数据库设计 通过上图可以看出pid就是该栏目的父id,而path = 父path+pi ...
- 服务化之Netty
关于Netty的介绍可参考:https://www.zhihu.com/question/24322387 Netty官网为:http://netty.io/ Git:https://github.c ...
- Ubuntu下codeblocks编译器程序执行对话框内能进行粘贴编辑操作的指令
如这个: gnome-terminal -t $TITLE -x
- AutoItLibrary安装和常见问题解决
http://blog.csdn.net/bible_reader/article/details/52044345