codeforce 891 A

题链

构造。

#include<bits/stdc++.h>
using namespace std;
int gcd(int x,int y) {
    return y?gcd(y,x%y):x;
}
#define sight(c) ('0'<=c&&c<='9')
#define INF (int)1e9
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
int n,cnt,a[10007];
int main () {
    read(n);
    int cnt=0;
    for(int i=0; i<n; i++) {
        read(a[i]);if(a[i]==1)cnt++;
    }
    if(cnt) writeln(n-cnt);
    else {
        int t=INF;
        for(int i=0; i<n; i++)
        for(int j=i,cur=0; j<n; j++) 
            if((cur=gcd(cur,a[j]))==1)
                    t=min(t,j-i),j=n;
        if(t==INF)
            writeln(-1);
        else
          writeln(t+n-1);
    } return 0; 
}

 

posted @ 2018-01-24 19:45  泪寒之雪  阅读(143)  评论(0编辑  收藏  举报