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. PAT 1063 计算谱半径(20)(代码)

    1063 计算谱半径(20 分) 在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a​1​​+b​1​​i,⋯,a​n​​+ ...

  2. XiaoKL学Python(D)argparse

    该文以Python 2为基础. 1. argparse简介 argparse使得编写用户友好的命令行接口更简单. argparse知道如何解析sys.argv. argparse 模块自动生成 “帮助 ...

  3. How to use bmw icom a2

    Lan Connect Operation Details 1. Connect the LAN cable to ICOM A1/ICOM A2, another side to laptop LA ...

  4. APP强制退出

    第一种方法: 企业版可以用,Appstore可能被拒,慎用 - (void)exitApplication { AppDelegate *app = [UIApplication sharedAppl ...

  5. 程序员"装B"手册

    一.准备工作 “工欲善其事必先利其器.” 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码.总之要显得信息量很大,效率很高. 2.椅子不一定要 ...

  6. Python-多线程之消费者模式和GIL全局锁

    一.生产者和消费者模式 什么是生产者消费者模式 生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯, 所以生产者生产完数据之后不 ...

  7. [网络流]Drainage Ditches(草地排水)

    Drainage Ditches(草地排水) 题目描述 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰 ...

  8. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

  9. python代码检索小引擎

    https://www.programcreek.com/python/

  10. 【Linux】Jenkins+Git源码管理(三)

    摘要 本章介绍Jenkins配合Git源码管理,关于Jenkins的基本操作,参照[Linux]Jenkins配置和使用(二) 事例说明:在linux环境下,安装的jenkins,已安装git. 代码 ...