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的更多相关文章
随机推荐
- java 字符串截取类 区分中文、英文、数字、标点符号
package com.founder.fix.ims; /** * @author WANGYUTAO * 操作字符串 */ public class SubString { // public s ...
- 在centos7上安装elasticSearch
因为elasticsearch是用java编写的,所以需要先安装JDK: jdk1.8的安装:安装指导 elasticsearch的下载和安装 一.下载elasticSearch 1.下载地址: ht ...
- 全基因组测序 Whole Genome Sequencing
全基因组测序 Whole Genome Sequencing 全基因组测序(Whole Genome Sequencing,WGS)是利用高通量测序平台对一种生物的基因组中的全部基因进行测序,测定其 ...
- BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防
一道树的直径 树网的核 BZOJ原题链接 树网的核 洛谷原题链接 消防 BZOJ原题链接 消防 洛谷原题链接 一份代码四倍经验,爽 显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍 ...
- boost asio 一个聊天的基本框架
示例代码 #include "Util.h" #include "MyAsio.h" #include "TcpConnectionManager.h ...
- 蛋白序列GO号注释及问题
#=============================== 版本1 ===============================================InterProSc ...
- mysql主从配置思路
记录一下 原文:http://www.rjfw.com.cn/qamain/prevView.action?id=40482017200000031 mysql主从配置(清晰的思路) mysql主从配 ...
- syslog系统日志、事件日志分析、EventLog Analyzer
syslog系统日志.事件日志分析.EventLog Analyzer Eventlog Analyzer是用来分析和审计系统及事件日志的管理软件,能够对全网范围内的主机.服务器.网络设备.数据库以及 ...
- 检测鼠标是否在UI上unity
public static bool IsCursorOnUI(int inputID=-1){ EventSystem eventSystem = EventSystem.current; retu ...
- 第1章 Python数据模型
#<流畅的Python>读书笔记 # 第一部分 序幕 # 第1章 Python数据模型 # 魔术方法(magic method)是特殊方法的昵称.于是乎,特殊方法也叫双下方法(dunder ...