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

题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的。存在这样的操作,使ai,ai+1变为(ai-ai+1), (ai+ai+1)。问最少进行这样的操作使数列是“漂亮”的。

思路:考虑gcd(a1,a2,a3...,an)>1 的情况:

我们对ai,ai+1进行两次操作可以得到2ai,2ai+1,也就是说一对相邻的数字最多操作2次使它们的gcd=2>1。而对于一对奇数来说,操作一次就能使它们成为偶数。

现在就是要把数列中所有的数变为偶数。先对相邻奇数进行操作(每次+1),再对单个奇数进行操作(每次+2)即可。

AC代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXN=2e6+;
long long num[MAXN];
long long gcd(long long a, long long b){
return (b==)?a:gcd(b, a%b);
}
int main()
{
int n;
while(cin>>n){
memset(num, , sizeof(num));
for(int i=;i<n;i++){
cin>>num[i];
}
long long g=gcd(num[], num[]);
for(int i=;i<n;i++){
g=gcd(g, num[i]);
if(g==)
break;
}
if(g>){
cout<<"YES"<<endl;
cout<<<<endl;
continue;
}
int res=;
for(int i=;i<n;i++){
if(num[i-]%&&num[i]%){
num[i-]++;
num[i]++;
res++;
}
}
for(int i=;i<n;i++){
if(num[i-]%&&num[i]%==||(num[i]%&&num[i-]%==)) {
num[i-]=;
num[i]=;
res+=;
}
}
cout<<"YES"<<endl;
cout<<res<<endl;
} return ;
}

codeforces798C - Mike and gcd problem (数论+思维)的更多相关文章

  1. CF798 C. Mike and gcd problem

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

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

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

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

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

  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. #410div2C. Mike and gcd problem

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

  7. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

  8. Mike and gcd problem CodeForces - 798C (贪心思维+数论)

    题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...

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

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

随机推荐

  1. iOS应用发布打包时为什么选择release,而不是debug

    一.Debug和Release版本区别? 众所周知,我们进行iOS开发,在Xcode调试程序时,分为两种方式,Debug和Release,在Target的Setting中相信大家应该看到很多选项都分为 ...

  2. 127、TensorFlow 计算图执行(二)

    import tensorflow as tf # Define a placeholder that expects a vector of three floating-point values ...

  3. Linux实用技巧--隧道

    平时开发过程中,可能会遇到一些网络问题,比如npm install 一些依赖包.本地电脑是可以,没有问题.但是测试环境服务器,由于公司内部网络安全限制,不可以随意访问外部网络.因此下载一个依赖包就变得 ...

  4. SSH 的原理和实践

    最近自己在学习使用SSH,现将自己理解的SSH原理和实践SSH的操作写成一篇博客,以供日后查看. 一.SSH是什么?为什么会出现SSH? SSH英文全称是Secure Shell,即安全外壳.首先SS ...

  5. python3 -tensorflow-dataset 之map函数 lambda隐函数的应用

    ###隐函数lanbda及map函数的应用实例 ''' lambda 参数1,参数2,参数3,...:表达式 ''' f = lambda x,y :x+y print(f(1,2)) ###输出3 ...

  6. 在Linux上安装MySQL

    一.获取mysql YUM源    打开网址:https://dev.mysql.com/downloads/repo/yum/,选择对应linux版本 点击“No thanks, just star ...

  7. seaborn教程2——颜色调控

    原文转载 https://segmentfault.com/a/1190000014966210 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风 ...

  8. 攻防世界--simple-unpack

    下载链接:https://adworld.xctf.org.cn/media/task/attachments/b7cf4629544f4e759d690100c3f96caa 1.准备 获取到信息: ...

  9. VS Code的使用

    之前一直使用的是WebStorm来学习web前端开发,最近开始使用VSCode,很多方面和WebStorm不一样,需要一段时间适应,以下是我初次使用VSCode进行web前端开发学习所遇到的一些问题以 ...

  10. JS高级 — 函数中的this指向问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...