题目链接:http://codeforces.com/problemset/problem/798/C

题意:给你n个数,a1,a2,....an。要使得gcd(a1,a2,....an)>1,可以执行一次操作使ai,ai+1变为ai - ai + 1, ai + ai + 1。求出使得gcd(a1,a2,....an)>1所需要的最小操作数。

解题思路:首先,要知道如果能够实现gcd(a1,a2,....an)>1,那么a1~an肯定都是偶数(0也是偶数),所以我们的目的就是用最少的操作次数把所有数变成偶数。如果两个数都是奇数,那需要操作一次(奇数加减变成偶数),如果是偶数和奇数,那需要操作两次(奇数和偶数加减得到奇数,奇数再和奇数加减得到偶数),所以不存在gcd不能大于1的情况

 #include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+;
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
} int main(){
int n,tmp=;
int a[N]={};
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
tmp=gcd(tmp,a[i]);
}
if(tmp!=)//如果不需要操作就得到gcd>1,直接输出0次;
cout<<"YES"<<"\n"<<""<<endl;
else{
int ans=;
for(int i=;i<n;i++){//找奇奇
if(a[i]&&&a[i+]&){
ans++;
a[i]=a[i+]=;
}
}
for(int i=;i<n;i++){
if(a[i]&||a[i+]&){//找奇偶或者偶奇
ans+=;
a[i]=a[i+]=;
}
}
cout<<"YES"<<endl;
cout<<ans<<endl;
}
return ;
}

。因为要求的最小操作数,所以我们要先找奇奇的情况,再找奇偶或者偶奇的情况。

Codeforces 798C - Mike and gcd problem(贪心+数论)的更多相关文章

  1. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  2. codeforces 798C.Mike and gcd problem 解题报告

    题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...

  3. codeforces 798c Mike And Gcd Problem

    题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...

  4. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  5. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

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

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  7. CF798 C. Mike and gcd problem

    /* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...

  8. #410div2C. Mike and gcd problem

    C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

随机推荐

  1. Python中__init__.py文件的作用详解

    转自http://www.jb51.net/article/92863.htm Python中__init__.py文件的作用详解 http://www.jb51.net/article/86580. ...

  2. Laravel 项目集合

    1.  CMS LaraCMS  https://github.com/wanglelecc/laracms 2. 电商 3.  点播 MeEdu      https://github.com/Qs ...

  3. for循环 底层工作原理

    for 循环是对容器进行迭代的过程. 什么是迭代? 迭代就是从某个容器对象中逐个地读取元素,直到容器中没有更多元素为止. for 循环的步骤是什么? 先判断对象是否为可迭代对象,不是的话直接报错,抛出 ...

  4. django中的认证与登录

    认证登录 django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1  authenticate(**credentials)    提供了用户认证,即验证用户名以及密码是否 ...

  5. 项目经验总结-first

    1. org.apache.commons.lang中StringUtils判空使用经验之谈 StringUtils.isEmpty(String str) 判断字符串str是否为空串且是否长度为0, ...

  6. JVM 体系结构介绍

    JVM是Java的一大利器.它可以屏蔽各个计算机平台相关软件和硬件之间的差异.把平台相关的耦合统一工作交由JVM的实现者. JVM(Java 虚拟机),它通过模拟一个计算机来达到一个计算机所拥有的计算 ...

  7. Sql Server 优化技巧

    1.查看执行时间和cpu占用时间 set statistics time on select * from dbo.Product set statistics time off 打开你查询之后的消息 ...

  8. np.argsort函数

    np.argsort函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me numpy.argsort(a, axis=-1, kind='quicksort', order=None) 功能: ...

  9. C语言第九节进制

    进制 什么是进制 是一种计数的方式,数值的表示形式 数一下方块的个数 汉字:十一 十进制:11 二进制:1011 八进制:13 多种进制:十进制.二进制.八进制.十六进制.也就是说,同一个整数,我们至 ...

  10. windows10安装配置scikit-learn步骤

    安装python-2.7.13.msi(到C:\Python27),把C:\Python27和C:\Python27\Scripts添加到系统环境变量 安装numpy:下载numpy-1.11.3+m ...