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

题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 并且把 ai - a(i + 1), ai + a(i + 1)

放入原来的位置。问是否能够在几步操作后使得串的gcd大于1然后要求最小的操作数。

题解:偶数=偶数*偶数 or 奇数*偶数,奇数=奇数*奇数。

如果整个字符串全是偶数的话肯定gcd是大于1的。介于题目要求的操作,奇数-(or)+奇数=偶数

奇数-(or)+偶数=奇数,但是操作两次就是偶数。所以只要把原来串中的奇数改成偶数就行了。

因为影响gcd结果的就是奇数。然后贪心一下就行了。先处理两个奇数连在一起的然后再考虑一奇一

偶的。

#include <iostream>
#include <cstring>
using namespace std;
const int M = 1e5 + 10;
int a[M];
int gcd(int x , int y) {
return x % y ? gcd(y , x % y) : y;
}
int main() {
int n;
cin >> n;
for(int i = 0 ; i < n ; i++) {
cin >> a[i];
}
int num = 0;
for(int i = 0 ; i < n ; i++) {
num = gcd(num , a[i]);
}
if(num > 1) {
cout << "YES" << endl;
cout << 0 << endl;
}
else {
int ans = 0;
for(int i = 1 ; i < n ; i++) {
if(a[i] % 2 != 0 && a[i - 1] % 2 != 0) {
ans++;
a[i] = 2;
a[i - 1] = 2;
}
}
for(int i = 0 ; i < n ; i++) {
if(i == n - 1) {
if(a[i] % 2 != 0) {
ans += 2;
a[i] = 2;
}
}
else {
if(a[i] % 2 != 0) {
ans += 2;
a[i] = 2;
}
}
}
cout << "YES" << endl;
cout << ans << endl;
}
return 0;
}

codeforces 798 C. Mike and gcd problem(贪心+思维+数论)的更多相关文章

  1. codeforces 798 D. Mike and distribution(贪心+思维)

    题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...

  2. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

  3. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  4. 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 ...

  5. 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 ...

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

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

  7. 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); ...

  8. CF798 C. Mike and gcd problem

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

  9. #410div2C. Mike and gcd problem

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

随机推荐

  1. 7z 命令行方式生成自解压exe

    一.下载 7z是一个免费的工具,除了通过命令行的方式提供各种文件.压缩包相关的操作外,还提供了一种方式可以打出自解压的exe程序.该程序从运行到结束经历了三个流程: (1) 解压文件到用户临时目录: ...

  2. window下打jar包

    比如我的项目在 F/Myjar F:\Myjar>ll'll' 不是内部或外部命令,也不是可运行的程序或批处理文件. F:\Myjar>cd mian系统找不到指定的路径. F:\Myja ...

  3. MobaXterm:远程终端登录软件封神选手

    提到SSH.Telnet等远程终端登录,我相信很多人想到的都是PuTTY PuTTY通常用于Windows,但实际上可以多平台运行,因此不表达为"Windows下的远程终端登录" ...

  4. [ubuntu][deepin]系统增加自定义开机启动项

    [ubuntu][deepin]系统增加自定义开机启动项 进行配置 cd /etc/init.d/ ls vim myScript nginx实例 #! /bin/sh # chkconfig: # ...

  5. Hibernate 框架

    首先,我们先来了解一下什么是 Hibernate 框架. 什么是 Hibernate 框架? Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO ...

  6. 算法实战-OJ之旅

    算法虽然不是特别简单,但没有你想象中的那么难. Sort Array By Parity easy AC-17ms. 按照<算法导论>排序一章的一些概念,第二种可以称为是原址的(in-pl ...

  7. linux学习总结--linux100day(day2)

    Linux中的哲学--一切皆文件 为了便于操作,我们可以使用secureCRT或Xshell连接到我们的虚拟机. 要用远程工具连接到虚拟机上,我们只需要打开虚拟机上的ssh服务,在xshell中填写主 ...

  8. Spring入门(九):运行时值注入

    Spring提供了2种方式在运行时注入值: 属性占位符(Property placeholder) Spring表达式语言(SpEL) 1. 属性占位符 1.1 注入外部的值 1.1.1 使用Envi ...

  9. 调试应用不发愁,免安装的 curl 来帮忙

    1 cURL简介 cURL是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行.它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具.cURL还包含了用于程序开 ...

  10. 项目启动会(project initiating meeting)与项目开工会(kick-off meeting)区别

    一.项目启动会initiating meeting 召开时间:是启动阶段结束时召开的会议:主要任务:发布项目章程,并任命项目经理,赋予项目经理动用组织资源的权力:注意事项:(1)会议召开前已经对干系人 ...