codeforces798C - Mike and gcd problem (数论+思维)
原题链接: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 (数论+思维)的更多相关文章
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- 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 ...
- 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); ...
- 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 ...
- #410div2C. Mike and gcd problem
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
随机推荐
- Fedora 26 安装搜狗拼音输入法 sogoupinyin
Fcitx 官方 wiki Fcitx GitHub Arch wiki 上关于 Fcitx 的资料 故障排除可以参考这里 Fedora 中文社区 Fedora 中文社区软件源 Fcitx (Flex ...
- poj3614Sunscreen
Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her ...
- APT攻防整理-攻击方法/工具
攻击步骤 一般步骤 社工 武器制造 武器投递 漏洞利用 安装后门 后渗透 这5个阶段攻击非常隐蔽,可绕过传统安全设备检测 潜伏控制 传统通信方式不会使用,如cc/socket/http(可采用安全隧道 ...
- 面向JVM的应用程序的项目结构
对于Maven所用的项目结构,称为Maven标准的目录结构,不包含git 一.一个典型的源代码结构: / [project-name] README.txt LICENSE.txt pom.xml / ...
- selenium python 报错“ unable to find binary in default location”
selenium python 报错如下: raise exception_class(message, screen, stacktrace)selenium.common.exceptions.W ...
- 查询SQLSERVER执行过的SQL记录(历史查询记录)(转)
原文链接:https://www.cnblogs.com/icycore/p/10493237.html 有的时候,需要知道近段时间SQLSERVER执行了什么语句,可以用下面的方法: SELECT ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- 【Linux】php7.2.8 + xdebug + composer + php代码覆盖率 + jenkins配置 (实操记录,亲测可用)
[一.linux安装php 7.2.8] 1.wget http://nginx.org/download/nginx-1.9.9.tar.gz # nginx可不安 ...
- [fw]Understanding a Kernel Oops!
An “Oops” is what the kernel throws at us when it finds something faulty, or an exception, in the ke ...
- hdu6354 Everything Has Changed (圆的相交弧长)
题目传送门 题意: 用一堆圆来切割一个圆心为原点,半径为R的圆A,问切割完毕后圆A外围剩余部分的周长(图中的红线部分). 思路: 首先判定圆与圆A的关系,这题我们只需要与A内切.相交的圆. 然后就是求 ...