题目链接: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. 在 Windows 上使用 Python 进行 web 开发

    本文由葡萄城技术团队于原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 上一篇我们介绍了在Windows 10下进行初学者入门开发Python的指 ...

  2. 【错误】【vscode】输出中文是乱码问题

  3. 大厂面试Kafka,一定会问到的幂等性

    01 幂等性如此重要 Kafka作为分布式MQ,大量用于分布式系统中,如消息推送系统.业务平台系统(如结算平台),就拿结算来说,业务方作为上游把数据打到结算平台,如果一份数据被计算.处理了多次,产生的 ...

  4. [原创实践]redhat linux 5.3搭建Nexus

    1:下载安装JDK,配置好环境变量(JAVA_HOME等) 下载linux下64位的jdk-7u45-linux-x64.tar.gz(百度网盘下载,官网的jdk-7u51-linux-x64.tar ...

  5. studio无限轮播

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  6. 用python实现银行家算法

    编制模拟银行家算法的程序,并以下面给出的例子验证所编写的程序的正确性. 进程 已占资源 最大需求数 资源种类 A B C D A B C D P0 0 0 1 2 0 0 1 2 P1 1 0 0 0 ...

  7. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  8. Vim高手,从来不用鼠标

    Vim脱离鼠标第一步 平时不可缺少的会用到vim,但是避免不了鼠标,事实上,省略鼠标是完全可以的,没有想像中那么难,看我短短几行带大家一起省略鼠标. 对了,vim有三种模式,基本模式就是用来输入命令的 ...

  9. 再读faster rcnn,有了深层次的理解

    1. https://www.wengbi.com/thread_88754_1.html (图) 2. https://blog.csdn.net/WZZ18191171661/article/de ...

  10. 随笔编号-12 阿里云CentOS7系列一 -- 安装JDK7的方法.

    最近因为数据采集以及生产环境冲突.导入windows Server 2008系统经常死机.经讨论决定把采集服务程序和生产服务进行分开.采集程序通过windows Server2008运行.而生产程序通 ...