题目链接: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. Luogu 1220 关路灯(动态规划)

    Luogu 1220 关路灯(动态规划) Description 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项 ...

  2. 1 Kafka概念和架构

    第一讲:概念.ZK的存储结构.Producer.Consumers流程.Kafka Broker的启动(额外) 从客户端使用角度来讲. 第二讲:从设计原理角度来讲. Kafka属于Apache组织,是 ...

  3. JVM加载一个类的过程

    类的加载过程 Java源代码被编译成class字节码,JVM把描述类数据的字节码.Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机 ...

  4. COGS 1516. 棋盘上的车

    COGS 1516. 棋盘上的车 http://www.cogs.pro/cogs/problem/problem.php?pid=1516 ☆   输入文件:rook.in   输出文件:rook. ...

  5. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  6. srpingboot2 session过期时间设置

    springboot2 设置session过期的配置 server.servlet.session.timeout = 1800 而不再是 server.session.timeout=1800

  7. Maximal Rectangle&Largest Rectangle in Histogram

    这两天在做leetcode的题目,最大矩形的题目以前遇到很多次了,一直都是用最笨的方法,扫描每个柱子,变换宽度,计算矩形面积,一直都以为就这样O(n2)的方法了,没有想到居然还有研究出了O(n)的算法 ...

  8. MongoDB - MongoDB CRUD Operations, Bulk Write Operations

    Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...

  9. 兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法

    兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法 // 获取事件function getEvent(){ if(window.event) {return w ...

  10. 【leetcode 简单】 第九十二题 第N个数字

    在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). 示例 1: ...