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, ...
随机推荐
- 云计算openstack共享组件——Memcache 缓存系统
一.缓存系统 静态web页面: 1.工作流程: 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Reques ...
- DJango安装-windows
1.进入虚拟环境后启动 activate 2.查看当前虚拟环境是否存在Django环境 pip list 3.不存在则 安装Django环境 pip install django 4.查看Django ...
- MySQL 基准测试
这是<高性能 MySQL(第三版)>第二章的读书笔记. 基准测试(benchmark)是针对系统的压力测试,用于掌握系统行为或重新系统的某个状态.sysbench 是常用的 MySQL 基 ...
- 阻塞进程函数 wait()和waitpid()
1. wait()和waitpid()函数说明 wait() 进程一旦调用了wait(), 就立即阻塞自己,由wait自动分析是否有当前进程的某个子进程已经退出,如果让它找到了一个已经变成僵尸的子进 ...
- (转)https://wiki.debian.org/iwlwifi debian7下wifi intel 5100agn
https://wiki.debian.org/iwlwifi Debian 7 "Wheezy" Add a "non-free" component to ...
- C# 中定义斜杠 \
\ 是转义符 如 \’ 单引号 \” 双引号 \\ 反斜杠 \0 空 \a 警告(产生峰鸣) \b 退格 \f 换页 \n 换行 \r 回车 ...
- 使EasyUI的window、panel、dialog 不被拖出页面
function easyuiPanelOnMove(left, top) { var l = left; var t = top; if (l < 1) { l = 1; } ...
- PHP_CodeIgniter Github实现个人空间
github支持github Pages 可以实现自己的个人空间 XXX.github.io/project 1 注册自己的github账户 2 需要设置自己的user_name, user_name ...
- Codeforces - 1194B - Yet Another Crosses Problem - 水题
https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...
- python车牌精确定位
#coding=utf-8 import cv2 # 使用的是HyperLPR已经训练好了的分类器 watch_cascade = cv2.CascadeClassifier('model/casca ...