Codeforces 892 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 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?
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.
题目大意:给你一个长度为n的数组,每次可以进行一种操作把第i个数和第i+1个数的gcd替换为第i个数或者第i+1个数,问最少多少步能够使得序列全部变成1.
分析:对着样例手玩一下就能发现要先把其中的一个数变成1,再用这个1把其它的数全部变成1,关键就是怎么用最少的步数把其中的一个数变成1.我一开始的想法是找一对距离最近且gcd=1的数,借助中间的数把它们联系在一起.事实上这样是错的.比如42 35 15,这是可以变成1的,然而却找不到一对互素的数,再来考虑这个过程,并不需要两个数互素才行,可以把这两个数分别往中间传递,每次取gcd,这样枚举一个左端点和一个右端点判断一下gcd是否为1就好了.
- #include <cstdio>
- #include <cmath>
- #include <queue>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int inf = 0x7fffffff;
- int n, a[], minn = inf;
- int flag = ;
- int gcd(int x, int y)
- {
- if (!y)
- return x;
- return gcd(y, x % y);
- }
- int main()
- {
- scanf("%d", &n);
- for (int i = ; i <= n; i++)
- {
- scanf("%d", &a[i]);
- if (a[i] == )
- flag++;
- }
- int temp = a[];
- for (int i = ; i <= n; i++)
- temp = gcd(temp, a[i]);
- if (temp != )
- puts("-1");
- else
- if (flag)
- printf("%d\n", n - flag);
- else
- {
- for (int i = ; i <= n; i++)
- {
- int temp = a[i];
- for (int j = i - ; j >= ; j--)
- if ((temp = gcd(temp, a[j])) == )
- {
- minn = min(minn, i - j);
- break;
- }
- temp = a[i];
- for (int j = i + ; j <= n; j++)
- if ((temp = gcd(a[i], a[j])) == )
- {
- minn = min(minn, j - i);
- break;
- }
- }
- printf("%d\n", minn + n - );
- }
- return ;
- }
Codeforces 892 C.Pride的更多相关文章
- codeforces 892 - A/B/C
题目链接:https://cn.vjudge.net/problem/CodeForces-892A Jafar has n cans of cola. Each can is described b ...
- Codeforces 892 D.Gluttony
D. Gluttony time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces 892 B.Wrath
B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Codeforces 892 A.Greed
A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony
A 链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...
- 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 ...
- 【Codeforces Round #446 (Div. 2) C】Pride
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...
- Codeforces 892C/D
C. Pride 传送门:http://codeforces.com/contest/892/problem/C 本题是一个关于序列的数学问题——最大公约数(GCD). 对于一个长度为n的序列A={a ...
- Codeforces Round #446 (Div. 2)
Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...
随机推荐
- eclipse启动一闪而退
eclipse启动一闪而退 打开eclipse,启动画面一闪而过退出. 解决方法: 以下每一步结束都重启eclipse一下,看能不能正常启动. 1. 在C:/WINDOWS/system32 系统文件 ...
- underscore.js源码解析(三)
最近工作比较忙,做不到每周两篇了,周末赶着写吧,上篇我针对一些方法进行了分析,今天继续. 没看过前两篇的可以猛戳这里: underscore.js源码解析(一) underscore.js源码解析(二 ...
- underscore.js源码解析(二)
前几天我对underscore.js的整体结构做了分析,今天我将针对underscore封装的方法进行具体的分析,代码的一些解释都写在了注释里,那么废话不多说进入今天的正文. 没看过上一篇的可以猛戳这 ...
- ADAS芯片解决方案汇总
ADAS(高级辅助驾驶系统),是指利用安装于车上各式各样的传感器,在第一时间收集车内的环境数据,进行静.动态物体的辨识.侦测与追踪等技术上的处理,从而能够让驾驶者在最快的时间察觉可能发生的危险. 在过 ...
- $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 区别
PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别 PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 通常情况下,PHP ...
- 1019psp
1.本周psp: 2.本周进度条: 3.累计进度图(折线图): 4.psp饼状图:
- Alpha 冲刺(8/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 多次测试软件运行 学习OPENMP ...
- SGU 199 Beautiful People 二维最长递增子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...
- Qt使用QNetworkAccessManager实现Ftp操作
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt使用QNetworkAccessManager实现Ftp操作 本文地址:http: ...
- Java 面试前的基础准备 - 01
使用这个在线网页编辑真的是不习惯,还是 windows live writer 好. 下面列一个清单用于最近的面试:( 清单是网上down的 ) static,final,transient 等关键字 ...