标签:
11427 Expect the Expected
Some mathematical background. This problem asks you to compute the expected value of a random
variable. If you haven’t seen those before, the simple definitions are as follows. A random variable is a
variable that can have one of several values, each with a certain probability. The probabilities of each
possible value are positive and add up to one. The expected value of a random variable is simply the
sum of all its possible values, each multiplied by the corresponding probability. (There are some more
complicated, more general definitions, 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), each with a probability of 1/6.
Its expected value is 1/6 + 2/6 + : : : + 6/6 = 3:5. Now the problem.
I like to play solitaire. Each time I play a game, I have probability p of solving it and probability
(1 ?? p) of failing. The game keeps statistics of all my games – what percentage of games I have won.
If I simply keep playing for a long time, this percentage will always hover somewhere around p 100%.
But I want more.
Here is my plan. Every day, I will 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 today becomes larger
than p. At this point, I’ll declare victory and go to sleep. As you can see, at the end of each day, I’m
guaranteed to always keep my statistics above the expected p 100%. I will have beaten mathematics!
If your intuition is telling you that something here must break, then you are right. I can’t keep
doing this forever because there is a limit on the number of games I can play in one day. Let’s say that
I can play at most n games in one day. How many days can I 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 of
playing to reach a failure.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one is a line containing
p (as a fraction) and n.
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 line of the form ‘Case #x: y’, where y is the expected number of days,
rounded down to the nearest integer. The answer will always be at most 1000 and will never be within
0.001 of a round-off error case.
Sample Input
4
1/2 1
1/2 2
0/1 10
1/2 3
Universidad de Valladolid OJ: 11427 – Expect the Expected 2/2
Sample Output
Case #1: 2
Case #2: 2
Case #3: 1
Case #4: 2
题意:
每天晚上都玩纸牌,如果第一次赢了就去sleep,不然就继续玩。每次游戏独立,赢的概率都是p。你会一直玩到当晚获胜局数占总局数比例严格大于p时才停止。但是一晚最多能玩n盘游戏,若获胜局数的比例始终不超过p的话就只能“垂头丧气地去睡觉”,并且之后晚上再也不玩纸牌了。计算平均情况下你会玩多少晚上的纸牌。
分析:
由于每个晚上的情况相对独立,所以研究单独一天晚上的情况。设只玩一天晚上纸牌时,“垂头丧气去睡觉”的概率是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(i,j) = 0。
边界是d(0,0) = 1、d(0,1) = 0。
则Q = d(n,0)+d(n,2)+d(n,3)+…
假设游戏天数是X,将X看作随机变量:
P(X = 1) = Q
P(X = 2) = (1 - Q) * Q
…
P(X = k) = (1 - Q) ^ (k - 1) * Q
则EX = Q + 2(1 - Q) * Q + 3(1 - Q)^2 * Q + …
令s = EX / Q,(1 - Q)s = (1 - Q) + 2(1 - Q)^2 + …
于是EX = 1 / Q。
其实,我们还可以根据全期望公式解除期望值。设所求的数学期望是e。把情况分成两类:第一天晚上就“垂头丧气去睡觉”,概率为Q,期望为1;第一天没有“垂头丧气去睡觉”,概率为1-Q,期望为e+1。所以e = Q * 1 + (1 - Q) * (e + 1)。
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 const int maxn = 100; 5 int main(){ 6 int T; scanf("%d",&T); 7 for(int kase = 1 ; kase <= T ; kase++){ 8 int n,a,b; scanf("%d/%d%d",&a,&b,&n); 9 double d[maxn + 1][maxn + 1],p; 10 p = (double)a / b; 11 memset(d,0,sizeof d); 12 d[0][0] = 1.0,d[0][1] = 0.0; 13 for(int i = 1 ; i <= n ; i++) 14 for(int j = 0 ; j * b <= a * i ; j++){ 15 d[i][j] = d[i - 1][j] * (1 - p); 16 if(j) d[i][j] += d[i - 1][j - 1] * p; 17 } 18 double Q = 0.0; // 垂头丧气去睡觉的概率 19 for(int j = 0 ; j * b <= a * n ; j++) Q += d[n][j]; 20 printf("Case #%d: %d\n",kase,(int)(1 / Q)); 21 } 22 return 0; 23 }
标签:
原文地址:http://www.cnblogs.com/cyb123456/p/5816377.html