1543C - Need for Pink Slips - CodeForces Solution


bitmasks brute force dfs and similar implementation math probabilities *1900

Please click on ads to support us..

C++ Code:

// Arnav

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <climits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl "\n"


unsigned long long power(unsigned long long x, int y, int p)
{
    unsigned long long res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    // equal to p

    while (y > 0) {

        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
unsigned long long modInverse(unsigned long long n, int p)
{
    return power(n, p - 2, p);
}
unsigned long long mul(unsigned long long x,
                    unsigned long long y, int p)
{
    return x * 1ull * y % p;
}
unsigned long long divide(unsigned long long x,
                        unsigned long long y, int p)
{
    return mul(x, modInverse(y, p), p);
}
unsigned long long nCrModPFermat(unsigned long long n,
                                int r, int p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;
    // if n-r is less calculate nCn-r
    if (n - r < r)
        return nCrModPFermat(n, n - r, p);

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long res = 1;
    // keep multiplying numerator terms and deviding denominator terms in res
    for (int i = r; i >= 1; i--)
        res = divide(mul(res, n - i + 1, p), i, p);
    return res;
}

vector<bool> sieve(int n)
{
    //Time Complexity:- O(log(log(n)))
    vector<bool> is_prime(n+1, 1);
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; i++) 
    {
        if (is_prime[i] && i*i <= n) 
        {
            for (int j = i*i; j<=n; j+=i)
                is_prime[j] = 0;
        }
    }
    return is_prime;
}


#define auto2 vector<pii >::iterator
#define auto1 vector<vector<int> >::iterator
#define auto map<int, int >::iterator
#define pii pair<int,int> 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair

// Code Starts here
const float scale = 1e+6;
float expected(float c,float m,float p,float v)
{
    float ans=p/scale;
    float fc=0,fm=0;
    // cout<<ans<<" ans"<<endl;
    if(c!=0 && m!=0)
    {
        fc=(c/scale)*(1+expected(c-min(c,v),m+min(c,v)/2,p+min(c,v)/2,v));
        fm=(m/scale)*(1+expected(c+min(m,v)/2,m-min(m,v),p+min(m,v)/2,v));
        // cout<<fc<<" "<<fm<<endl;
        return ans+fc+fm;    
    }
    else if(c==0 && m!=0)
    {
        fm=(m/scale)*(1+expected(c,m-min(m,v),p+min(m,v),v));
        // cout<<fm<<endl;
        return ans+fm;
    }
    else if(c!=0 && m==0)
    {
        fc=(c/scale)*(1+expected(c-min(c,v),m,p+min(c,v),v));
        // cout<<fc<<endl;
        return ans+fc;
    }
    else
    {
        return ans;
    }
}

void solve(int tc){
    float c,m,p,v;
    cin>>c>>m>>p>>v;
    // cout<<c<<" "<<m<<" "<<p<<" "<<v<<endl;
    cout<<fixed<<setprecision(8)<<expected(c*scale,m*scale,p*scale,v*scale)<<endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    // for(int tc=1;tc<=t;tc++){
    //     cout<<"Case #"<<tc<<": ";
    //     solve();
    // }

    for (int tc = 1; tc <= t; tc++)
    {
        solve(tc);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings