LightOJ 1408 Batting Practice(数学期望)

Batting Practice

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

题意
连续k1次未命中球或者连续k2次命中球的期望

题解
f ( x ) f(x) f(x)表示连续x次未命中后还要击球的数量, g ( x ) g(x) g(x)表示连续x次命中球后还要击球的数量
所以 f ( k 1 ) = g ( k 2 ) = 0 f(k_1)=g(k_2)=0 f(k1)=g(k2)=0
f ( x ) = f ( x + 1 ) ⋅ ( 1 − p ) + g ( 1 ) ⋅ p + 1 \begin{aligned}f(x)&=f(x+1)·(1-p)+g(1)·p+1\\ \end{aligned} f(x)=f(x+1)(1p)+g(1)p+1
高中知识我竟然想了好久。。。废了废了
我们倒过来表示令 f ( x ) = a n , k = g ( 1 ) ⋅ p + 1 f(x)=a_n,k=g(1)·p+1 f(x)=an,k=g(1)p+1,即 a 1 = f ( k 1 ) = 0 a_1=f(k_1)=0 a1=f(k1)=0我们要求的为 a k 1 a_{k_1} ak1
由上式得
a n = a n − 1 ⋅ ( 1 − p ) + k a n − k p = ( 1 − p ) ( a n − 1 − k p ) a n − k p = ( 1 − p ) k 1 − 1 ( a 1 − k p ) a n = ( 1 − p ) k 1 − 1 ( a 1 − k p ) + k p \begin{aligned}a_n&=a_{n-1}·(1-p)+k\\ a_n-\cfrac{k}{p}&=(1-p)(a_{n-1}-\cfrac{k}{p})\\ a_n-\cfrac{k}{p}&=(1-p)^{k_1-1}(a_1-\cfrac{k}{p})\\ a_n&=(1-p)^{k_1-1}(a_1-\cfrac{k}{p})+\cfrac{k}{p} \end{aligned} ananpkanpkan=an1(1p)+k=(1p)(an1pk)=(1p)k11(a1pk)=(1p)k11(a1pk)+pk
带入值得 f ( 1 ) = ( g ( 1 ) ⋅ p + 1 ) ( 1 − ( 1 − p ) k 1 − 1 ) p f(1)=\cfrac{(g(1)·p+1)(1-(1-p)^{k_1-1})}{p} f(1)=p(g(1)p+1)(1(1p)k11)
因为 g ( x ) = g ( x + 1 ) ⋅ p + f ( 1 ) ⋅ ( 1 − p ) + 1 g(x)=g(x+1)·p+f(1)·(1-p)+1 g(x)=g(x+1)p+f(1)(1p)+1
同理得: g ( 1 ) = ( f ( 1 ) ⋅ ( 1 − p ) + 1 ) ( 1 − p k 2 − 1 ) 1 − p g(1)=\cfrac{(f(1)·(1-p)+1)(1-p^{k_2-1})}{1-p} g(1)=1p(f(1)(1p)+1)(1pk21)
解方程组。
最后结果 a n s = f ( 1 ) ⋅ ( 1 − p ) + g ( 1 ) ⋅ p + 1 ans=f(1)·(1-p)+g(1)·p+1 ans=f(1)(1p)+g(1)p+1

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-09;
const double PI = acos(-1.0);

int main(){
	int t,ca=1;cin>>t;
    while(t--){
        double p;
        int k1,k2;
        cin>>p>>k1>>k2;
        double q=1-p;
        printf("Case %d: ",ca++);
        if(p < eps) cout<<k1<<endl;
        else if(q < eps) cout<<k2<<endl;
        else{
            double a1=1-pow(q,k1-1),b1=a1/p;
            double a2=1-pow(p,k2-1),b2=a2/q;
            double f1=(a1*b2+b1)/(1-a1*a2);
            double g1=(b1*a2+b2)/(1-a1*a2);
            printf("%.7lf\n",(1-p)*f1+p*g1+1);
        }
    }
	return 0;
}

/*
 
*/
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值