標籤:des style blog http os io strong ar for
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 of1/6. Its expected value is 1/6+ 2/6 + ... +6/6 = 3.5. Now the problem.
I like to play solitaire. Eachtime 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 ofgames I have won. If I simply keep playing for a long time, this percentagewill always hover somewhere aroundp*100%. But I want more.
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 expectedp*100%. I will have beatenmathematics!
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+機率)