CodeForce-798C Mike and gcd problem(贪心)
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.
is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
Input
The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
Output
Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.
Example
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
Note
In the first example you can simply make one move to obtain sequence [0, 2] with .
In the second example the gcd of the sequence is already greater than 1.
题意:n个数,n<=1e5,操作:把a[i],a[i+1] 替换成 a[i]-a[i+1],a[i]+a[i+1],问至少要多少次操作才能让整个a数组的最大公约数gcd大于1.
由题目给出操作可知:当gcd(a,b)<=1时,进行操作为:
初始:a b
第一步:a-b a+b
第二步:-2b 2a
即两个数最多2步操作就能满足GCD==2。
对于两个偶数,要进行0步操作;对于两个奇数,要进行1步操作;对于一个奇数一个偶数,要进行2步操作。
先把所有“2个奇数成对”的情况计数+1并把两个奇数更新为偶数,然后在重新判断所有“1个奇数1个偶数成对”的情况计数+2。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200050],n,num=0;
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
ll ans=gcd(abs(a[1]),abs(a[2]));
for(int i=3;i<=n;i++)\
ans=gcd(ans,abs(a[i]));
if(ans>1) cout<<"YES"<<endl<<0<<endl;
else
{
for(int i=1;i<n;i++)
if(a[i]%2&&a[i+1]%2)
a[i]=0,a[i+1]=0,num++;
for(int i=1;i<n;i++)
if((a[i]%2&&a[i+1]%2==0)||(a[i]%2==0&&a[i]%2))
a[i]=0,a[i+1]=0,num+=2;
cout<<"YES"<<endl<<num<<endl;
}
return 0;
}
CodeForce-798C Mike and gcd problem(贪心)的更多相关文章
- 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 模拟构造 数组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
题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...
- codeforces 798C.Mike and gcd problem 解题报告
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...
- 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); ...
- #410div2C. Mike and gcd problem
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
随机推荐
- IOC(概念和原理)
什么是 IOC (1)控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理 (2)使用 IOC 目的:为了耦合度降低 (3)做入门案例就是 IOC 实现 IOC 底层原理 xml 解 ...
- Java HashSet和TreeSet【笔记】
Java HashSet和TreeSet[笔记] PS:HashSet.TreeSet 两个类是在 Map 的基础上组装起来的类 HashSet 类注释 1.底层实现基于 HashMap,所以迭代时不 ...
- 【原创】利用“进程注入”实现无文件不死webshell
引子 上周末,一个好兄弟找我说一个很重要的目标shell丢了,这个shell之前是通过一个S2代码执行的漏洞拿到的,现在漏洞还在,不过web目录全部不可写,问我有没有办法搞个webshell继续做内网 ...
- .NET 6 全新指标 System.Diagnostics.Metrics 介绍
前言 工友们, .NET 6 Preview 7 已经在8月10号发布了, 除了众多的功能更新和性能改进之外, 在 preview 7 版本中, 也新增了全新的指标API, System.Diagno ...
- .NET第三方补丁工具(Visual Patch)常用手册
SetupFactory简介 这是Indigo Rose(蓝玫瑰)公司开发的一套打包-补丁解决方案的补丁工具,相比Setup Factory,他的知名度似乎不太高,网上也很少找到相关资料,但是真的很简 ...
- SQL 练习38
查询选修了全部课程的学生信息 SELECT * from Student WHERE SId IN ( SELECT sid from sc GROUP BY sid HAVING COUNT(cid ...
- Window如何查看cpu核数,更改CPU开启的核数?
转载地址:http://www.win7zhijia.cn/win10jc/win10_8627.html
- C++类构造函数、拷贝构造函数、复制构造函数、复制构造函数、构造函数显示调用和隐式调用
一. 构造函数是干什么的 class Counter { public: // 类Counter的构造函数 // 特点:以类名作为函数名,无返回 ...
- 解析一个HTML字符串
存在问题 来自用户输入,一个文件或一个网站的HTML字符串,你可能需要对它进行解析并取其内容,或校验其格式是否完整,或想修改它.怎么办?jsonu能够帮你轻松解决这些问题 解决方法 使用静态Jsoup ...
- C++继承体系中的内存分段
---------------综述与目录-------------- 讨论这个问题之前我们先明确类的结构,一个类的大概组成,下面的很多分类名词都是我个人杜撰,为的就是让读者看懂能够区分,下面分别分类: ...