Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1
2 seconds
256 megabytes
standard input
standard output
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).
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 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.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
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.
题目链接:http://codeforces.com/contest/798/problem/C
题意:一次操作中可以选择i (1 ≤ i < n),删除ai, ai + 1添加 ai - ai + 1, ai + ai + 1到相同的位子。尽可能的操作少的数量使得a数组的最大公约数大于1。注意
思路:ai - ai + 1, ai + ai + 1两个数相差2*ai + 1,2*ai + 1为它们之间最大公约数的倍数,即它们之间的最大公约数为2*ai + 1的约数。并且它们的最大公约数不可能为ai + 1的约数,否则ai,ai+1之间的最大公约数大于1,不需要进行操作。那么操作之后的a数组,之间的最大公约数为2。奇偶之间需要2次操作,奇奇之间需要1次操作,偶数直接跳过。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll MAXN=1e13+;
int n,a[maxn];
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++) scanf("%d",&a[i]);
int sign=a[];
for(int i=; i<=n; i++) sign=__gcd(sign,a[i]);
if(sign>)
{
cout<<"YES"<<endl<<<<endl;
return ;
}
int ans=;
for(int i=; i<=n; i++) a[i]=a[i]%;
for(int i=; i<=n; i++)
{
if(a[i]==) continue;
if(i==n) ans+=;
if(i+<=n)
{
if(a[i+]) ans+=;
else ans+=;
a[i+]=;
}
}
cout<<"YES"<<endl<<ans<<endl;
return ;
}
gcd
Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1的更多相关文章
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- 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 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 791C. Bear and Different Names 模拟构造
C. Bear and Different Names time limit per test:1 second memory limit per test:256 megabytes input:s ...
- CodeForces 689A Mike and Cellphone (模拟+水题)
Mike and Cellphone 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/E Description While sw ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- 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 C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
随机推荐
- 1.5.2、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包存储库)
本主题描述如何在Cloudera Manager部署中创建内部包存储库和直接主机以使用该存储库.您可以创建永久或临时存储库. 完成这些步骤后,您可以安装特定版本的Cloudera Manager或在未 ...
- SML + NL + HJ
Join是一种试图将两个表结合在一起的谓词,一次只能连接2个表,表连接也可以被称为表关联.在后面的叙述中,我们将会使用”row source”来代替”表”,因为使用row source更严谨一些,并且 ...
- java使用正则表达式
package com.regexp; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Tes ...
- zabbix 3.2.6+centos 7 +nginx 1.12+ mysql 5.6+ Grafana +php 5.6
先只记录关键点 1.nginx.MYSQL 用官方的YUM库安装 2.CENTOS升级到最新 3.ZABBIX 官方RPM安装,然后下载源码库,源码中的PHP复制至NGINX的配置目录 4.NGINX ...
- pta7-19打印学生选课清单(模拟)
题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114875633664 题意:输入n个学生,k門课程, ...
- 电商项目中学到的git命令
1.在拉下来的文件夹被删除后的操作 创建了文件后 git init 增加了 .git文件 ls -al 查看后有.git文件夹 git remote add origin (ssh) 连接到git仓库 ...
- oracle 修改数据精度问题
今天,在建表后插入语句时,报了这样的错误,对于这样的错误,搜集了一些资料,进行了总结如下: 建表语句: 1 2 3 4 5 6 7 8 9 create table pre_info( pre_cod ...
- Linux系统(Centos)下安装nodejs并配置环境
总结Centos下安装nodejs并配置环境,记录成功安装的方法.推荐的安装方法是利用已编译的二进制文件安装,不推荐使用源码的形式安装,一是源码安装比较麻烦,二是需要自行下载编译浪费时间. 1.安装n ...
- Centos和Redhat的区别与联系
CentOS的简介 CentOS是Community ENTerprise Operating System的简称,我们有很多人叫它社区企业操作系统,不管你怎么叫它,它都是Linux操作系统的一个发行 ...
- composer ip2city配置
//根据ip获取地址信息composer require "mylukin/ip2city: dev-master" // vendor/mylukin/ip2city/src/I ...