SRM 802 DIV2 Hard by DP

Problem description here DisjointDiceValues

We will solve it by considering Alice’s win probability. Let’s denote it by P(Alice).

P(Alice) = (number of events where Alice wins) / (total events)
total events = 6^(A+B)

We will use DP to calculate the number of events where Alice wins.

Consider an array of (A+B) elements where first A elements are the dice values drawn from Alice and last B elements are the dice values from Bob. We will fill out these elements recursively and at the same time we will have two bitmasks to represent unique dice values found by Alice and Bob respectively.

So the dp state will look like this:

dp[index][maskAlice][maskBob]

This dp state answers the question of “How many events are possible where Alice wins given that index number of elements have already been filled up and Alice has picked up certain unique values represented by maskAlice and Bob has picked up certain unique values represented by maskBob?”

Base Case: index = A+B, and (maskAlice & maskBob) > 0. Meaning we filled up all A+B elements and there is a dice value common in Alice and Bob’s elements.

My code after contest: (it passed all sample tests)

#include<bits/stdc++.h>
using namespace std;
#define forl(i,a,b) for ( i = a; i < b; i++)

long long int dp[20][130][130];
int n,m;

bool iscommon(int m1, int m2){
    int i;
    forl(i,1,7){
        if((m1&(1<<i)) and (m2 &(1<<i)))
            return true;
    }
    return false;
}

long long int doit(int ind, int mask1, int mask2){
    if(ind == n+m){
        return iscommon(mask1, mask2);
    }
    if(dp[ind][mask1][mask2] != -1)
        return dp[ind][mask1][mask2];
    long long int ans = 0;
    //fill Alice's elements
    int i,j,k;
    if(ind <n){
        forl(i,1,7){
            ans += doit(ind+1, mask1|(1<<i), mask2);
        }
    }
    else{//fill Bob's elements
        forl(i,1,7){
            ans += doit(ind+1, mask1, mask2|(1<<i));
        }
    }
    
    dp[ind][mask1][mask2] = ans;
    return ans;
}
class DisjointDiceValues {
    public:
    double getProbability(int A, int B) {
        long long int total = pow(6, A+B);
        n = A;
        m = B;
        int i,j,k,a,b;
        forl(i,0,20){
            forl(k,0,130){
                forl(a,0,130){
                    dp[i][k][a] = -1;
                }
            }
        }

        long long int r = doit(0,0,0);
        double ans = r;
        ans /= total;
        return ans;
    }
};

Topcoder SRM 800 div2 medium

Topcoder achieved a milestone by arranging 800 Single Round Algorithm Matches (SRM) to the coder all over the world. Topcoder arranged a virtual party before this 800th match. Among this 800 matches, I participated in over 150 matches. (See my Topcoder profile). Thank you Topcoder for helping me improve my coding skill.

Let’s get back to the problem. This problem was worth 400 points. Please take a look at the problem statement as I am not gonna repeat the problem statement.

Look at the above picture to visualize this problem. As like this picture, our goal is to get letter ‘c’ (cherry) in all reels.

Key insights from this problem:

  • There are maximum 5 reels.
  • Each reel has maximum 10 fruits (or letters) in a circular fashion.
  • From these two constraints, we know that there could be maximum 10*10*10*10*10 = 10^5 different display strings you will see based on positions of the fruits although two different configurations might end up with same display string as there could be duplicate letters in a reel.

    My approach for this problem is to play the slot machine rounds and keeping track of configurations in a map or dictionary. If a configuration repeats before getting a jackpot, that basically means the game entered into a cycle and we can return “impossible” in this case. Each configuration corresponds to a set of position indices of current display letters from all reels. Now the question is – how to store these configurations in map? My approach is to use unique letters to denote each index across all reels. For example ‘a’ is for index 0, ‘b’ is for index 1 and so on. The good thing is we will never run out of letters to represent indices as there would be maximum 10 letters (meaning 10 positions) in each reel. Remember that, this configuration letters are different from the letters in each reel.

    After each round of play, we have to adjust the positions for next round. See the code below for a better understanding of my approach.

    #include<bits/stdc++.h>
    using namespace std;
    
    class SlotMachineHacking {
    public:
    int win(vector reels, vector steps) {
    map uniq_configs;
    string jackpot = ""; //jackpot string: all 'c'
    int i,j,k;
    for(i = 0;i<reels.size();i++)
    {
    jackpot = jackpot + 'c';
    }
    int cnt = 0;
    int n = reels.size();
    int pos[n]; //indices of current display letters for all reel strings
    for(i=0;i<n;i++)
    {
    pos[i] = 0;
    }
    auto newpos = [&pos, &reels, &steps](int n){
    int i;
    for(i=0;i<n;i++){
    pos[i] = (pos[i]+ steps[i])%reels[i].size();
    }
    };
    while (true){
    string curr_display = "";
    newpos(n);
    for(i=0;i<n;i++)
    {
    curr_display += reels[i][pos[i]];
    }
    if(curr_display == jackpot)  //current display matches jackpot
    return cnt;
    string curr_config= "";
    for(i = 0;i<n;i++){
    curr_config = curr_config + char(('a'+pos[i]));
    }
    if(uniq_configs[curr_config])
    return -1;
    uniq_configs[curr_config] = 1;
    cnt += 1;
    }
    return -1;
    }
    };
    
    

    In the above code you will see a c++ closure defined.

    Another approach is to run the simulation for 10^5 times without keeping a map

    more specifically,

    number of rounds, R = Length(reels[0])*Length(reels[1])*…..Length(reels[n-1]);
    n is total number of reels.

    So if you don’t hit jackpot during this many rounds, you will never hit jackpot. Because if you do not enter into cycle during this many rounds, you will cycle in the very next round (i.e R+1‘th round)

    References

    1. image source