1599C - Bubble Strike - CodeForces Solution


combinatorics math probabilities ternary search *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using indexed_set = tree<T, null_type, less<T>, 
            rb_tree_tag, tree_order_statistics_node_update>;
 
#define ll long long
#define ld long double
#define cy cout<<"YES\n"
#define cn cout<<"NO\n"
 
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{ return (a.second < b.second); }
 
ll fastpow(ll a, ll b, ll m)
{
    if(b==0)return 1;
    if(b%2==0){ll take=fastpow(a,b/2,m);return (take*take)%m;}
    else{ll take=fastpow(a,b-1,m);return (a*take)%m;}
}

void modularInverse(ll n, ll prime, vector<ll> &dp)
{
    dp[0] = dp[1] = 1;
    for (ll i = 2; i <= n; i++)
        dp[i] = dp[prime % i] *
               (prime - prime / i) % prime;   
}

ll ncr(vector<ll> &arr, ll a, ll b, ll mod)
{
    ll ans=(arr[a]*fastpow(arr[b],mod-2,mod))%mod;
    ans=(ans*fastpow(arr[a-b],mod-2,mod))%mod;
    return ans;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen("haybales.in", "r", stdin);
    // freopen("haybales.out", "w", stdout);
    int cases=1;
    // cin>>cases;
    ll mod=1e9+7;
    // Error can be because you didn't input the entreies
    while(cases--)
    {
        ll n;cin>>n;
        double p;cin>>p;
        vector<ll> arr(n+1,1);
        for(ll i=1;i<=n;i++)arr[i]=(arr[i-1]*i);
        ll ans;
        for(ll i=0;i<=n;i++)
        {   
            ll x0=(((n-i)*(n-i-1)*(n-i-2))/6);
            ll x1=i*(((n-i)*(n-i-1))/2);
            ll x2=((i*(i-1))/2)*(n-i);
            ll x3=(((i)*(i-1)*(i-2))/6);
            double z=1.0;
            double it=((2*x2+2*x3+x1)*z)/(2*(x0+x1+x2+x3));
            if(it>=p)
            {
                ans=i;
                break;
            }
        }   
        cout<<ans;
    }
    
    // minimise distance in a matkjrix, make a queue with the source and check for adjacent cells
    // max/min problem ?? try to solve using binary search
    // find result in form of mod ?? never use divide operation
    
    return 0;
}















Comments

Submit
0 Comments
More Questions

13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum