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)= (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;
}
时间: 2024-12-09 17:56:10

LightOJ1408---Batting Practice (期望,推公式)的相关文章

HDU dice DP求期望+推公式

题意: 一个m边形的骰子,求连续投出n个相同的面,和m个两两不同的面的期望次数. solution: 令\(f_i\)表示已经连续投出i个相同的面,到连续投出n个还需要的期望次数. 令\(g_i\)类似的表示第二种问题的期望次数. 对于\(f_i\) ,有两种情况: ① 投出了和前i个相同的面,转移到了\(f_{i+1}\) ,那么\(f_i+=(f_{i+1}+1)*\frac{1}{m}\) ② 投出了一个不同的面,转移到了\(f_1\),那么\(f_i+=(f_1+1)*\frac{m-1

HDU 4870 Rating(概率、期望、推公式) &amp;&amp; ZOJ 3415 Zhou Yu

其实zoj 3415不是应该叫Yu Zhou吗...碰到ZOJ 3415之后用了第二个参考网址的方法去求通项,然后这次碰到4870不会搞.参考了chanme的,然后重新把周瑜跟排名都反复推导(不是推倒)四五次才上来写这份有抄袭嫌疑的题解... 这2题很类似,多校的rating相当于强化版,不过原理都一样.好像是可以用高斯消元做,但我不会.默默推公式了. 公式推导参考http://www.cnblogs.com/chanme/p/3861766.html#2993306 http://www.cn

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

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

sgu495:概率dp / 推公式

概率题..可以dp也可以推公式 抽象出来的题目大意: 有 n个小球,有放回的取m次  问 被取出来过的小球的个数的期望 dp维护两个状态 第 i 次取出的是 没有被取出来过的小球的 概率dp[i] 和取出的是已经被取出来过的小球的概率np[i]; 如果第 i-1 次取出的是已经被取出来过的小球 那么第 i 次取出没有取出来过小球的概率即为 dp[i-1]: 反之则为 dp[i-1] - 1/n(没有取出来过的小球少了一个) 所以可以得到状态转移方程 dp[i]=dp[i-1]*(dp[i-1]-

hdu_5810_Balls and Boxes(打表推公式)

题目链接:hdu_5810_Balls and Boxes 题意: 如题,让你求那个公式的期望 题解: 打表找规律,然后推公式.这项技能必须得学会 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 long long n,m,g; 7 while(~scanf("%I64d%I64d",&n,&m)&&n+m) 8

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno

bjfu1211 推公式,筛素数

题目是求fun(n)的值 fun(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])C[n][k] means the number of way to choose k things from n things. n最大一百万,马上反映到可能是递推打表. 首先肯定是推公式了,fun(n)其实就是Gcd(n)的一个前n项和,没意义,直接看Gcd(n),把前几项列出来,发现公式是Gcd(n) =

ASC(22)H(大数+推公式)

High Speed Trains Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by high