zoukankan      html  css  js  c++  java
  • 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?

    Input

    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.

    Output

    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.

    Examples
    input
    Copy
    5
    2 2 3 4 6
    output
    Copy
    5
    input
    Copy
    4
    2 4 6 8
    output
    Copy
    -1
    input
    Copy
    3
    2 6 9
    output
    Copy
    4
    Note

    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("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N=3000;
    // name*******************************
    int f[N];
    int ans=inf;
    int a[N];
    int n;
    int cnt=0;
    // 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,1,n)
        {
            cin>>a[i];
            if(a[i]==1)cnt++;
        }
        if(cnt!=0)
        {
            cout<<n-cnt;
            return 0;
        }
    
        For(i,2,n)
        {
            int j=i-1;
            bool flag=false;
            int cnt=0;
            int t=a[i];
            FFor(j,i-1,1)
            {
                cnt++;
                int g=__gcd(a[j],t);
                if(g!=1)
                {
                    t=g;
                }
                else
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
                ans=min(ans,cnt);
        }
        if(ans!=inf)
        {
            ans+=n-1;
            cout<<ans;
        }
        else
            cout<<-1;
    
        return 0;
    }
  • 相关阅读:
    android 近百个源码项目【转】
    503 Service Temporarily Unavailable
    linux 复制文件夹内所有文件到另一个文件夹
    国甲魔方(1)
    大雁展翅,轮回,国甲,国丙,速龙,傲龙,御龙这些魔方有什么区别
    77教师一定要看的15部电影【附下载地址】
    教师一定要看的15部电影
    孩子
    为什么我的 app:actionViewClass="android.widget.SearchView"和app:showAsAction="ifRoom|collapseActionView"才有
    大数据征信 是伪命题?还是金融行业的救世主?
  • 原文地址:https://www.cnblogs.com/planche/p/8778454.html
Copyright © 2011-2022 走看看