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)= (1?pk1 )?E(0,1)+∑k1?1i=0 pi
E(0,0)= (1?qk2 )?E(1,0)+∑k2?1i=0 qi
E(1,0)= (1?pk1?1 )?E(0,1)+∑k1?2i=0 pi
E(0,1)= (1?qk2?1 )?E(1,0)+∑k2?2i=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;
}