CodeForces798cMike 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
21 1
YES1
36 2 4
YES0
21 3
YES1
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个数字输入,第二行输入数字,你可以对第i个数和第i+1执行一种交换操作,即用ai, ai + 1 替代ai - ai + 1, ai + ai + 1
求最少进行多少次操作,能使该序列的最大公共公因数>1,这里题目是绝对有解的,不需考虑no的情况
对于数字a,b进行操作: a,b a-b,a+b -2b,2a
由此题目就容易了,2是最容易想到的最大公因数,而进行操作又可以令无论奇偶的两个数都变成偶数
那么对数组进行访问
1.假如第i个数和第i+1个数都为偶数,操作次数为0(不产生影响,不用判断)
2.假如第i个数和第i+1个数都为奇数,操作次数为1
3.假如一个奇数一个偶数,操作次数为2
但这里就有一个问题了,2和3之间存在冲突,并且2几乎无法触发。而题目求的是最少操作数量,那么显然2是优于3的,所以在处理时应给2更高的优先级。因此可以对数组进行两次访问,第一次执行操作2,第二次执行操作3。
另外,题目有一个要注意的地方是在交换前最大公共公因数已经符合条件的话,可以直接结束,代码如下
#include<stdio.h> #define MAX 100000 int gcd(int s1,int s2) {int r;while (s2!=0) {r=s1%s2;s1=s2;s2=r;}return s1; } int main() {int n,i,j,a[MAX],t,ans;while(scanf("%d",&n)!=EOF){t=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);}ans=gcd(a[1],a[2]);for(i=3;i<=n;i++)ans=gcd(ans,a[i]);if(ans>1)printf("YES\n0\n");else{ ans=0;for(i=2;i<=n;i++){if(a[i-1]%2&&a[i]%2){ans++;a[i-1]=0;a[i]=0;}}for(i=2;i<=n;i++){if(a[i-1]%2==0&&a[i]%2||a[i-1]%2&&a[i]%2==0){ans+=2;a[i-1]=0;a[i]=0;}}printf("YES\n%d\n",ans);}} }
转载于:https://www.cnblogs.com/qq936584671/p/6814876.html
CodeForces798cMike and gcd problem相关推荐
- Codeforces 798C:Mike and gcd problem
Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...
- G - Mike and gcd problem
G - Mike and gcd problem Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the seq ...
- 牛客多校第四场【B-Basic Gcd Problem】
牛客多校第四场[B-Basic Gcd Problem] 题目链接:https://ac.nowcoder.com/acm/contest/5669/B 思路:先要理解公式,多看几个数据基本就会有点想 ...
- codeforces798C - Mike and gcd problem (数论+思维)
原题链接:http://codeforces.com/contest/798/problem/C 题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是"漂亮& ...
- 牛客多校4 - Harder Gcd Problem(构造+贪心)
题目链接:点击查看 题目大意:给出一个 n ,表示 1 ~ n 的 n 个数字,现在要求选出尽可能多的两两匹配,使得每组匹配的 gcd 都大于 1,输出最多能有多少组匹配,以及方案 题目分析: 这样的 ...
- Mike and gcd problem(思维)
Mike has a sequence A = [a1, a2, -, an] of length n. He considers the sequence B = [b1, b2, -, bn] b ...
- 2020牛客暑期多校训练营(第四场)H.Harder Gcd Problem(把1到n分为不互质的数对,找最多的对数)
题目大意:把1到n分为不互质的数对,找最多的对数 思路:先从最大的质因数开始找,因为小的比大的更容易匹配,所以贪心的从大的开始找. 首先要预处理出所以数的最大质因数. 然后根据质因数从大往小找,当质因 ...
- 【Codeforces - 798C】 Mike and gcd problem(思维,贪心)
题干: Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, .. ...
- 牛客多校4 - Basic Gcd Problem(预处理质因子的个数)
题目链接:点击查看 题目大意:给出一个函数,有 t 次询问,每次询问给出相应的参数,要求计算函数值 题目分析:打个表不难将函数化简为: ,x 为 n 中的质因子个数,而质因子的个数可以 n * sqr ...
最新文章
- 2022-2028年中国轻型客车行业投资分析及前景预测报告
- suse linux不能识别u盘,SUSE Linux mount u盘
- ZedGraph在Asp.net中的应用
- c语言static. volatile,嵌入式系统C语言重点语法const、volatile、static、堆栈等的意义及用法...
- 相依關係: XXXXXXXXX但它卻無法安裝
- python通过指定网卡发包_windows下用UDP 广播在特定网卡上发包
- 计算机整个文稿应用回顾主题,《计算机应用基础》精品课程电子教案-PowerPoint 2003...
- java 并发计数器_Java 8 LongAdders:管理并发计数器的正确方法
- java %3c%=a%%3e_跪求帮忙解析,急!!!
- Scala学习笔记04:内建控制结构
- 计算机运行卡英语怎么说,处理电脑卡顿(国外英文资料).doc
- 有各组方差怎么算组间平方和_组内离差平方和,组间离差平方和与总离差平方和各反映了什么?...
- 计算机学校的逻辑思维题,2013逻辑推理专项习题100道(附答案).docx
- php配置站点报错403,phpstudy V8 报403错误怎么办
- 照片模糊怎么变清晰?
- qq三方登托管模式选择_我应该为我的网站选择哪种托管?
- 用AntlR4实现简单的汇编编译器
- 爬虫从入门到精通(15) | 使用Python-OCR识别库对图形验证码进行识别
- Python深度使用指南
- 全国大学生计算机技能应用(2020年)——C++科目决赛程序设计题解
热门文章
- OpenLayers3基础教程——OL3之Popup
- 不完整类型(partial type)
- unity自发光透明shader
- uboot 命令分析(一) — bootm
- react 遍历对象_探索:跟随《Build your own React》实现一个简易React
- delphi报列表索引越界怎么处理_Python入门第3课:列表元组,看这一篇够了 | 原创...
- ThingJS:部署物联网不用买买买,互联互动是切入点
- iphone6 充电电流测试软件,iPhone7支持快充? 9个充电器数据实测
- n条直线相交最多有几个邻补角_【东升二中数字课堂】创意微课直线的交点|Super数学璐...
- html table设置行高_html 表格单元格的宽度和高度的设置方法