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 ...
随机推荐
- Nginx 出现 403 Forbidden 最终解决
Nginx 出现 403 Forbidden 最终解决 步骤一: 检查目录权限.权限不足的就加个权限吧. 例子:chmod -R 755 / var/www 步骤二: 打开nginx.conf 例子: ...
- Android重复依赖解决办法
参考文章:https://blog.csdn.net/qq_24216407/article/details/72842614 在build.gradle引用了Vlc的安卓包:de.mrmaffen: ...
- python编写文件统计脚本
python编写文件统计脚本 思路:用os模块中的一些函数(os.listdir().os.path.isdir().os.path.join().os.path.abspath()等) 实现功能:显 ...
- docker往阿里云推镜像和打包镜像
向仓库推镜像 1. 登录到阿里云docker镜像站点,然后创建仓库. 2.要按照阿里云官方给定的仓库名称来使用,所以我们一般都要继续给准备要上传的镜像二次添加标签,如下所示: 3.在终端登录阿里云站点 ...
- SpringCloud之初识Zuul(网关)---动态路由,权限验证
通过前面的学习,使用Spring Cloud实现微服务的架构基本成型,大致是这样的: 我们使用Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现:而服务间通过 ...
- Python开发【第三篇】:函数&读写文件
三元运算 三元运算,是条件语句的简单的写法.如果条件为真,则返回值1,否则,返回值2. ret = 值1 if 条件 else 值2 深浅拷贝 对于数字(int)和字符串(str)而言,赋值.深拷贝. ...
- python list和tuple
list列表简介:列表是python的基础数据类型之⼀ ,其他编程语⾔也有类似的数据类型. 比如JS中的数组, java中的数组等等. 它是以[ ]括起来, 每个元素⽤' , '隔开⽽且可以存放各种数 ...
- wangedit
<template> <el-row id="AddRoom"> <el-col :span="5">.</el-co ...
- hover效果的几种方式
1.改变透明度 #share_wrap a{display: inline-block;width: 48px;height: 44px;background: url(/images/finance ...
- 在Linux上搭建VisualSVN Server(svn服务端)
一.检查是否安装了低版本的SVN # rpm -qa | grep subversion 如果已安装SVN,则会返回版本信息.这时需要卸载旧版本的SVN. 卸载旧版本SVN # yum remove ...