Skip to content

Commit e35f1dd

Browse files
committed
Solved problem SRM339-D1-500 from TopCoder
1 parent 2e1869e commit e35f1dd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

TopCoder/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [SRM319-D1-500](http://community.topcoder.com/stat?c=problem_statement&pm=6714&rd=9999)
1111
- [SRM335-D1-500](https://community.topcoder.com/stat?c=problem_statement&pm=7337&rd=10659)
1212
- [SRM338-D1-500](https://community.topcoder.com/stat?c=problem_statement&pm=7289&rd=10662)
13+
- [SRM339-D1-500](https://community.topcoder.com/stat?c=problem_statement&pm=7422&rd=10663)
1314
- [SRM391-D1-500](https://community.topcoder.com/stat?c=problem_statement&pm=8202&rd=11125)
1415
- [SRM394-D2-1000](http://community.topcoder.com/stat?c=problem_statement&pm=8547&rd=11128)
1516
- [SRM458-D2-500](http://community.topcoder.com/stat?c=problem_statement&pm=10726&rd=14180&rm=&cr=14970299)

TopCoder/SRM339-D1-500.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Idea:
3+
- Dynamic Programming.
4+
- dp[sum][rem][cur]:
5+
`sum` represents the current money we have.
6+
`rem` represents the remaining rounds we have.
7+
`cur` represents the number of rounds we had lose.
8+
- In each step we calculate the summation of the probability
9+
of winning or lossing the current round.
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
14+
using namespace std;
15+
16+
double dp[1001][51][51];
17+
18+
class TestBettingStrategy {
19+
double rec(int sum, int rem, int cur, int &gol, double prb) {
20+
if(sum >= gol)
21+
return 1;
22+
if(sum <= 0 || rem <= 0 || pow(2, cur) > sum)
23+
return 0;
24+
25+
double &ret = dp[sum][rem][cur];
26+
if(ret == ret)
27+
return ret;
28+
ret = 0;
29+
30+
return ret = prb * rec(sum + pow(2, cur), rem - 1, 0, gol, prb) +
31+
(1.0 - prb) * rec(sum - pow(2, cur), rem - 1, cur + 1, gol, prb);
32+
}
33+
34+
public:
35+
double winProbability(int initSum, int goalSum, int rounds, int prob) {
36+
memset(dp, -1, sizeof dp);
37+
return rec(initSum, rounds, 0, goalSum, 1.0 * prob / 100.0);
38+
}
39+
};

0 commit comments

Comments
 (0)