1195B - Sport Mafia - CodeForces Solution


binary search brute force math *1000

Please click on ads to support us..

Python Code:


n, k = input().split(" ")
n = int(n)
k = int(k)

def works(value):
    total_candies_provisioned = (value * (value + 1)) // 2
        return total_candies_provisioned - (n - value) - k

def binary_search():
    if n == 1:
        return 0

    L = 0
    R = 10 ** 9

    while L <= R:
        mid = (L + R) // 2
        result = works(mid)

                                
        if result == 0:
            return n - mid
        elif result < 0:
            L = mid + 1
        else:
            R = mid - 1

print(binary_search())

C++ Code:

#include<bits/stdc++.h>
#define ll long long int
#define whole(v) v.begin(),v.end()
#define rwhole(v) v.rbegin(),v.rend()
#define endl '\n'
#define pb push_back
#define Speedforce ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

ll egcd(ll a, ll b)
{
    if(a%b == 0) return b;
    else return egcd(b, a%b);
}

int main()
{
    Speedforce;
    ll t, n, d, i, j, k, x, y, tmp, tmp1;
    cin >> x >> y;
    
    ll high = x, low = 1, mid = (high+low)/2;
    d = (mid*(mid+1)/2);
    ll ate = x-mid;
    ll left = d-ate;
    while(1)
    {
        if(left == y) break;
        else if(left > y)
        {
            high = mid;
            mid = (high+low)/2;
            d = (mid*(mid+1)/2);
            ate = x - mid;
            left = d-ate;
        }
        else
        {
            low = mid;
            mid = (high+low)/2;
            d = (mid*(mid+1)/2);
            ate = x - mid;
            left = d-ate;
        }

    }
    cout << ate << endl;



}


Comments

Submit
0 Comments
More Questions

1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book