1909D - Split Plus K - CodeForces Solution


greedy math number theory

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

// ALIASES
using ll = long long;
using ull = unsigned long long;
using ld = long double;

// CONSTANTS
constexpr ld PI = 3.141592653589793238;
constexpr ll INF = 2e18;
constexpr ld EPS = 1e-16;
constexpr ll MOD = 1e9 + 7;
constexpr ll MOD1 = 998244353;
 
// MACROS
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define mp make_pair 
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define pcl pair<char,ll>
#define plc pair<ll,char>
#define vll vector<ll>
#define vc vector<char>
#define vd vector<double>
#define vvll vector<vector<ll>>
#define vvc vector<vector<char>>
#define vvd vector<vector<double>>
#define vpll vector<pair<ll,ll>>
#define vpcc vector<pair<char,char>>
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define Sort(x) sort(all(x))
#define rev_sort(x) sort(all(x),greater<int>())
#define int long long

// SOLVE HERE 
void solve(int tests)
{
    ll n,k; cin>>n>>k;
    vll a(n);
    ll pn = 0, nn = 0, z = 0;
    for (int i = 0; i < n; ++i){
        cin>>a[i]; a[i]-=k;
        if(a[i]>0) pn++;
        else if(a[i]==0) z++;
        else nn++;
    }
    if(pn == n){
        ll op = 0, g;
        Sort(a); 
        g = a[0];
        for (int i = 1; i < n; ++i)
            g = __gcd(g,a[i]-a[i-1]);
        for (int i = 0; i < n; ++i)
            op += (a[i]-g)/g;
        cout<<op<<'\n';
    }
    else if(nn == n){
        ll op = 0, g;
        for (int i = 0; i < n; ++i)
            a[i]*=-1;
        Sort(a); 
        g = a[0];
        for (int i = 1; i < n; ++i)
            g = __gcd(g,a[i]-a[i-1]);
        for (int i = 0; i < n; ++i)
            op += (a[i]-g)/g;
        cout<<op<<'\n';
    }
    else if(z == n){
        cout<<0<<'\n';
    }
    else cout<<-1<<'\n';    
}

// MAIN FUNCTION 
int32_t main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout << setprecision(12) << fixed;
    // ifstream cin("input.txt");
    // ofstream cout("output.txt");

    int tests = 1;
    cin >> tests;
    for (int tt = 1; tt <= tests; tt++){
        // cout<<"~ Test case : "<<tt<<" ~"<<'\n';
        solve(tt);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}


Comments

Submit
0 Comments
More Questions

1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals