原题链接: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. python学习之路---day16--面向对象

    面向对象及成员的基本总结复习一:先用一个实际例子说明类: class 类名: class Car: #__init__ :使我们在具体构造一个对象时,使这个对象有具体的属性,像有名字啊,价格等等这样的 ...

  2. zookeeper centos分布式安装使用

    1. 请先安装jdk和下载zookeeper.ssh免密登录请自行配置.大家可以到官网下载或我的网盘. 网盘地址: 共3台机器c0,c1,c2 192.168.132.148 c0192.168.13 ...

  3. linux新建用户并修改提示符

    1 新建用户 # useradd –d /home/ap/testapp -m testapp 此命令创建了一个用户testapp ,其中-d和-m选项用来为登录名testapp 产生一个主目录/ho ...

  4. python2.7+RobotFramework的UI自动化环境搭建

    robotFramework是一种比较常见的自动化测试框架,此篇记录环境搭建 目录 1.软件准备 2.执行安装 1.软件准备 python-2.7.15.amd64.msi              ...

  5. EL表达式(二)运算符

    运算符"."和"[]": "."能做的"[]"也能做,"[]"能做的"."不一定 ...

  6. Mac006--swithchosts安装

    Mac006--swithchosts安装 使用brew命令安装:brew cask install switchhosts 因为mac上,网络下载无法进行安装,所以应用brew命令安装. switc ...

  7. java.lang.NumberFormatException: For input string: "title"异常

    java.lang.NumberFormatException: For input string: "title" at java.lang.NumberFormatExcept ...

  8. forEach究竟能不能改变数组的值

    forEach究竟能不能改变数组的值 :https://blog.csdn.net/ZhengKehang/article/details/81281563 初学者每次提到Array对象的时候有些烦人 ...

  9. [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)

    [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP) 题面 有三个人从一张N个点无重边的有向无环图上的三个点出发,每单位时间,他们分别选择当前 ...

  10. kvm扩容home目录

    KVM虚拟磁盘扩容 1.磁盘扩容分为raw和qcow2两种扩容方式,命令相同,区别是后缀名 [root@daixuan ~]# qemu-img info /data/daixuan1.qcow2 / ...