Question link: http://www.topcoder.com/stat? C = problem_statement & PM = 10335.
General meaning of the question:
Take the number of m in the middle of the interval [lower, upper] so that the number of K smaller values in the number of M is the probability of N.
Solution 1: (powerful DP) written by referring to others' problem-solving reports
DP [m] [s] [B]
Take the number of M, the maximum number of s smaller than N, the probability of a maximum of B larger than N (note the maximum number of 2 words)
DP [0] [s] [B] = 1
This Initialization is very important.
DP [m] [s] [B] = Pe * DP [M-1] [s] [B] +
PS * DP [M-1] [s-1] [B] + Pb * DP [s-1] [s] [b-1] (of course, ensure that s-1 and B-1 are greater than 0 before conversion)
Totel = upper-lower + 1;
PE is the probability that PE is equal to n = 1/totel;
PS is a probability smaller than N. PS = (n-lower)/totel;
PB is the probability Pb = (upper-N)/totel;
# Include <iostream> <br/> using namespace STD; <br/> class kthprobableelement <br/>{< br/> public: <br/> double DP [101] [101] [101]; <br/> double probability (int m, int lowerbound, int upperbound, int N, int K) <br/> {<br/> double Pe = 1.0/(upperbound-lowerbound + 1); <br/> double PS = (double) (N-lowerbound) /(upperbound-lowerbound + 1); <br/> double Pb = (double) (upperbound-N)/(upperbound-lowerbound + 1 ); <br/> for (INT m = 0; m <= m; m ++) <br/> {<br/> for (int s = 0; S <= K-1; s ++) <br/> {<br/> for (INT B = 0; B <= M-K; B ++) <br/> {<br/> If (M = 0) <br/> DP [m] [s] [B] = 1; <br/> else <br/> {<br/> DP [m] [s] [B] = Pe * DP M-1] [s] [B]; <br/> If (S> 0) <br/> DP [m] [s] [B] + = Ps * DP [s-1] [s-1] [B]; <br/> If (B> 0) <br/> DP [m] [s] [B] + = Pb * DP [s-1] [s] [b-1]; <br/>}< br/> return DP [m] [K-1] [M-K]; <br/>}< br/>}; <br/>
Solution 2: Probability Analysis Statistics
The number of K is N. Suppose there is a number smaller than N, and there is B number larger than N, then M-A-B is equal to n.
Just ensure a <= K-1
B <= m-K, then the number of K is n
Then the probability of A and B for each decision is:
C (m, a) * pA ^ A * C (m-a, B) * pb ^ B * PAB ^ (m-a-B) Pa, Pb, pab is similar to the above ps, PB
Finally, enumerate a and B that meet the conditions to accumulate the probability.