uva11427 Expect the Expected

175 篇文章 0 订阅
137 篇文章 0 订阅

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 次,赢了j次,不曾成功的概率。

dp[i][j]={0pdp[i1][j1]+(1p)dp[i1][j](j/i>p)(j/ip)

求出某一天失败的概率 q ,解方程x=qx+(1q)0+1得期望天数为 1/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));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值