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. Python while for if....else

    1函数input()的工作原理: 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量(即要向用户显示的提示或说明) raw_input    是Py ...

  2. PAT 1087 有多少不同的值(20)(STL-set代码)

    1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...

  3. andorid 数据储存、SharedPreferences存储和手机内部储存

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  4. RabbitMQ 的基本介绍

    RabbitMQ官网教程:http://www.rabbitmq.com/getstarted.html RabbitMQ 是一个由 Erlang 语言开发的 AMQP 的开源实现.AMQP :Adv ...

  5. java如何实现发邮件功能。

    http://blog.sina.com.cn/s/blog_59ca2c2a01013800.html http://www.cnblogs.com/xdp-gacl/p/4216311.html

  6. c++11 并发 条件变量 超时等待的代码练习

    资料地址 http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until http://www.cnblogs.com/ha ...

  7. Minimum Size Subarray Sum LT209

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  8. 各种编译不通过xcode

    2017-08-24 Apple Mach-O Linker (Id) Error Linker command failed with exit code 1 (use -v to see invo ...

  9. vue动态路由配置,vue路由传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...

  10. Git使用基础篇(zz)

    Git使用基础篇 您的评价:          收藏该经验       Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体 ...