Algorithm


 Problem Statement for TestBettingStrategy

Problem link- https://community.topcoder.com/stat?c=problem_statement&pm=7422&rd=10663

Problem Statement

    

You are thinking of using the following betting strategy: in the first round, you bet one dollar. If you win the bet, you win the dollar and bet another dollar in the next round. Otherwise you lose the dollar and bet two dollars in the second round (provided you still have at least two dollars in your account). If you win, you get the two dollars and bet one dollar in the third round, otherwise you lose the two dollars and bet four dollars in the third round (provided you have at least that amount in your account) and so on. In other words, whenever you lose a bet, you double the value of the bet for the next round. If you don't have enough money to cover your bet, you have to stop betting. Whenever you win, the bet for the next round will be one dollar.

For example, if you start with 10 dollars, and you win the bet in the first round, lose the bet in the next two rounds and then win the bet in the fourth round, you will end up with 10+1-1-2+4 = 12 dollars.

You will be given four ints initSum, the amount of money you initially have, goalSum, the sum you wish to achieve, rounds, the maximum number of rounds you wish to play, and prob, the percent probability of winning one round. Return the probability that you will reach your goal within the maximum number of rounds you wish to play. Note that if you manage to get to goalSum or more, you stop betting.

 

Definition

    
Class: TestBettingStrategy
Method: winProbability
Parameters: int, int, int, int
Returns: double
Method signature: double winProbability(int initSum, int goalSum, int rounds, int prob)
(be sure your method is public)
    
 
 

Notes

- The returned value must have an absolute or relative error less than 1e-9.
 

Constraints

- initSum will be between 1 and 1,000, inclusive.
- goalSum will be between (initSum + 1) and 1,000, inclusive.
- rounds will be between 1 and 50, inclusive.
- prob will be between 0 and 100, inclusive.
 

Examples

0)  
    
10
11
4
50
Returns: 0.875
You have a 50% chance of reaching 11 dollars in the first round. You could also win by losing the first round and winning the second, with a 25% chance, or losing the first two rounds and winning the third one, with a 12.5% chance. Note that the fourth round is never needed. If you lose the first three rounds, you can't cover your fourth bet. In any other case, you will have already reached 11 dollars and stopped.
1)  
    
10
20
20
50
Returns: 0.3441343307495117
 
2)  
    
10
20
10
90
Returns: 0.34867844010000015
You have to win every round. Since the probability of winning a round is pretty high, you have a decent chance of doing this.
3)  
    
24
38
24
60
Returns: 0.5940784635646947
 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

double dp[1001][51][51];

class TestBettingStrategy {
  double rec(int sum, int rem, int cur, int &gol, double prb) {
    if(sum >= gol)
      return 1;
    if(sum <= 0 || rem <= 0 || pow(2, cur) > sum)
      return 0;

    double &ret = dp[sum][rem][cur];
    if(ret == ret)
      return ret;
    ret = 0;

    return ret = prb * rec(sum + pow(2, cur), rem - 1, 0, gol, prb) +
                 (1.0 - prb) * rec(sum - pow(2, cur), rem - 1, cur + 1, gol, prb);
  }

public:
  double winProbability(int initSum, int goalSum, int rounds, int prob) {
    memset(dp, -1, sizeof dp);
    return rec(initSum, rounds, 0, goalSum, 1.0 * prob / 100.0);
  }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
11
4
50

Output

x
+
cmd
0.875
Advertisements

Demonstration


TopCoder Solution SRM339-D1-500 Statement for TestBettingStrategy C,C++, Java, Js and Python ,SRM339-D1-500,TopCoder Solution