codeforces891a
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.
这个题很简单,就是找一下那个公约数为1
看测试样例1:
2 2 3 4 6
先求他的第一层公约数就是2和2,2和3,3和4,4和6求公约数
2 2 3 4 6
2 1 1 2
这时候发现有1的存在
答案就是 当前的层数-1+n-1 (n就是几个数) 因为有一个1就能把所有的都变成1
注意特判
2
1 1
这种的
丑陋的代码:
#include <iostream>
#include <cstdio>
using namespace std;
long long arr[2005][2005];
long long gcd(long long a,long long b);
int main()
{
long long n,i,j;
int flag = 0;
scanf("%lld",&n);
for(i = 0; i < n; ++i) {
scanf("%lld",arr[0]+i);
if(arr[0][i] == 1)
flag ++;
}
if(flag) {
printf("%lld\n",n-flag);
return 0;
}
for(i = 1; i <= n-1; ++i)
{
for(j = 0; j < n - i; ++j)
{
arr[i][j] = gcd(arr[i-1][j], arr[i-1][j+1]);
if(arr[i][j] == 1) {
printf("%lld\n",i+n-1);
return 0;
}
}
}
printf("-1\n");
}
long long gcd(long long a,long long b)
{
return b == 0?a:gcd(b,a%b);
}
codeforces891a的更多相关文章
随机推荐
- Liunx cp
功能: 复制文件或目录 使用权限:所有使用者说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同 ...
- mybatis入门--初识mybatis
初识mybatis 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis.所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子的. ...
- 64位Win7系统下vs2010调试无法连接oracle解决办法
具体的解决办法如下: 1.先将WebDev.WebServer20.EXE和WebDev.WebServer40.EXE文件从Program Files (x86)目录中拷贝出来放到c:\dev目录中 ...
- Java 7.35 游戏:猜字游戏(C++&Java)
Ps: 有人可能好奇我为什么大费周章的去写主函数,而不直接把所有操作放在类中,Why?类就好比骨干(存放核心的数据和功能),最好提供接口, 避免了类中的输入输出,当然,这也不是绝对的. C++: #i ...
- PAT 甲级 1008 Elevator (20)(代码)
1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...
- PAT 1007 素数对猜想(20)
1007 素数对猜想(20 分) 让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数.显然有d1=1,且对于n>1有dn是偶数."素 ...
- django admin管理后台中文添加问题
django版本号 1.7.8 #create database mydb character set utf8;#django-admin.py startproject mysite#设置sett ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- sed删除行
删除文件中含有$word字符串的某些行(在文件中修改) sed -i '/$word/d' file
- Composer 中文镜像 Lavavel-china 公益项目
『Composer 中国全量镜像』是由 Laravel China 社区联合 又拍云 与 优帆远扬 共同合作推出的公益项目,旨在为广大 PHP 用户提供稳定和高速的 Composer 国内镜像服务. ...