Zoj3582: Back to the past (probability DP)

Source: Internet
Author: User

Recently poet mr. po encountered a serious problem, rumor said some of his early poems are written by others. this brought a lot of trouble to mr. po, so one day he went to his best friend mastero for help. mastero handed over a small wooden box
With a silent smile. mr. po checked the box, a word "ygbh" carved on the side. "The box can take you back to the past," mastero said, "so you can get any evidence you need. but, before that, you need some patience."

There areNTiny dark holes on both sides of the box (2NHoles in total). Every day, for each hole, there is a possibilityPTo successfully absorb
The power of moon and then magically sparkle. the possibilities among holes are independent in each day. once a hole sparkles, it will never turn dark again. the box only works when there are no lessMSparkling Holes
On each side of the box. The question is that what is the expected number of days before the box is available.

Input

The input consists of several test cases. For each case there are 3 numbers in a line:N,M,P. 1 ≤N≤ 50, 1 ≤M≤N,
0.01 ≤P≤ 1. A case with three zeros indicates the end of the input.

Output

For each test case, output the expected number of days, accurate up to six decimal places.

Sample Input
2 1 11 1 0.50 0 0
Sample output
1.0000002.666667
 
Meaning: n, m, p. There are n lamps on both sides of the box, which require at least m lamps to be lit on both sides. The brightness and darkness of each lamp are independent, the probability of a bright light is P, and the bright light will not go out again.
Idea: Another question is the expectation of probability DP. If DP [I] [J] is set, it indicates the expectation of reaching the target from I on the left and J on the right.
Then DP [I] [J] = 1 + Sigma {C (n-I, x) * P ^ x * (1-p) ^ (n-I-x) * C (n-J, Y) * P ^ y * (1-p) ^ (n-J-y) * DP [I + x] [J + Y]};
Note that C (n-I, x) Here is a combination number. These combinations can be table processed by Yang Hui's triangle.
# Include <stdio. h> # include <string. h> # include <math. h ># include <algorithm> using namespace STD; double DP [55] [55], C [55] [55]; double Yes [55], no [55]; // DP [I] [J] indicates the number of days void set ()/Yang Hui triangle table required to highlight one hole and one hole to reach the target, that is, the coefficients {int I, j; for (I = 0; I <= 55; I ++) {c [I] [0] = C [I] [I] = 1; for (j = 1; j <I; j ++) c [I] [J] = C [I-1] [J-1] + C [I-1] [J];} int main () {int n, m, I, J, x, Y; Double P, sum; Set (); While (~ Scanf ("% d % lf", & N, & M, & P) {If (n = 0 & M = 0) break; yes [0] = No [0] = 1; for (I = 1; I <= N; I ++) {Yes [I] = Yes [I-1] * P; no [I] = No [I-1] * (1.0-p);} memset (DP, 0, sizeof (DP); for (I = N; I> = 0; I --) {for (j = N; j> = 0; j --) {// I, j, indicates the number of bright lines left and right. If (I> = M & J> = m) continue; sum = 0; For (x = 0; x + I <= N; X ++) {for (y = 0; y + j <= N; y ++) {If (x = 0 & Y = 0) continue; if (n-I> = x & N-j> = y) sum + = DP [x + I] [Y + J] * C [n-I] [x] * Yes [x] * No [n-I-x] * C [n-J] [Y] * Yes [y] * No [n-J-y]; // X, Y is the number of new bright lines on the left and right. // C [n-I] [X], X is selected as the new light in the remaining N-I, that is, a combination number} DP [I] [J] = (sum + 1.0) /(1.0-No [n-I] * No [n-J]); // calculate the expectation. + 1 is over a day, divided by (1.0-No [n-I] * No [n-J]), the printf ("%. 6f \ n ", DP [0] [0]);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.