#410div2C. Mike and gcd problem
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.
题解:当$\gcd (x,y)! = 1$时,直接得出答案。
当$\gcd (x,y) = = 1$,令$d = \gcd (x - y,x + y)$,
所以,$d|(x - y),d|(x + y)$
由信安数基课本P4,$a|b,a|c \to a|tb + sc$,
$d|2x,d|2y$$\to d|\gcd (2x,2y) \to d|2\gcd (x,y) \to d|2$,$d = = 1or2$因为若再继续下去,必须满足此等式,故不必继续。
可以看出最后的d一定整除偶数,所以n个数必须都为偶数.
所以此题即变为,把n个数变为偶数的最小步数。
当$a[i]\% 2 = = 1 ,a[i + 1]\% 2 = = 1$,步数增加1,
当a[i]和a[i+1]有一个为偶数时,步数增加2.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
ll n,a[],t=;
ll gcd(ll a,ll b){
while(b){
ll temp=b;
b=a%b;
a=temp;
}
return a;
}
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
t=gcd(t,a[i]);
}
for(int i=;i<n;i++){
a[i]%=;
}
if(t!=){
cout<<"YES\n0\n";
return ;
}
ll ans=;
for(int i=;i<n;i++){
if(a[i]){
ans++;
if(!a[i+]){
ans++;
}
a[i]=a[i+]=;
}
}
cout<<"YES\n"<<ans<<endl; }
#410div2C. Mike and gcd problem的更多相关文章
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于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 ...
- 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 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
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- codeforces798C - Mike and gcd problem (数论+思维)
原题链接:http://codeforces.com/contest/798/problem/C 题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的.存在这 ...
随机推荐
- 高性能javascript学习总结(3)--数据访问
在 JavaScript 中,数据存储位置可以对代码整体性能产生重要影响.有四种数据访问类型:直接量,变量,数组项,对象成员. 直接量仅仅代表自己,而不存储于特定位置. JavaScr ...
- PHP数组各种操作与函数汇总
对于Web编程来说,最重要的就是存取和读写数据了.存储方式可能有很多种,可以是字符串.数组.文件的形式等.数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是我学习的小结,借 ...
- [转] 携程App网络服务通道治理和性能优化@2016
App网络服务的高可靠和低延迟对于无线业务稳定发展至关重要,过去两年来我们一直在持续优化App网络服务的性能,到今年Q2结束时基本完成了App网络服务通道治理和性能优化的阶段性目标,特此撰文总结其中的 ...
- 认识CoreData—使用进阶
之前两篇文章都比较偏理论,文字表达比较多一些,但都是干货!学习时先理解理论知识,才能更好的帮助后面的理解.在这篇文章中,将会涉及关于CoreData的一些复杂操作,这些操作会涉及分页查询.模糊查询.批 ...
- Django之Form详解
Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 1.创建Form类.View函数处理 from ...
- Delphi 的类型与指针
Delphi 的指针分为 "类型指针" 和 "无类型指针" 两类.Delphi 中的类型, 常用的也得有几百个, 我们可以给每种类型定义相应的类型指针.其实 D ...
- HDU Rightmost Digit
Rightmost Digit Time Limit:1000MS Memory Limit: ...
- Nginix安装教程(Ubuntu)
安装gcc g++的依赖库 #apt-get install build-essential #apt-get install libtool 安装 pcre依赖库 #sudo apt-get u ...
- C++ typedef的一个用法
1.不适用typedef: #include <iostream> #include <cstring> using namespace std; struct Books { ...
- Codeforces 509F Progress Monitoring:区间dp【根据遍历顺序求树的方案数】
题目链接:http://codeforces.com/problemset/problem/509/F 题意: 告诉你遍历一棵树的方法,以及遍历节点的顺序a[i],长度为n. 问你这棵树有多少种可能的 ...