A. Pride
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say xand y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.

What is the minimum number of operations you need to make all of the elements equal to 1?

Input

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.

The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

Examples
input
5
2 2 3 4 6
output
5
input
4
2 4 6 8
output
-1
input
3
2 6 9
output
4
Note

In the first sample you can turn all numbers to 1 using the following 5 moves:

  • [2, 2, 3, 4, 6].
  • [2, 1, 3, 4, 6]
  • [2, 1, 3, 1, 6]
  • [2, 1, 1, 1, 6]
  • [1, 1, 1, 1, 6]
  • [1, 1, 1, 1, 1]

We can prove that in this case it is not possible to make all numbers one using less than 5 moves.

这个题很简单,就是找一下那个公约数为1

看测试样例1:

2 2 3 4 6

先求他的第一层公约数就是2和2,2和3,3和4,4和6求公约数

2 2 3 4 6

2 1 1 2

这时候发现有1的存在

答案就是   当前的层数-1+n-1    (n就是几个数)   因为有一个1就能把所有的都变成1

注意特判

2

1 1

这种的

丑陋的代码:

#include <iostream>

#include <cstdio>

using namespace std;

long long arr[2005][2005];

long long gcd(long long a,long long b);

int main()

{

long long n,i,j;

int flag = 0;

scanf("%lld",&n);

for(i = 0; i < n; ++i) {

scanf("%lld",arr[0]+i);

if(arr[0][i] == 1)

flag ++;

}

if(flag) {

printf("%lld\n",n-flag);

return 0;

}

for(i = 1; i <= n-1; ++i)

{

for(j = 0; j < n - i; ++j)

{

arr[i][j] = gcd(arr[i-1][j], arr[i-1][j+1]);

if(arr[i][j] == 1) {

printf("%lld\n",i+n-1);

return 0;

}

}

}

printf("-1\n");

}

long long gcd(long long a,long long b)

{

return b == 0?a:gcd(b,a%b);

}

codeforces891a的更多相关文章

随机推荐

  1. slam14讲证明构成李代数

  2. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  3. hdu 1010(DFS) 骨头的诱惑

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...

  4. (O)编写可维护的代码示例(原创)

    图片轮播: /*广告图片数组*/ var imgs=[ {"i":0,"img":"images/index/banner_01.jpg"} ...

  5. 修改别人写的Hibernate数据库操作代码

    最近正在维护别人写的一个关于Hibernate操作数据库的项目,在运行测试的时候(向表中插入记录),报了一个错误:cannot insert a null into column(XXX字段名,下文统 ...

  6. NGS基础 - 高通量测序原理

    NGS基础 - 高通量测序原理 原创: 赑屃 生信宝典 2017-07-23 NGS系列文章包括NGS基础.转录组分析.ChIP-seq分析.DNA甲基化分析.重测序分析五部分内容. NGS基础系列文 ...

  7. 抽屉效果几大github第三方库

    首先感谢董铂然博客园,鄙人收藏学习之用,如有朋友看到.有需要请直接前往董铂然博客园本文, 请点击查看原文 在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库 ...

  8. tmux 快捷操作

    -- 基本使用 tmux   # 运行 tmux -2 以256终端运行 C-b d  # 返回主 shell , tmux 依旧在后台运行,里面的命令也保持运行状态 tmux ls # 显示已有tm ...

  9. System.Runtime.InteropServices.COMException: 检索 COM 类工厂中 CLSID 为 {0002E510-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154

    这个问题困恼我好几天了,今天终于解决. 开始我在网上左百度右google,都没搜到最终的解决方案,今天我把解决方案贴出来,以供大家分享! 网上有些是报80070005错误的,跟我这个80040154错 ...

  10. 清晰易懂!关于PS入门的超详细笔记!

    给大家分享一篇关于PS入门的超详细笔记!原理讲解清晰明了,虽不是新版本解析,但都是新手学习PS必掌懂的一些知识点,灰常的实用,转走收藏学习! 编辑:千锋UI设计 来源:PS学堂