Codeforces 798C Mike and GCD problem
来源:互联网 发布:金庸群侠传 for mac 编辑:程序博客网 时间:2024/06/06 08:36
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
Input
2
1 1
Output
YES
1
Input
3
6 2 4
Output
YES
0
Input
2
1 3
Output
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.
题意:一个数组,数组内的元素之间不互质,换句话说,就是所有元素的最大公约数大于1。
分析:起初,我以为要把素数处理了就行,后来发现这主要是考虑奇数与偶数的事。当有使公因数是1的时候,我们可以想到只要把奇数都变成偶数,这样最大公因数就大于1了。当两个数都是奇数时,只需要一步就可以;当两个数是一奇一偶时,我们要操作两步才能达到目的。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[100005];int gcd(int a,int b){ if(b==0) return a; return(gcd(b,a%b));}int main(){ int n; while(~scanf("%d",&n)) { int i; for(i=0; i<n; i++) scanf("%d",&a[i]); int x=a[0]; for(i=1; i<n; i++) { x=gcd(x,a[i]); } if(x>1) printf("YES\n0\n"); else { int sum=0; for(i=0; i<n-1; i++) if(a[i]%2 && a[i+1]%2) { int t1=a[i]+a[i+1],t2=a[i]-a[i+1]; a[i]=t1; a[i+1]=t2; sum++; } for(int i=0; i<n; i++) if(a[i]%2) { if(i==0) { int t1=-2*a[i+1],t2=2*a[i]; a[i]=t1; a[i+1]=t2; sum+=2; } else { int t1=-2*a[i],t2=2*a[i-1]; a[i-1]=t1; a[i]=t2; sum+=2; } } printf("YES\n%d\n",sum); } }}
- Codeforces 798C Mike and gcd problem
- codeforces 798C Mike and gcd problem
- CodeForces 798C Mike and gcd problem
- Codeforces 798C Mike and GCD problem
- Codeforces 798C Mike and gcd problem gcd+贪心
- Codeforces-798C-Mike and gcd problem(贪心+数论)
- Codeforces 798C Mike and gcd problem (贪心)
- Codeforces 798C:Mike and gcd problem【数论+贪心】
- Mike and gcd problem CodeForces
- cf 798c Mike and gcd problem
- 798 C. Mike and gcd problem
- C. Mike and gcd problem
- C. Mike and gcd problem
- Mike and gcd problem-codeforces-Round 410 Div2-C
- codeforces round410 div2 C.Mike and gcd problem (数学)
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- codeforce 798C Mike and gcd problem (简单数学)
- CF#798 C. Mike and gcd problem(思维)
- 蓝牙数据传输过程简述
- HDU 1698 Just a Hook
- thinkphp上传图片自动压缩问题
- 【Linux学习笔记】20:Bash基础-历史命令
- 静态成员-学习笔记
- Codeforces 798C Mike and GCD problem
- 【网络流24题】餐巾计划(最小费用流)
- Go1.9 安全map用法
- 第二次采样的原理
- Apache虚拟主机中 Options 的设置~~~~~~~显示Apache目录列表
- 最小生成树
- yolo2
- xml、dtd、SAX解析、DOM解析、DOM4J解析学习(一)
- 最近的维护心得总结(关于安全)