题目传送门:

[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的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. 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 ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [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 ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

随机推荐

  1. mssql 一次向表中插入多条数据的方法分享 (转自:http://www.maomao365.com/?p=6058)

    转自:http://www.maomao365.com/?p=6058) <span style="font-size:16px;font-weight:bold;"> ...

  2. python len()函数的用法

    函数:len() 返回字符串.列表.字典.元组等长度. 语法:len(str) str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度. Test: 1:计算字符串的 ...

  3. 【MM系列】SAP 根据PO查找对应的打印FORM

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 根据PO查找对应的打印FOR ...

  4. MySQL常用命令(一)

    (1)库的基础操作 查看已有库: show databases; 创建库(制定默认字符集): ccreate database 库名 default charset=utf8; 查看创建库的语句: s ...

  5. 基数排序python实现

    基数排序python实现 基数排序 基数排序(英语:Radix sort)是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较.由于整数也可以表达字符串(比如名字或 ...

  6. openSUSE Leap 15.0 Adobe Flash Player 安装说明

    鉴于Firefox安装配置文件: mozilla_lib=file $MOZ_PROGRAM LIB=lib -bit.*(x86-|S/|PowerPC|ARM aarch64)’ &&am ...

  7. Redis学习笔记--Redis数据过期策略详解

    本文对Redis的过期机制简单的讲解一下 讲解之前我们先抛出一个问题,我们知道很多时候服务器经常会用到redis作为缓存,有很多数据都是临时缓存一下,可能用过之后很久都不会再用到了(比如暂存sessi ...

  8. Contest Setting 2018 ICPC Pacific Northwest Regional Contest dp

    题目:https://vj.69fa.cn/12703be72f729288b4cced17e2501850?v=1552995458 dp这个题目网上说是dp+离散化这个题目要对这些数字先处理然后进 ...

  9. SQLite中的WAL机制详细介绍-与回滚日志原理

    一.什么是WAL? WAL的全称是Write Ahead Logging,它是很多数据库中用于实现原子事务的一种机制,SQLite在3.7.0版本引入了该特性. 二.WAL如何工作? 在引入WAL机制 ...

  10. 数据库的未来:ORM+LINQ+RX

    数据库的未来:ORM+LINQ+RX 数据        操作         异步 ORM       LINQ        RX