倘若存在 1,那么答案是 \(n-cnt_1\)。

否则,设最短的公约数为 1 的区间长度为 \(minlen\),答案是 \(minlen-1+n-1\)。

#include <iostream>
#include <cstdio>
using namespace std;
int n, ans, gcd[2005][2005], cnt;
int getGcd(int x, int y){
return !y?x:getGcd(y, x%y);
}
int getAns(){
if(cnt) return n-cnt;
for(int l=2; l<=n; l++)
for(int i=1; i<=n-l+1; i++){
int j=i+l-1;
gcd[i][j] = getGcd(gcd[i][j-1], gcd[j][j]);
if(gcd[i][j]==1) return n+l-2;
}
return -1;
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
scanf("%d", &gcd[i][i]);
if(gcd[i][i]==1) cnt++;
}
ans = getAns();
cout<<ans<<endl;
return 0;
}

cf891a Pride的更多相关文章

  1. 《傲慢与偏见》(Pride and Prejudice)

    <傲慢与偏见>(Pride and Prejudice)改编自英国作家简·奥斯汀的同名小说,1940年上映.讲述了19世纪初期英国的一个普通的中产家庭中五姐妹的爱情与择偶故事.片中因为男主 ...

  2. Codeforces 892 C.Pride

    C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  3. Codeforces Round #446 (Div. 2) C. Pride【】

    C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.

    Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.七宗罪:暴食.贪婪.懒惰.暴怒.傲慢.色欲.妒忌.

  5. codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony

    A  链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...

  6. A. Pride

    You have an array a with length n, you can perform operations. Each operation is like this: choose t ...

  7. A. Pride (emmmm练习特判的好题)

    题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...

  8. 【Codeforces Round #446 (Div. 2) C】Pride

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...

  9. Lesson 22 A glass envolops

    Text My daughter, Jane, never dreamed of receiving a letter from a girl of her own age in Holland. L ...

随机推荐

  1. vue项目打包后文本溢出代码消失问题

    补充 https://www.cnblogs.com/richard1015/p/8526988.html vue webpack 打包编译-webkit-box-orient: vertical 后 ...

  2. atomic用法

    memory order 源码变成可执行程序,一般由预编译,编译,汇编,链接.源码重排序一般分为编译期重排序和运行期重排序. 编译期重排序:编译器在不改变单线程程序的语义的前提下,可以重新安排语句的执 ...

  3. javascript ES 6 class 详解

    Introduction 上篇文章大致介绍了一些ES6的特性,以及如何在低版本浏览器中使用它们.本文是对class的详解. 译自Axel Rauschmayer的Classes in ECMAScri ...

  4. PL/SQL学习笔记(二)

    select * from protype;select * from product;---笛卡尔连接查询(交叉连接)select * from protype,product;select * f ...

  5. python GIL锁问题

    一.GIL是什么 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple na ...

  6. tomcat 发布本地文件

    应用场景,通过web,jsp访问本地mouse文件夹的静态文件 通过修改tomcat配置文件server.xml <!--在Host标签下加入Context标签,path指的是服务器url请求地 ...

  7. Win7下vc++6.0打开项目出现问题的解决方案

    Win7下vc++6.0打开项目出现Microsoft(R) Developer Studio以及Unable to register this add-in because its DLLRegis ...

  8. 一个简单的例子教会您使用javap

    javap是JDK自带的工具: 这篇文章使用下面这段简单的Java代码作为例子进行讲解. class Outer { Nested nested; Nested getNested() { retur ...

  9. 10048 - Audiophobia (Floyd)

    Floyd的变形,本质是动态规划,路径分成的两个部分中取最大值作为该路径的答案,在所有可行路径之中选一个最小值. #include<bits/stdc++.h> using namespa ...

  10. Django ORM 一对一,一对多,多对多, 添加,批量插入和查询

    模型类 class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_ ...