Codeforces Round #446 (Div. 2) C. Pride【】
2 seconds
256 megabytes
standard input
standard output
You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacentelements from a, say x and 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?
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.
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.
5
2 2 3 4 6
5
4
2 4 6 8
-1
3
2 6 9
4
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
Codeforces Round #446 (Div. 2) C. Pride【】的更多相关文章
- Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】
B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #446 (Div. 2) A. Greed【模拟】
A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #446 (Div. 2)
Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...
- 【Codeforces Round #446 (Div. 2) C】Pride
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...
- 【Codeforces Round #446 (Div. 2) B】Wrath
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...
- 【Codeforces Round #446 (Div. 2) A】Greed
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long l ...
- Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...
- Codeforces Round #451 (Div. 2) A. Rounding【分类讨论/易错】
A. Rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #164 (Div. 2) A. Games【暴力/模拟/每个球队分主场和客场,所有球队两两之间进行一场比赛,要求双方球服颜色不能相同】
A. Games time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
随机推荐
- 《Cracking the Coding Interview》——第4章:树和图——题目2
2014-03-19 03:32 题目:给定一个有向图,判断其中两点是否联通. 解法:DFS搜索解决,如果是无向图的话,就可以用并查集高效解决问题了. 代码: // 4.2 Write a progr ...
- 使用Visual Studio 快速把 Json,Xml 字符串创建为一个实体类
- 基类VS接口
该篇引用 CLR via C# 中的13.11节. 应该设计基类还是接口,这个问题不能一概而论,下面提供一些指导性原则: 1. IS_A关系(指属于,例如汽车属于交通工具) vs CAN_DO关系(指 ...
- loadrunner检查点设置失败,日志中SaveCount无法被正常统计出来
在脚本正确的情况下的web_reg_find检查点检查失败,SaveCount无法被正常统计出来. 在检查项Text为中文的情况下, ******(我是被录制下来的代码) web_reg_find(& ...
- java中利用正则表达式获取a标签
// 设置新闻内容 notice.setContent(editorValue); Matcher m = Pattern.compile("<a[^>]*>([^< ...
- django-settings里redis连接与缓存配置
# Django-redis的缓存配置 CACHES = { "default": { "BACKEND": "django_redis.cache. ...
- 浅谈传输层协议TCP和UDP
在当今因特网的层次结构中,传输层的协议主要有两种,其一为Transmission Control Protocol,即TCP:其二为User Datagram Protocol,即UDP. 1.TCP ...
- [ecmagent][redis学习][1初识redis] redis安装+redis快速教程+python操作redis
# redis安装 # redis安装教程 -- 服务器(ubuntu)安装redis服务 sudo apt-get install redis-server -- 源码安装 -- $ wget ht ...
- HDU 1939 HE IS OFFSIDE
He is offside! Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...