1780B - GCD Partition - CodeForces Solution


brute force greedy math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> //find_by_order,order_of_key
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define fr               first
#define sc                second
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll


template<typename T> istream &operator>>(istream &input, vector<T> &v) {for (auto &it : v) input >> it; return input;}
template<typename T> ostream &operator<<(ostream &output, vector<T> &v) {for (auto &it : v) output << it << " "; return output;}
template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;}
template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;}

const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const int N = 2*1e5 + 3;
const int INF = 1e17;
const int dx[4] {1, 0, -1, 0}, dy[4] {0, 1, 0, -1};

#ifndef ONLINE_JUDGE
#include "debug.h"
#else 
#define debug(x)
#endif

void Solve() {
    int n;
    cin >> n;
    vector<int> v(n);
    cin >> v;
    vector<int> prf(n,0),suff(n,0);
    prf[0] = v[0];
    int temp = v[0];
    suff[n-1] = v[n-1];
    for(int i = 1;i<n;i++) temp = __gcd(temp,v[i]),prf[i] = prf[i-1]+v[i];
    for(int i = n-2;i>=0;i--) suff[i] = suff[i+1]+v[i];
    int ans = 1;
    for(int i = 0;i<n-1;i++){
        ans = max(ans,__gcd(prf[i],suff[i+1]));
    }
    cout << max(ans,temp) <<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t = 1;
    cin >> t;
    while (t--) {
        Solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence