CF892/problem/C
题目传送门:
[http://codeforces.com/contest/892/problem/C]
题意:
给你一个长度为n的数组,相邻两个元素的GCD(最大公约数)可以取代二者的任意一个,问你最少需要多少个操作数使得所有元素变为1。
如果不可以全化为1,输出0。
思路:
GCD性质:gcd(gcd(a,b),gcd(b,c))=gcd(gcd(a,b),c)=gcd(a,gcd(b,c))。先特判一下初始数组有1这个元素,那么假设有sum1个,输出,n-sum1就好了,因为1可以扩展到其他位置。否则,凑出一个1。怎么凑?
首先明确找的是相邻两个数的最大公约数,若相邻两个数的最大公约数等于1了就结束了,若不等于1,替换其中一个,在和相邻数求gcd,对于一个数来说,它被替换成 和左边的数的gcd,或和右边数的gcd都一样,举个例子:2,6,9 任何相邻两个数的gcd都不为1,看6这个数的位置,它可以被替换成和2的gcd,再和9求gcd,或被替换成和9的gcd,再和2求gcd,你看看这两种情况的结果是一样吧;只需贪心地找每次更新最小即可。
尤其注意n==1,这个时候如果,该元素是1,就是特判了,否则不可能变为1,因为没有其他元素和它GCD了
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int gcd(int x,int y){
return x ? gcd(y%x,x) : y;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,i,j,ans,sum;
int a[2005];
while(cin>>n){
sum=0,ans=1e9;
for(i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==1) sum++;
}
if(sum>0){
cout<<n-sum<<endl;
continue;
}
for(i=1;i<=n;i++)
{
int tep=a[i];
for(j=i+1;j<=n;j++){
tep=gcd(tep,a[j]);
if(tep==1){
ans=min(ans,j-i);//记录化为1的最小步数
break;
}
}
}
if(n==1||ans==1e9) cout<<-1<<endl;
else
cout<<ans+n-1<<endl;
}
return 0;
}
CF892/problem/C的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- MSSQL Sql加密函数 hashbytes 用法简介
转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...
- Proxysql读写分离配置
ProxySQL是Percona主推的读写分离中间件,下载地址为: https://www.percona.com/downloads/proxysql/ 一.安装 1:下载 wget https:/ ...
- miui10 傻瓜式安装google框架方法
miui10,打开自带的小米应用商店,搜索youtube,然后往下看选择百度的搜索源,点进去选择第一个下载就可以了. 下载完成之后会就提醒你安装google框架了, 点确定后自动就装好了,就是这么简单 ...
- phpstorm设置背景图片
对于这个问题,其实很简单,连续按两下shift键或者是Ctrl+Shift+A键即可.出来的界面如下: 输入set Background Image命令,出现的界面如下: 然后选择图片, 点击ok,即 ...
- 为什么会出现Notice: Undefined index: submit in D:\xampp\htdocs\test.php on line 19
事例如下": <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- 将含有makefile文件的源码加入Eclipse工程
转载自https://www.linuxidc.com/Linux/2011-02/32763.htm 很多软件在开发或者分析时需要一个像样的IDE,Eclipse是其中很优秀的一个,至少个人感觉很好 ...
- 【2018.04.27 C与C++基础】关于switch-case及if-else的效率问题
对于这个问题自己是比较清楚的,在分支比较多时,switch-case的效率肯定比if-else的要高许多,其原理类似于我们在优化某些程序时使用查表来代替算法计算一样. 如果想进一步深究的话,可以查看G ...
- java 开发注意事项
开发过程中的一些经验总结,不定时更新 1, 在开发接口的时候,尽量一个接口一个功能,不要多个功能共用一个接口,以免后期需求更改时修改接口困难, 使逻辑复杂
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- MyBatis动态SQL之一使用 if 标签和 choose标签
bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...