倘若存在 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. MDX之Case When用法

    with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...

  2. myBatis-类型关联

    1.一对多 collection <resultMap id="deptsql" type="Dept"> <id column=" ...

  3. h5旋转效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. systemback-----做你折腾的后盾

    http://imcn.me/html/y2015/24421.html ubuntu的系统还原工具,最近在学习grunt,要安装nodejs 等一些依赖,对于有轻微系统洁癖的我来说是个好的解决方案, ...

  5. js实现接口的几种方式

    Javascript模仿接口可以有三种方式:1.注释法 2.检查属性法 3.鸭式辨形法 1.注释法:此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉 /* interface People{ ...

  6. 关于搭建系统直播和Thinkphp的杂谈(持续更新)

    Q:Access denied for user 'root'@'localhost' 错误 A:第一种:配置文件中把数据库的用户名密码再改一遍,把runtime里的文件删除  第二种:修改syste ...

  7. C# 一维数组 二位数组 多维数组

    什么是数组? 数组是一组变量,就是把一些变量串在一起,放在一块. 数组的作用? 假设有一堆变量,每个变量都有一些程序,那么这堆程序放在一起  程序就会混乱,处理起来有些麻烦,那么数组就是把这些变量放在 ...

  8. SQL2005中使用backup、restore来备份和恢复数据库

    在SQL2005数据库中利用SQL语句进行数据备份与还原: 备份backup:backup database 数据库名称 tO disk = 备份路径例:BACKUP DATABASE test TO ...

  9. GNU make(2)

    GNU make(2) 参考: GNU Make学习总结(二) 变量 变量由一个前导符号$加上字符或者是括号字符组成, 名称区分大小写. 命名: 习惯上用全部大写字符表示常量, 小写字符表示变量, 单 ...

  10. Tensorflow_入门学习_2_一个神经网络栗子

    3.0 A Neural Network Example 载入数据: from tensorflow.examples.tutorials.mnist import input_data mnist ...