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)优 ...
随机推荐
- VueJS标签消息显示HTML:v-html
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- Thrift安装介绍
一.简介 1.语言库要求 因为thrift支持多语言.所以编译thrift源代码的过程中,会用到该语言的一些类库.如c++的boost.java的jdk等. 那么,在安装thrift过程中,须要对各种 ...
- 修改mysql数据库 密码
将密码改成123456 update mysql.user set authentication_string=password('123456') where user='root' and Hos ...
- Class doesn't implement Cloneable之怪象
1. 报错: -20 ::): threadid=: thread exiting with uncaught exception (group=0x4001d400) -20 ::): FATAL ...
- Java的泛型约束和限制
不能用基本类型实例化类型参数 不能用类型参数代替基本类型:例如,没有Pair<double>,只有Pair<Double>,其原因是类型擦除.擦除之后,Pair类含有Objec ...
- CASIO fx-991es Plus科学计算器使用技巧
关于输出: 默认是按照自然书写格式显示的,计算结果是按照分数形式显示,如0.5x0.5,会显示=1/4.虽然很直观,但是在测量和估算上略有不便.此时用 SHIFT --> MODE (也就是se ...
- erlang启动参数记录
不管在erlang的shell下还是脚本里,启动参数都是非常有用的,抽空儿整理下erlang的常用启动参数: +A size 异步线程池的线程数,范围为0~1024,默认为10 +P Number ...
- 用JS写九九乘法表
本来JS部分觉得就不是很好,结果经过一个寒假,在家的日子过的太舒适,基本把学的都快忘干净了,今天老师一说九九乘法表,除了脑子里浮现出要满足的条件,其他的都不记得了,赶快整理了一下: <scrip ...
- php遍历统计文件目录和文件
function total($dirname, &$dirnum, &$filenum){ $dir=opendir($dirname); readdir($dir)."& ...
- Vue源码探究-状态初始化
Vue源码探究-状态初始化 Vue源码探究-源码文件组织 Vue源码探究-虚拟DOM的渲染 本篇代码位于vue/src/core/instance/state.js 继续随着核心类的初始化展开探索其他 ...
.
is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
.