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 ...
随机推荐
- Centos下安装并设置nginx开机自启动
一.在centos环境下安装下载并安装nginx,由于nginx需要依赖一些环境才能安装,主要依赖g++.gcc.openssl-devel.pcre-devel和zlib-devel这些环境,首先得 ...
- libCurl 初步认识 - cur easy
cur easy接口简洁明了,主接口4个,辅接口5个. 主接口 初始化 + 配参数 + 执行 + 销毁 初始化 CURL* curl_easy_init() 获得CURL句柄,返回值需要判空. 配参数 ...
- NO.2:自学python之路------变量类型、列表、字典
引言 本周初步认识了库,并学习了Python中各种类型的变量和常用操作.并完成了较为完善的用户与商家购物界面设计. 正文 模块: Python有标准库和第三方库.第三方库需要安装才能使用.大量的库可以 ...
- 基于 Agent 的模型入门:Python 实现隔离仿真
2005 年诺贝尔经济学奖得主托马斯·谢林(Thomas Schelling)在上世纪 70 年代就纽约的人种居住分布得出了著名的 Schelling segregation model,这是一个 A ...
- java面向对象的有序数组和无序数组的比较
package aa; class Array{ //定义一个有序数组 private long[] a; //定义数组长度 private int nElems; //构造函数初始化 public ...
- 王者荣耀交流协会scrum立会20171111
1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:高远博 2.时间跨度: 2017年11月10日 18:00 - 18:33 ,总计33分钟. 3.地 点: 一 ...
- EF动态排序
转载的代码,改天再研究 public PageData<T> FindAll(int PageIndex, int PageSize, Expression<Func<T, b ...
- QMultiMap使用
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMultiMap使用 本文地址:http://techieliang.com/201 ...
- 【第二周】Java实现英语文章词频统计
1.需求:对于给定的英文文章进行单词频率的统计 2.分析: (1)建立一个如下图所示的数据库表word_frequency用来存放单词和其对应数量 (2)Scanner输入要查询的英文文章存入Stri ...
- 在DBGrid中实现多选功能
1.首先把DBGrid->options-dgMulitSelect设为True. dgRowSelect也设为True,此属性设为true后,DBGrid将不能编辑,如何实现能否编辑代码如下 ...