cf891a Pride

倘若存在 1,那么答案是 \(n-cnt_1\)
否则,设最短的公约数为 1 的区间长度为 \(minlen\),答案是 \(minlen-1+n-1\)

#include <iostream>
#include <cstdio>
using namespace std;
int n, ans, gcd[2005][2005], cnt;
int getGcd(int x, int y){
	return !y?x:getGcd(y, x%y);
}
int getAns(){
	if(cnt)	return n-cnt;
	for(int l=2; l<=n; l++)
		for(int i=1; i<=n-l+1; i++){
			int j=i+l-1;
			gcd[i][j] = getGcd(gcd[i][j-1], gcd[j][j]);
			if(gcd[i][j]==1)	return n+l-2;
		}
	return -1;
}
int main(){
	cin>>n;
	for(int i=1; i<=n; i++){
		scanf("%d", &gcd[i][i]);
		if(gcd[i][i]==1)	cnt++;
	}
	ans = getAns();
	cout<<ans<<endl;
	return 0;
}
posted @ 2018-01-21 22:05  poorpool  阅读(160)  评论(0编辑  收藏  举报