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 ...
随机推荐
- JSON: jasckson 字段 过滤
有这样一个场景存在一个model类如果User,这里省略了getter/setter方法 class User { String name; String uuid; Long created; Lo ...
- JavaScript数组&类数组转换
一.数组 在JavaScript中数组可以容纳任何类型的值,可以是数字.字符串.对象.甚至其他数组(多为数组) var a = [1,'2',[3]]; a.length;//3 a[0];//1 a ...
- 地区picker 各选择器,优劣分析
移动端选择器picker有很多,各大ui组件都有自己的picker,比如light7,HUI,MUI,jqueryUI等等.但是,我发现他们都有各种各样的问题.这次的地区选择,需要地区的省份+市+经纬 ...
- html5的web存储与cookie的区别
以下从3个方面进行比较: 1,容量:cookie只有4KB,localStorage和sessionStorage最大容量5M 2,是否会携带到ajax中:cookie由每个对服务器的请求来传递,会影 ...
- 搭建ReactNative时的最普遍的错误—— ":CFBundleIdentifier", Does Not Exist
报错 ":CFBundleIdentifier", Does Not Exist 今天搭建Reactnative 报错 注意当你第一次搭建RN时,包体下载的都是最新的版本,由于现在 ...
- Linux 中 awk命令应用
ls -la | awk '{printf ("%8s %8s %8s %8s %8s %8s %8s %8s %8s\n",$1,$2,$3,$4,$5,$6,$7,$8,sub ...
- WiFi 统一管理以及设备自动化测试实践
ATX 安卓设备 WiFi 统一管理以及设备自动化测试实践 (零散知识梳理总结) 此文为转载,感谢作者 目录 众所周知,安卓单台设备的UI自动化测试已经比较完善了,有数不清的自动化框架或者工具.但 ...
- 留言板0.4_model中的数据库(1)
1.先在数据库中加入一天测试数据先 2.在model的"views"中载入数据库和model的类 import pymysql from .models import UserMe ...
- docker in all
docker vs hyper-v,vmware,xen,kvm docker host, docker container, docker engineen, docker image images ...
- Sql server 账号被锁住:"the account is currently locked out. The system administrator can unlock it."的解决办法(转载)
今天遇到的问题比较有意思.首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.Data.SqlClient.SqlExcept ...