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 de nitions 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 de nitions, 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
因为每天都是独立的,可以先算出来某一天失败的概率。
用
dp[i][j]
表示玩了
i
次,赢了
求出某一天失败的概率
q
,解方程
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double dp[110][110],p;
int a,b,n;
int main()
{
int T,i,j,K;
double ans;
scanf("%d",&T);
for (K=1;K<=T;K++)
{
scanf("%d/%d%d",&a,&b,&n);
p=(double)a/b;
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for (i=1;i<=n;i++)
for (j=0;j*b<=a*i;j++)
dp[i][j]=p*dp[i-1][j-1]+(1-p)*dp[i-1][j];
ans=0;
for (j=0;j*b<=a*n;j++)
ans+=dp[n][j];
printf("Case #%d: %d\n",K,(int)(1/ans));
}
}