
Input: ‘N’ = 2, ‘P' = 30, ‘X’ = 29
Output: 1
In this case,
The probability to reach point ‘2(=N)’ while wearing the magic boots is
‘P/100’ = 30/100 = 0.30 i.e. 0.30 > (‘X/100’ = 29/100 = 0.29). Hence the output will be ‘1’.
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of one line.
The first line of input contains three integers, ‘N’, ‘P’, and ‘X’ separated by spaces.
For each test case, print ‘1’ or ‘0’ depending on the above-mentioned conditions.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘P’ <= 100
1 <= ‘X’ <= 100
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
The idea for this approach is to divide the problem into subproblems, calculate their answers individually and then merge them for the main problem.
Here in this problem, when we are at some point ‘X’ then we calculate the probability to reach point ‘N’ from point ‘X+2’, and then multiply the probability to reach ‘X+2’ (i.e. ‘P/100’) by it. Similarly, we calculate the probability to reach point ‘N’ from point ‘X+3’, and then multiply the probability to reach ‘X+3’ (i.e. ‘(100-P)/100’) by it, Then we add both these probabilities to find the probability to reach point ‘N’ from point ‘X’.
Thus the above idea, shows there are two sub-problems involved with every main problem. Thus it can be solved using a recursive algorithm that will return the probability to reach point ‘N’ from that point.
Algorithm:
// This function will return the probability to reach point ‘N’ from the index ‘i’.
Double getProbability(I, N, P)
// The function will return if the magic boots should be worn by Mohan or not.
Int canWear(N, P, X)
The idea for this approach is based on dynamic programming. Let’s suppose we are at point ‘X’ then we either go to point ‘X+2’ with a probability of ‘P/100’ or we go to point ‘X+3’ with a probability of ‘(100-P)/100’ and the same thing happens for point ‘X+2’ and ‘X+3’. Hence it is clearly visible that the main problem can be solved by computing the probability of subproblems and merging them.
Now if we try out every possibility by a recursive algorithm without any optimization that would be O(2^N) algorithm. But if we observe there are many overlapping subproblems involved that don’t need to be re-calculated and hence can be memoized.
For e.g. let’s suppose we are at position ‘X’, then we have to calculate answers to subproblems of positions ‘X+2’ and ‘X+3’,
Now, from point ‘X+2’, we have to calculate answers to subproblems of points ‘X+4’ and ‘X+5’.
And from point ‘X+3’, we have to calculate answers to subproblems of points ‘X+5’ and ‘X+6’.
So from this small sample, it is visible that the answer to the subproblem of point ‘X+5’ is calculated twice (i.e. once from point ‘X+2’ and once from point ‘X+3’), but the answer for that will remain the same hence we can memoize it and reduce the exponential time complexity to linear.
Thus we store the probability to reach point ‘N’ from ‘i’, where ‘i’ ∈ ‘[0, N]’. This can be done using memoization.
The base case for this algorithm is if we reach a point ‘i>N’ then we don’t need to take into account the probability to reach that point as it is ahead of point ‘N’, and in the case of ‘i=N’ we say the probability is ‘1’, as we are already on the point ‘N’.
// This function will return the probability to reach point ‘N’ from the index ‘i’.
Double getProbability(i, N, P, DP)
// The function will return if the magic boots should be worn by Mohan or not.
Int canWear(N, P, X)
The idea for this approach is similar to the memoized DP version, but the only difference is we don’t need a ‘DP’ array of length ‘N+1’, and also ‘DP[i]’ will represent the probability to reach point ‘i’ from ‘0’.
If we observe properly there are only ‘2’ ways to reach some point ‘X’ i.e. either from ‘X-2’ with a probability of ‘P/100’ or from ‘X-3’ with a probability of ‘(100-P)/100’. Hence thus when we iteratively calculate the probability to reach some point ‘X’ we only need to maintain the value of points ‘X-1’, ‘X-2’, and ‘X-3’ (We need to maintain the value of ‘X-1’ because it will be used by point ‘X+1’ in next iteration).
The base case here will be ‘DP[0]’ = ‘1’, ‘DP[1]’ = ‘0’ and ‘DP[2]’ = ‘P/100’.
The transitions will be similar to the previous solution, i.e.
‘DP[i]’ = ‘DP[i-2]*(P/100)’ + ‘DP[i-3]*((100-P)/100)’.
But to make the information useful for further points we remove the ‘i-3’ point and insert the value of point ‘i’ into the end of a list. This can be easily done using deque.
Algorithm:
// The function will return if the magic boots should be worn by Mohan or not.
Int canWear(N, P, X)