首页 > 代码库 > ZOJ3582---Back to the Past
ZOJ3582---Back to the Past
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 are N tiny dark holes on both sides of the box (2N holes in total). Every day, for each hole, there is a possibilityP to 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 less thanM sparkling 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 1 1 1 0.5 0 0 0
Sample Output
1.000000 2.666667
Author: ZHENG, Jianqiang
Contest: ZOJ 10th Anniversary Contest
概率dp,设dp[u][v] 表示 第一面有u个亮的黑洞,第二面有v个亮的黑洞时,达到目标状态的期望值
dp[u][v] = (dp[u + i][v + j] + 1) * C(i, n - u) * (p^i) * ((1-p) ^(n - u - i)) * C(j, n - v) * (p ^ j) & ((1 - p) ^ (n - v - j)) (0 <= i + u <= n && 0 <= j + v <= n )
然后稍微变形下,把i == 0 && j == 0 的提出来,这样就可以递推了,或者记忆化搜索也可以,至于组合数,可以先预处理出来
/************************************************************************* > File Name: zoj3582.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2014年12月25日 星期四 20时24分54秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; double dp[55][55]; long long C[55][55]; double p, px[55], py[55]; int n, m; double dfs(int u, int v) { if (u >= m && v >= m) { return dp[u][v] = 0; } if (dp[u][v] != -1) { return dp[u][v]; } double ans = 0; double p1 = 0, p2 = 0; for (int i = 0; i + u <= n; ++i) { p1 = C[n - u][i] * px[i] * py[n - u - i]; // printf("C[%d][%d] = %lld\n", n - u, i, C[n - u][i]); for (int j = 0; j + v <= n; ++j) { if (i == 0 && j == 0) { continue; } p2 = C[n - v][j] * px[j] * py[n - v - j]; ans += (dfs(u + i, v + j) + 1) * p1 * p2; } } double tmp = py[n - u] * py[n - v]; ans += tmp; ans /= (1 - tmp); return dp[u][v] = ans; } int main() { for (int i = 0; i <= 50; ++i) { C[i][0] = 1; } C[1][1] = 1; for (int i = 1; i <= 50; ++i) { for (int j = 1; j <= i; ++j) { if (i == 1 && j == 1) { continue; } if (i == j) { C[i][j] = 1; continue; } C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } while (~scanf("%d%d%lf", &n, &m, &p)) { if (n == 0 && m == 0 && p == 0) { break; } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { dp[i][j] = -1.0; } } px[0] = py[0] = 1; for (int i = 1; i <= n; ++i) { px[i] = px[i - 1] * p; py[i] = py[i - 1] * (1 - p); // printf("%f %f\n", px[i], py[i]); } printf("%.6f\n", dfs(0, 0)); } return 0; }
ZOJ3582---Back to the Past