题目链接:https://codeforces.com/problemset/problem/891/A
如果原序列有 1 ,答案等于 n 减一的个数
没有 1 ,则找出公约数最短的序列长度
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 2010;
int n;
int a[maxn],dp[maxn][maxn],g[maxn][maxn];
int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
n = read();
int mi = 1000000007;
int f=0;
for(int i=1;i<=n;++i){
a[i] = read(), g[i][i] = a[i];
if(g[i][i] == 1){
++f;
mi = 0;
}
}
for(int l=2;l<=n;++l){
for(int i=1;i<=n;++i){
int r = min(n,i+l-1);
g[i][r] = gcd(g[i][r-1],a[r]);
if(g[i][r] == 1){
mi = min(mi,l-1);
}
}
}
if(mi == 1000000007){
printf("-1
");
}
else if(f>0){
printf("%d
",n-f);
}
else{
printf("%d
",mi + n - 1);
}
return 0;
}