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 ...
随机推荐
- 二分partition算法应用
一个二分partition算法,将整个数组分解为小于某个数和大于某个数的两个部分,然后递归进行排序算法. 法一: int partition(vector<int>&arr, in ...
- 笔记:XML-解析文档-XPath 定位信息
如果需要定位某个XML文档中的一段特定信息,那么通过遍历DOM 树的众多节点来进行行查找显得有些麻烦,XPath语言使得访问树节点变得很容易,例如,下面的XML文档结构: <?xml versi ...
- console.log(0.2+0.4===0.6)// true or false??
在正常的数学逻辑思维中,0.2+0.4===0.6这个逻辑是正确的,但是在JavaScript中0.2+0.4!==0.6这是为什么呢?这个问题也会偶尔被用来当做面试题来考查面试者对 JavaScri ...
- java排序算法(五):快速排序
java排序算法(五):快速排序 快速排序是一个速度非常快的交换排序算法,它的基本思路很简单,从待排的数据序列中任取一个数据(如第一个数据)作为分界值,所有比它小的元素放到左边.所有比它大的元素放到右 ...
- python全栈学习--day1
计算机基础 CPU:中央处理器 内存:4GB,8GB,临时处理事务的地方,供给CPU数据. 硬盘:相当于电脑的数据库,存储着大量的数据,文件,电影等. 操作系统:执行者,支配所有关系 window ...
- MySQL 操作详解
MySQL 操作详解 一.实验简介 本节实验中学习并实践 MySQL 上创建数据库.创建表.查找信息等详细的语法及参数使用方法. 二.创建并使用数据库 1. 创建并选择数据库 使用SHOW语句找出服务 ...
- sqlserver之排序规则和ETL不支持sqlserverdatetime2的问题
sqlserver的排序规则大概分为Windows 排序规则和 SQL Server 排序规则.数据在安装的时候,默认不设置会默认为SQL_Latin1_General_CP1_CI_AI.数据库在创 ...
- TensorFlow-谷歌深度学习库 手把手教你如何使用谷歌深度学习云平台
自己的电脑跑cnn, rnn太慢? 还在为自己电脑没有好的gpu而苦恼? 程序一跑一俩天连睡觉也要开着电脑训练? 如果你有这些烦恼何不考虑考虑使用谷歌的云平台呢?注册之后即送300美元噢-下面我就来介 ...
- VS Code 常用命令记录
1. 创建解决方案 例:dotnet new sln -o HelloWorld.Solutions 其中 -o 表示输出文件夹 2.创建类库.web.mvc.webapi等项目 例:dotnet n ...
- C# reportview 按时间改变行颜色
//) AND ((Day(Now()) - Day() AND (Day(Now()) - Day()),) AND (Day(Now()) - Day()) OR (Month(Now()) - ...