1836B - Astrophysicists - CodeForces Solution


greedy math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define pll pair<ll,ll>
#define mp make_pair
#define M1 1000000007
#define M2 998244353
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (ll)x.size()
#define dbg(x) cout<<#x<<" = "<<x<<endl
#define print(x) for(auto it:x) {cout<<it<<" ";}cout<<endl;
#define pprint(x) for(auto it:x) cout<<it.ff<<" "<<it.ss<<endl;
const int inf = 1000000000;

bool cmp(pair<ll,ll> a,pair<ll,ll> b){
    if(a.ff==b.ff)  return a.ss>b.ss;
    else  return a.ff < b.ff;
}

ll pwr(ll a,ll b){ ll prd=1; while(b--){ prd*=a; } return prd; }

void init_code(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
}

void solve() {
    ll n,k,g;
    cin>>n>>k>>g;
    ll tcoin=k*g;
    ll maxl=0;
    if(g%2==0){
        maxl=(g/2)-1;
    }
    else{
        maxl=(g-1)/2;
    }
    ll ans=0;
    float pep=(float)tcoin/(float)maxl;
    if(maxl==0){
        cout<<0<<endl;
        return;
    }
    if((ll)pep>=n){
        ans+=maxl*(n-1);
        tcoin-=maxl*(n-1);
        ll r=tcoin%g;
        if(r>maxl){
            ans-=(g-r);
        }
        else ans+=r;
    }
    else{
        ans+=k*g;
    }
    cout<<ans<<endl;

 
 
 
}
 
int main() {
 
#ifndef ONLINE_JUDGE
  freopen("output.txt","w",stderr);
#endif
 
init_code();
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
    ll tc;
    tc=1;
    cin>>tc;
    while(tc--){
        solve();
    }
return 0;
}


Comments

Submit
0 Comments
More Questions

870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class