891A - Pride - CodeForces Solution


brute force dp greedy math number theory *1500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#define ll long long
using namespace std;

void solve() {
    int n,i,j,ans=4004,ones=0;
    cin >> n;
    vector<int> a(n);
    for(i=0;i<n;i++) {
        cin >> a[i];
        if(a[i]==1) ones++;
    }
    if(ones==n) {
        cout << 0 << endl;
        return;
    }
    for(i=0;i<n-1;i++) {
        int check=a[i+1];
        for(j=i+1;j<n;j++) {
            check=gcd(check,a[j]);
            if(gcd(a[i],check)==1) ans=min(ans,j-i+n-1);
        }
    }
    if(ans==4004) cout << -1 << endl;
    else cout << ans-ones << endl;
}

int main() {
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int t=1;
    // cin >> t;
    while(t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother