A. Pride
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.
像dp的东西,每次处理前i项,第i项要与第i-1项不互质的话,就需要将第i-1项赋值为其gcd然后往前重复操作至互质,f[i]即为从i点开始向前的最少步数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N=;
// name*******************************
int f[N];
int ans=inf;
int a[N];
int n;
int cnt=;
// function****************************** //***************************************
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin>>n;
For(i,,n)
{
cin>>a[i];
if(a[i]==)cnt++;
}
if(cnt!=)
{
cout<<n-cnt;
return ;
} For(i,,n)
{
int j=i-;
bool flag=false;
int cnt=;
int t=a[i];
FFor(j,i-,)
{
cnt++;
int g=__gcd(a[j],t);
if(g!=)
{
t=g;
}
else
{
flag=true;
break;
}
}
if(flag)
ans=min(ans,cnt);
}
if(ans!=inf)
{
ans+=n-;
cout<<ans;
}
else
cout<<-; return ;
}
A. Pride的更多相关文章
- 《傲慢与偏见》(Pride and Prejudice)
		
<傲慢与偏见>(Pride and Prejudice)改编自英国作家简·奥斯汀的同名小说,1940年上映.讲述了19世纪初期英国的一个普通的中产家庭中五姐妹的爱情与择偶故事.片中因为男主 ...
 - Codeforces 892 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【】
		
C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
 - Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.
		
Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.七宗罪:暴食.贪婪.懒惰.暴怒.傲慢.色欲.妒忌.
 - codeforces #446   892A	Greed  892B  Wrath  892C Pride  891B  Gluttony
		
A 链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...
 - A. Pride  (emmmm练习特判的好题)
		
题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...
 - cf891a Pride
		
倘若存在 1,那么答案是 \(n-cnt_1\). 否则,设最短的公约数为 1 的区间长度为 \(minlen\),答案是 \(minlen-1+n-1\). #include <iostrea ...
 - 【Codeforces Round #446 (Div. 2) C】Pride
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...
 - Lesson 22 A glass envolops
		
Text My daughter, Jane, never dreamed of receiving a letter from a girl of her own age in Holland. L ...
 
随机推荐
- php实现同一时间内一个账户只允许在一个终端登陆
			
在账户表的基础上,我新建了一个账户account_session表,用来记录登录账户的account_id和最新一次登录成功用户的session_id,然后首先要修改登录方法:每次登录成功后,要将登录 ...
 - github 账号创建
			
1.注册 注册地址: https://github.com/join?source=header-home 2.建立组织 (1)点击头像旁边的"+",选择New organiz ...
 - kotlin3-IdeaIU编辑器字体自动放大缩小
 - iframe页面刷新问题
			
1.问题:当iframe的页面加载过再关闭之后,如果iframe的src没有发生变化,js不会重新加载,再次打开页面不会刷新: 2.解决方法:关闭页面时清空src,再次打开时即可重新加载最新数据. $ ...
 - 禅道项目管理软件 为提交Bug页面添加“优先级”字段
			
为提交Bug页面添加“优先级”字段 by:授客 QQ:1033553122 测试环境: 禅道项目管理软件7.1.stable版本 备注:仅适合windows版本,linux下,直接在页面管理后台安装官 ...
 - LeetCode题解之Unique Email Addresses
			
1.题目描述 2.问题分析 将字符串中的 ‘.’ 去掉,将 ‘+’后面直到‘@’的字符串去掉,然后利用set的特性. 3.代码 int numUniqueEmails(vector<string ...
 - Oracle EBS OM 删除订单行
			
DECLARE l_header_rec OE_ORDER_PUB.Header_Rec_Type; l_line_tbl OE_ORDER_PUB.Line_Tbl_Type; l_action_r ...
 - Oracle 数据库 简单查询
			
select DISTINCT dept_id from s_emp; desc s_emp; ; --给入职3年以上员工发10万元年终奖 ; --列出职位是仓库管理员的名字和工资 select la ...
 - Linux内核参数基础优化
			
web 服务负载均衡器常规网站服务器优化的基本配置: net.ipv4.tcp_fin_timeout =2 net.ipv4.tcp_tw_reuse =1 net.ipv4.tcp_tw_recy ...
 - ESXI6.0新添加硬盘未能格式化成功
			
最近练手,手头现有的硬盘是从其他机器上拆下来的,插入ESXI主机上,然后在系统配置硬盘的时候,不能格式化 报错 提示如下错误:"在ESXi"xxx.xxx.xxx.xxx" ...