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)pwill 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

然后根据期望的公式推啊,我要刚开始成功的概率已经是1了,那么期望值肯定是k2,因为我不可能失败,但是我成功的几率接近于0,那么我只要成功k1次就好了

#include <bits/stdc++.h>
using namespace std;
int main(){
int T,ca=1;
scanf("%d",&T);
while(T--){
    double p,q;
    int k1,k2;
    scanf("%lf%d%d",&p,&k1,&k2);
    q=1-p;
    if(p<1e-6)
        printf("Case %d: %d\n",ca++,k1);
    else if(q<1e-6)
    printf("Case %d: %d\n",ca++,k2);
    else{
    double a=1-pow(q,k1-1),b=1-pow(p,k2-1);
    double x=(a*b/q+a/p)/(1-a*b),y=b*x+b/q;
    printf("Case %d: %f\n",ca++,p*y+q*x+1);}
}
return 0;}
时间: 2024-10-12 11:08:10

LightOJ - 1408 Batting Practice的相关文章

LightOJ - 1408 Batting Practice 公式 + 概率

题目大意:有一个人在投篮,投不进的概率为p.现在他要玩一个游戏,如果连续投中m颗球或者连续投不进n颗球,那么就停止投篮 现在问,他停止投篮的期望是多少 解题思路:设g[i]为已经投中了i颗,还需要投进m-i颗球的期望,设f[i]为投丢了i颗,还需要再投丢n-i颗球的期望,那么可得公式 g[i] = (1-p) * (g[i+1] + 1) + p * (f[1] + 1),即 g[i] = (1-p) * g[i+1] + f[1] + 1 f[i]和g[i]的公式相似,这里就不写了 观察g的公

LightOj_1408 Batting Practice

题目链接 题意: 击球训练中, 你击中一个球的概率为p,连续击中k1个球, 或者连续击空k2个球, 则训练结束. 求结束训练所击球次数的期望. 思路: 设f[x]为连续击中x个球, 距离结束训练所需要的期望 设g[x]为连续击空x个球, 距离结束训练所需要的期望 f[x] = p * (f[x + 1] + 1) + (1 - p) * (g[1] + 1) g[x] = p * (f[1] + 1) + (1 - p) * (g[x + 1] + 1) 令 x = (1 - p) * (g[1

lightoj 1408 概率dp

https://blog.csdn.net/moon_sky1999/article/details/98097470 博主在此,牛逼神犇 1 #include<bits/stdc++.h> 2 using namespace std; 3 const double eps = 1e-9; 4 int main(){ 5 int T,k1,k2; 6 double p; 7 scanf("%d",&T); 8 for(int cas=1;cas<=T;cas+

LightOJ 1029 Civil and Evil Engineer最小生成树和最大生成树

F - Civil and Evil Engineer Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1029 Description A Civil Engineer is given a task to connect n houses with the main electric power station directly

lightoj 1074 spfa判断负环

Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1074 Appoint description:  System Crawler  (2016-05-03) Description Dhaka city is getting crowded and noisy day by day. Certain

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

LightOJ 1356 Prime Independence (素数 二分图)

Prime Independence Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1356 Description A set of integers is called prime independent if none of its member is a prime multiple of another member. A

LightOJ 1030 数学期望

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1030 uDebug Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amoun

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairs