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 ...
随机推荐
- xlsx导入成--json
这两天遇到大难题了,就是这个 xlsx 导入问题,之前用的xlsx.full.min.js,写的导入,结果不兼容ie浏览器,研究这个也好长时间,网上居然还没有搜到合适的,自己写从xlsx官网上 ...
- python列表操作符
list1=[123,456] list2=[234,234] list1>list2 >>>False#返回False 第一项比较之后直接返回false,第二项不看 #+实现 ...
- Acer Aspire E1 471G 加装SSD+机械盘后无法启动的问题
老笔记本 Acer Aspire E1 471G 加装了一块 SSD 作为系统盘(win10),原机械盘格式化后,装在光驱托架上作为数据盘. 可能会出现: 系统无法启动,显示找不到启动设备,并且在F2 ...
- New UWP Community Toolkit - XAML Brushes
概述 上一篇 New UWP Community Toolkit 文章中,我们对 V2.2.0 版本的重要更新做了简单回顾.接下来会针对每个重要更新,结合 SDK 源代码和调用代码详细讲解. 本篇我们 ...
- Nginx阻止对不明确主机名的请求
在用户请求头中,有可能会有Host行不明确的情况,如果不想处理这类用户请求,那么可以定义一个默认的server来丢弃这类请求.例如 server{ listen default_server; ser ...
- linux系统命令学习-用户管理
1. 用户 a) 系统使用user id 简称UID来标志用户的唯一性 b) 用户分为三类:系统用户,根用户,普通用户 i. 普通用户 UID大于500,系统默认普通用户UID从500开始 只能操 ...
- js如何获取隐藏的元素的高度
首先,正常情况下,确保div是有高度的. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- Java读取文件存储到mysql
写了一批Lua脚本,要放到数据库里面,调用的时候进行计算,由于有太多lua脚本,就写了个程序来录入. 下面主要分三个部分: public static String readToString(Stri ...
- 通过cmd命令行连接mysql数据库
找到 mysqld.exe所在的路径 使用cd切换到msyqld.exe路径下 输入mysql连接命令,格式如下 Mysql -P 端口号 -h mysql主机名\ip -u root (用户) ...
- CodeBlocks使用介绍
CodeBlocks对单个文件没法调试,需要建工程后才能调试,所以使用CodeBlocks必须先建工程. 一.如何用CodeBlocks建工程文件. 选择File->New->Projec ...