首页 > 代码库 > UVA - 11427 Expect the Expected (DP+概率)
UVA - 11427 Expect the Expected (DP+概率)
Description
Problem A
Expect the Expected
Input: Standard Input
Output: Standard Output
Some mathematicalbackground. This problem asks you to compute the expected value of arandom variable. If you haven‘t seen those before, the simple definitions areas follows. Arandom variable is a variable that can have one of severalvalues, each with a certain probability. The probabilities of each possiblevalue are positive and add up to one. Theexpected value of a randomvariable is simply the sum of all its possible values, each multiplied by thecorresponding probability. (There are some more complicated, more generaldefinitions, but you won‘t need them now.) For example, the value of a fair,6-sided die is a random variable that has 6 possible values (from 1 to 6), eachwith a probability of
I like to play solitaire. Eachtime I play a game, I have probability p of solving it and probability
Here is my plan. Every day, Iwill play a game of solitaire. If I win, I‘ll go to sleep happy until the next day.If I lose, I‘ll keep playing until the fraction of games I have won todaybecomes larger thanp. At this point, I‘ll declare victory and go tosleep. As you can see, at the end of each day, I‘m guaranteed to always keep mystatistics above the expected
If your intuition is telling youthat something here must break, then you are right. I can‘t keep doing thisforever because there is a limit on the number of games I can play in one day.Let‘s say that I can play at mostn games in one day. How many days canI expect to be able to continue with my clever plan before it fails?Note that the answer is always at least 1 because it takes me a whole day ofplaying to reach a failure.
Input
The first line of input gives thenumber of cases, N. N test cases follow. Each one is a linecontaining p (as a fraction) andn.
1 ≤ N ≤ 3000, 0 ≤ p< 1,
The denominator of p will be at most 1000,
1 ≤ n ≤ 100.
Output
For each test case, print a lineof the form "Case #x: y", where y is theexpected number of days, rounded down to the nearest integer. The answer willalways be at most 1000 and will never be within 0.001 of a round-off errorcase.
Sample Input Output for Sample Input
4 1/2 1 1/2 2 0/1 10 1/2 3 | Case #1: 2 Case #2: 2 Case #3: 1 Case #4: 2 |
Problemsetter: Igor Naverniouk
Special thanks: Frank Chu
题意:每天晚上你都玩牌,如果赢一次了就去睡觉,如果输了继续玩,每盘赢的概率是p,你会一直玩到获胜局数大于p才会停,每晚只有n盘,问你计算出平均情况下,你会玩多少晚上
思路:首先设Q为只玩一晚就再也不玩了的概率,设d[i][j]表示前i局中获胜比例不超过p,且前i局胜了j局的概率,那么就有全概率公式,
当:j/i<=p的时d[i][j] = d[i-1][j] * (1-p) + d[i-1][j-1] * p,d[0][0] = 1, d[0][1] = 0,那么d[n][0]+d[n][1]+...就是所求的概率Q,
然后就是计算总天数X的数学期望,讨论当X=1,X=2....根据EX=Q + 2*Q(1-Q)+3*Q*(1-Q)^2...推出EX=1/Q
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 105; int main() { int t, cas = 1; scanf("%d", &t); while (t--) { int n, a, b; double d[maxn][maxn], p; scanf("%d/%d%d", &a, &b, &n); p = (double) a / b; memset(d, 0, sizeof(d)); d[0][0] = 1.0, d[0][1] = 0.0; for (int i = 1; i <= n; i++) for (int j = 0; j * b <= a * i; j++) { d[i][j] = d[i-1][j] * (1-p); if (j) d[i][j] += d[i-1][j-1] * p; } double Q = 0.0; for (int j = 0; j*b <= a*n; j++) Q += d[n][j]; printf("Case #%d: %d\n", cas++, (int)(1/Q)); } return 0; }
UVA - 11427 Expect the Expected (DP+概率)