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的更多相关文章
随机推荐
- Python 字符串(count)
字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...
- css font-size=0的妙用
转自:css font-size=0有什么妙用? 回答一: 问题的根源是 inline(a标签默认是display:inline) 和 inline-block (.list-info 设置的是 di ...
- ssm介绍
1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE ...
- suricata 原文记录
如何在 Linux 系统上安装 Suricata 入侵检测系统 编译自:http://xmodulo.com/install-suricata-intrusion-detection-system-l ...
- 如何搭建http服务仓库
1.拷贝仓库repo-A文件到服务器/media/D: 2.通过createrepo_c 生成仓库rpm信息数据 cd repo-A createrepo . 3.chmod -R 775 repo ...
- 第五周Java学习总结(补)
第五周java学习内容(补) 学习内容: File类方法的操作 public String getName() public boolean canRead() public boolean canW ...
- Spring ApplicationContext(八)事件监听机制
Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...
- PHP ini 配置无效的坑给自己记录
装redis 扩展时,发现装成功之后扩展一直加载不上, 于是phpinfo 发现Configuration File (php.ini) Path:none Loaded Configuration ...
- python 字符串占位符的使用
name2='我是{} 我的专业是 {}'.format('张三','计算机科学技术')print(name2)
- nginx的hash
hash结构中有若干个桶,桶内是hash(key)值相同的若干数据. 查找数据时,首先对key值进行hash计算,然后hash值对桶的个数进行求余,得到数据所在的桶.然后在桶中使用key逐个查找,直到 ...