codeforces 798c Mike And Gcd Problem
题意:
给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]。
问现在需要最少多少次操作,使得整个数列的gcd大于1。
思路:
经过思考后发现,除非所有的数的gcd已经大于1,那么就必须把全部数字变为偶数。
变数字的时候,必须从第一个不是偶数的数字开始变化,每次都从下标最小的为奇数的数字开始变化。
如果是奇数,奇数,那么显然经过一次就可以全部变为偶数;
如果是奇数,偶数,那么必须经过两次才能全部变成偶数。
这题实际是贪心。
代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; typedef long long ll;
const int inf = 0x3f3f3f3f; const int N = 1e5 + ; int a[N]; int gcd(int x,int y)
{
if (y == ) return x;
else return gcd(y,x % y);
} int main()
{
int n; cin >> n; for (int i = ;i <= n;i++)
{
cin >> a[i];
} int _gcd = a[]; for (int i = ;i <= n;i++)
{
_gcd = gcd(a[i],_gcd);
} if (_gcd > )
{
return *printf("YES\n0\n");
} int ans = ; int p = ; while (p <= n)
{
if (a[p] % == )
{
p++;
}
else
{
if (p < n && a[p+] % )
{
ans++;
a[p] = a[p+] = ;
p++;
}
else if (p < n && a[p+] % == )
{
ans += ;
a[p] = ;
p++;
}
else
{
ans += ;
p++;
}
}
} //if (a[n] % 2) ans += 2; cout << "YES" << endl << ans; return ;
}
codeforces 798c Mike And Gcd Problem的更多相关文章
- 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 ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
- codeforces 798C.Mike and gcd problem 解题报告
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- 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 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 ...
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...
- #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 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
随机推荐
- Jquery入门(初学者易懂)
一.定义 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是"w ...
- Linux源码-等待队列注释
等待队列 Linux中了等待队列的毒,代码中充斥着等待队列.不信你翻翻代码. 等待队列的唤醒我们这里叫激活.免得和线程唤醒混淆. 数据结构 头结点wait_queue_head_t的结构 struct ...
- eclipse报错排解
一.解决eclipse中git插件中的cannot open git-upload-pack问题 有时候在eclipse上使用插件egit向github或者osc上同步代码时,有时候会发现出现cann ...
- 【Python】 零碎知识积累 I
大概也是出于初高中时学化学,积累各种反应和物质的习惯,还有大学学各种外语时一看见不认识的词就马上记下来的习惯,形成了一种能记一点是一点的零碎知识记录的癖好.这篇文章就是专门拿来记录这些零碎知识的,没事 ...
- table 表格的增删和修改
如上图,图片的增删都没有问题:唯一的问题就是我改变下一行的内容时,把上面一行给覆盖了,费了好久,终于找到原因了,直接贴代码: 效果如下:
- c++ --> #define中的三个特殊符号:#,##,#@
#define中的三个特殊符号:#,##,#@ 看下面三个define宏定义: #define Conn(x,y) x##y #define ToChar(x) #@x #define ToStrin ...
- linux小白成长之路4————centos7配置自动更新安装安全补丁
[内容指引] 安装yum-cron; 修改配置:nano: 手工启动服务: 将服务设置为开机自动启动. 为保证linux系统的安全性以及稳定性,可以使用yum-cron服务自动更新: 1.安装yum- ...
- linux小白成长之路6————安装Java+Apache(httpd)+Tomcat
[内容指引] 安装Java环境: 查看JDK版本: 安装Apache(httpd); 安装Tomcat: 设置服务开机启动. 1.安装Java环境 指令: yum intall java-1.8.0* ...
- 多目标跟踪(MOT)论文随笔-SIMPLE ONLINE AND REALTIME TRACKING WITH A DEEP ASSOCIATION METRIC (Deep SORT)
网上已有很多关于MOT的文章,此系列仅为个人阅读随笔,便于初学者的共同成长.若希望详细了解,建议阅读原文. 本文是tracking by detection 方法进行多目标跟踪的文章,在SORT的基础 ...
- Leetcode 6——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...