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

Copy
5
2 2 3 4 6
output

Copy
5
input

Copy
4
2 4 6 8
output

Copy
-1
input

Copy
3
2 6 9
output

Copy
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.

像dp的东西,每次处理前i项,第i项要与第i-1项不互质的话,就需要将第i-1项赋值为其gcd然后往前重复操作至互质,f[i]即为从i点开始向前的最少步数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N=;
// name*******************************
int f[N];
int ans=inf;
int a[N];
int n;
int cnt=;
// function****************************** //***************************************
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin>>n;
For(i,,n)
{
cin>>a[i];
if(a[i]==)cnt++;
}
if(cnt!=)
{
cout<<n-cnt;
return ;
} For(i,,n)
{
int j=i-;
bool flag=false;
int cnt=;
int t=a[i];
FFor(j,i-,)
{
cnt++;
int g=__gcd(a[j],t);
if(g!=)
{
t=g;
}
else
{
flag=true;
break;
}
}
if(flag)
ans=min(ans,cnt);
}
if(ans!=inf)
{
ans+=n-;
cout<<ans;
}
else
cout<<-; return ;
}

A. 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 (emmmm练习特判的好题)

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

  7. cf891a Pride

    倘若存在 1,那么答案是 \(n-cnt_1\). 否则,设最短的公约数为 1 的区间长度为 \(minlen\),答案是 \(minlen-1+n-1\). #include <iostrea ...

  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. Django使用多个数据库

    一.定义数据库 使用Django的多个数据库的第一步是告诉Django将使用的数据库服务器. 这是使用DATABASES设置完成的. 此设置将数据库别名映射到该特定连接的设置字典,该数据库别名是一种在 ...

  2. ConcurrentDictionary的用法

    private static ConcurrentDictionary<Guid, string> dictDbNames = new ConcurrentDictionary<Gu ...

  3. JSP指示元素<%@ %> 与指示类型

    JSP提示元素 <%@ 指示类型  [属性="值"]%> 指示类型 指示类型有三种,分别是taglib.include.page taglib指示类型 用于引入标签库 ...

  4. 成为Java顶尖程序员,先过了下面问题!

    一.数据结构与算法基础 说一下几种常见的排序算法和分别的复杂度. 用Java写一个冒泡排序算法 描述一下链式存储结构. 如何遍历一棵二叉树? 倒排一个LinkedList. 用Java写一个递归遍历目 ...

  5. MySQL主从复制——主库已有数据的解决方案

    在上篇文章中我们介绍了基于Docker的Mysql主从搭建,一主多从的搭建过程就是重复了一主一从的从库配置过程,需要注意的是,要保证主从库my.cnf中server-id的唯一性.搭建完成后,可以在主 ...

  6. Python+Selenium笔记(九):操作警告和弹出框

    #之前发的 driver.switch_to_alert() 这句虽然可以运行通过,但是会弹出警告信息(这种写法3.x不建议使用)  改成 driver.switch_to.alert就不会了. (一 ...

  7. apache web服务器安全配置

    尽管现在购买的云服务器很多都有一键web环境安装包,但是如果是自己配置web环境则需要对各种安全配置十分了解,今天我们就来尝试这做好web服务器安全配置.这里的配置不尽完善,若有纰漏之处还望指出. 修 ...

  8. mysql的几个知识点

    常用命令 原则:能用可视化工具的尽量使用工具,命令行仅限于问题排查. mysql -u ACCOUNT -pPWD -h IP -P port [-D database] //连接数据库 show d ...

  9. Oracle EBS OPM convert dtl reservation

    --convert_dtl_reservation --created by jenrry DECLARE l_reservation_rec mtl_reservations%ROWTYPE; l_ ...

  10. jetty和tomcat比较

    两点,性能极佳,配置简单方便. 主要是设计思想的不同.Tomcat 主要是作为 JSP/Servlet 最新规范的参考实现而设计,属于学院派,但是显得庞大而杂乱.Tomcat 的性能很差,一般是作为 ...