Batting Practice LightOJ - 1408

After being all out for 58 and 78 in two matches in the most prestigious tournament in the world, the coach of a certain national cricket team was very upset. He decided to make the batsmen practice a lot. But he was wondering how to make them practice, because the possibility of getting out seems completely random for them. So, he decided to keep them in practice as long as he can and told them to practice in the net until a batsman remains not-out for k1 consecutive balls. But if the batsman continues to be out for consecutive k2 balls, then the coach becomes hopeless about the batsman and throws him out of the team. In both cases, the practice session ends for the batsman. Now the coach is wondering how many balls the practice session is expected to take.

For a batsman the probability of being out in a ball is independent and is equal to p. What is the expected number of balls he must face to remain not out for k1 consecutive balls or become out in consecutive k2 balls.

Input

Input starts with an integer T (≤ 15000), denoting the number of test cases.

Each case starts with a line containing a real number p (0 ≤ p ≤ 1) and two positive integers k1 and k2 (k1 + k2 ≤ 50). p will contain up to three digits after the decimal point.

Output

For each case, print the case number and the expected number of balls the batsman will face. Errors less than 10-2 will be ignored.

Sample Input

5

0.5 1 1

0.5 1 2

0.5 2 2

0.19 1 3

0.33 2 1

Sample Output

Case 1: 1

Case 2: 1.5

Case 3: 3

Case 4: 1.2261000000

Case 5: 1.67

每次投球投偏的概率为p,如果连续投中k1次或者连续投偏k2次则投球结束,问投球个数的期望值
 

题解:数学期望推导公式
设f(x)为连续投偏x次后还要投球个数的期望  ,        g(x)为连续投中x次后还要投球个数的期望
f(k1)=g(k2)=0
先考虑f(x)

b=g(1)*p+1,f(k_1)=g(k_2)=0

f(x)=f(x+1)*(1-p)+g(1)*p+1

可以递推得到f(1)=(1-p)^{k_1-1}*f(k_1)+b*[1+...+(1-p)^{k_1-2}]

化简得到f(1)=\frac{[g(1)*p+1]*[1-(1-p)^{k_1-1}]}{p}
同理,g(1)=\frac{[f(1)*(1-p)+1]*[1-p^{k_2-1}]}{1-p}
最后解方程得到f(1)和g(1),答案就是ans=f(1)*(1-p)+g(1)*p+1

 

 

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
int main(){
    int T,k1,k2;
    double p;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%lf",&p);
        scanf("%d%d",&k1,&k2);
        printf("Case %d: ",cas);
        if(p<eps) printf("%d\n",k1);
        else if(1-p<eps) printf("%d\n",k2);
        else{
            double x1=1-pow(1-p,k1-1);
            double x2=1-pow(p,k2-1);
            double y1=x1/p;
            double y2=x2/(1-p);
            double a=(x1*y2+y1)/(1-x1*x2);
            double b=(y1*x2+y2)/(1-x1*x2);
            printf("%.7f\n",(1-p)*a+p*b+1);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值