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 ...
随机推荐
- logstash配置
input { #You must define a [type], otherwise you cannot get a field to cut. tcp { port => 5045 ty ...
- PE 添加系统管理员账号(域控可加)转
使用U盘制作一个PE系统,这里推荐老毛桃或者大白菜:开机进入Bios,选择U盘启动:进入U盘启动画面后,选择一个PE系统:进入PE系统后,我们去本机系统盘,将 C:/Windows/System32/ ...
- [Hive_8] Hive 设计优化
0. 说明 在 Hive 中,数据库是一个文件夹,表也是文件夹 partition,是一个字段,是文件 前提:在 Hive 进行 where 子句查询的时候,会将条件语句和全表进行比对,搜索出所需的数 ...
- ELK收集tomcat访问日志并存取mysql数据库案例
这个案例中,tomcat产生的日志由filebeat收集,然后存取到redis中,再由logstash进行过滤清洗等操作,最后由elasticsearch存储索引并由kibana进行展示. 1.配置t ...
- SpringCloud之初识Hystrix熔断器 ----- 程序的保护机制
在上一篇的-负载均衡Robbin中,我们简单讲解到负债均衡的算法和策略.负载均衡就是分发请求流量到不同的服务器,以减小服务器的压力和访问效率,但是当负载均衡的某个服务器或是服务挂掉之后,那么程序会出现 ...
- syslog的坑
先看看代码: g_log, err := syslog.NewLogger(syslog.LOG_INFO, ) 再看看syslog的源码: // NewLogger creates a log.Lo ...
- 十大PHP程序员必备工具
十大PHP程序员必备工具 1.Notepad++ 总结来说就是小而精,7.4版本的软件包只有2.9M,比一般的IDE小数十倍,但是Notepad++的功能确是很全面的,代码高亮,语法折叠,宏功能,内置 ...
- centos7下安装docker(15.1跨主机网络)
之前学习了单个host上的网络,我们知道单个host上的网络有:none,host,bridge和joined,他们解决了单个host上面的容器通信的问题:接下来我们讨论跨主机间容器通信的方案 跨主机 ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
- 在Python虚拟环境中安装scrapy
虚拟环境安装scrapy 在虚拟环境中执行命令 (scrapyenv) E:\Python\Envs>pip install -i https://pypi.douban.com/simple/ ...