LightOJ1408---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

Output for Sample Input

5

0.5 1 1

0.5 1 2

0.5 2 2

0.19 1 3

0.33 2 1

Case 1: 1

Case 2: 1.5

Case 3: 3

Case 4: 1.2261000000

Case 5: 1.67

設球進的概率p,不進爲q
我們設E(i,0) 表示現在連續進了i個球,還需要再扔的球的數目的期望
E(0,j) 表示連續不進j個球,還需要再扔的球的數目的期望

顯然:
E(i,0)=p(E(i+1,0)+1)+q(E(0,1)+1)
E(0,j)=q(E(0,j+1)+1)+p(E(1,0)+1)
E(k1,0)=0
E(0,k2)=0
最後可以得到

E(0,0)= (1pk1 )E(0,1)+k11i=0 pi
E(0,0)= (1qk2 )E(1,0)+k21i=0 qi

E(1,0)= (1pk11 )E(0,1)+k12i=0 pi
E(0,1)= (1qk21 )E(1,0)+k22i=0 qi

用下面2個方程,可以解出E(1,0)E(0,1) 的值
然後帶到第1個或者第2個方程就可以求解出 E(0,0)

/*************************************************************************
    > File Name: Q.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月26日 星期二 17時10分31秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double Pow(double a, int b) {
    double ans = 1.0;
    for (int i = 1; i <= b; ++i) {
        ans *= a;
    }
    return ans;
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        double p;
        int k1, k2;
        scanf("%lf%d%d", &p, &k1, &k2);
        printf("Case %d: ", icase++);
        if (p == 1.000) {
            printf("%d.00\n", k2);
        }
        else if (p == 0.000) {
            printf("%d.00\n", k1);
        }
        else {
            double q = p;
            p = 1 - p;
            double a = 1 - Pow(p, k1 - 1);
            double c = 1 - Pow(q, k2 - 1);
            double b = a / (1 - p);
            double d = c / (1 - q);
            double y = (c * b + d) / (1 - c * a);
            double ans = (1 - Pow(p, k1)) * y + (1 - Pow(p, k1)) / (1 - p);
            printf("%f\n", ans);
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章