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 ...
随机推荐
- flex布局与ellipsis冲突问题
在flex布局里使用text-overflow: ellipsis;发现没有省略. 解决方案 .g-flex-c{ flex: 1; min-width: 0; }
- mongodb windows 4 zip安装
安装mongoDB目的:学习Express,顺带mongodb. 本文目的: 4.0.2的mongodb在windows7上竟然安装不了. 没办法,用压缩包手动安装吧... 安装环境:win7sp1x ...
- gets函数的完美替代
众所周知 在C语言中scanf用来读取一行字符串时遇到空格或回车会停止 而若要读入一行带空格的字符串时 有些人会用gets来代替 然而,gets的最大问题在于:会读取超过数组长度上限个字符,而超出长度 ...
- Seven super tips for successful selling on Amazon
Check for orders daily: To ship quickly you need to find out about orders promptly. We will e-mail y ...
- 【转】React-Native 实现增量热更新的思路
所谓热更新就是在不重新安装的前提下进行代码和资源的更新,相信在整个宇宙中还不存在觉得热更新不重要的程序猿. 增量热更新就更牛逼了,只需要把修改过和新增的代码和资源推送给用户下载即可,增量部分的代码和资 ...
- 在Asp.Net中使用Redis【本文摘自智车芯官网】
Redis安装 在安装之前需要获取Redis安装包.在这里我们就不详细介绍安装包的获取了.这里Redis-x64-3.2.100.zip安装包为例通过dos命令取安装.通过dos命令找到安装目录. 在 ...
- oracle与DB2的一些架构
首先,我们需要理解 Oracle 使用的架构,并理解它与 DB2 的不同之处.图 1 展示了 Oracle 的系统结构.将该图与 图 2 进行比较,后者显示了 DB2 的系统结构.在阅读本文的时候,为 ...
- 【第八周】【新蜂】新NABCD
由小组成员宫成荣撰写 一.小组项目申请时提交的NABCD: 痛点:普通的俄罗斯方块是不现实距离下一级有多远的,我们的游戏能显示距离下一等级游戏有多远.方便玩家体验. nabc: n:能满足大多数玩家的 ...
- linux svn启动和关闭
linux svn启动和关闭 博客分类: linux系统 svnlinux 1,启动SVN sudo svnserve -d -r /home/data/svn/ 其中 -d 表示守护进程, -r ...
- redis 入手
redis是数据库,数据库肯定是处理处理数据.既然是处理数据,无非就是增删查改 redis用于操作键的命令基本上分为两大类 对任何键都可执行的: DEL 命令. EXPIRE 命令. RENAME 命 ...