题目链接

题意:

  击球训练中, 你击中一个球的概率为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] + 1)

  迭代f[x] 得到f[1]的表达式为:

                f[1] = p^(k - 2) * x + p^(k - 3) * x + ... + p ^ 0 * x。

                f[1] = x * ( (1 - p ^ (k - 1))/ (1 - p))

  一样的解法,求出g[1]的表达式,再将f[1]代进g[1] 的表达式, 解得g[1].

  再将g[1]反代入f[1]的表达式, 解得f[1]。

  最后答案为 ans = p * (f[1] + 1) + (1 - p) * (g[1] + 1)

代码:

 #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define eps 1e-6
#define MAXN 1000000
#define MAXM 100
#define dd cout<<"debug"<<endl
#define p(x) printf("%d\n", x)
#define pd(x) printf("%.7lf\n", x)
#define k(x) printf("Case %d: ", ++x)
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define mes(x, d) memset(x, d, sizeof(x))
#define do(i, x) for(i = 0; i < x; i ++)
#define dod(i, x, l) for(i = x; i >= l; i --)
#define doe(i, x) for(i = 1; i <= x; i ++)
double p;
int k1, k2;
double qpow(double x, int k)
{
double res = 1.0;
while(k)
{
if(k & ) res *= x;
x *= x;
k >>= ;
}
return res;
} int main()
{
int T;
int kcase = ;
scanf("%d", &T);
while(T --)
{
scanf("%lf %d %d", &p, &k1, &k2);
if(p == 0.000)
printf("Case %d: %d\n", ++ kcase, k1);
else if(p == 1.000)
printf("Case %d: %d\n", ++ kcase, k2);
else
{
double q = 1.0 - p;
double x1 = 1.0 - qpow(p, k2 - );
double x2 = 1.0 - qpow(q, k1 - );
double f = x1 * x2 / q + x2 / p;
f = f / ( - x1 * x2);
double g = q * f * x1 / q + x1 / q;
double ans = q * f + p * g + 1.0;
printf("Case %d: %.3lf\n", ++ kcase, ans);
}
}
return ;
}

LightOj_1408 Batting Practice的更多相关文章

  1. Batting Practice LightOJ - 1408

    Batting Practice LightOJ - 1408(概率dp) 题意:有无限个球,进球的概率为p,问你连续不进k1个球或者连续进k2个球需要使用的球的个数的期望 思路: \(定义f[i]表 ...

  2. lightoj 1408 Batting Practice (概率问题,求期望,推公式)

    题意:一个人若连续进k1个球或连续不进k2个球,游戏结束,给出这个人不进球的概率p(注意:是不进球!!!),求到游戏结束时这个投球个数的期望. 不进球概率为p,进概率 q=1-p.设 f[i] 表示连 ...

  3. lightoj 1408 Batting Practice

    题意:一个人若连续进k1个球或连续不进k2个球,游戏结束,给出这个人进球的概率p,求到游戏结束时这个投球个数的期望. 进球概率为p,不进概率 q=1-p 设 f[i] 表示连续 i 次不进距离连续k2 ...

  4. 越狱Season 1-Episode 15: By the Skin and the Teeth

    Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...

  5. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  6. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  7. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  8. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  9. Atitit 数据存储视图的最佳实际best practice attilax总结

    Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...

随机推荐

  1. 合并两个vectcor——2013-08-26

    vector<int> v1; vector<int> v2; for(int i=0; i<5; i++) { v1.push_back(i); if(i%2==1) ...

  2. 多媒体应用-swift

    照片选择主要是通过UIImagePickerController控制器实例化一个对象,然后通过self.PresentViewController方法推出界面显示.需要实现代理UIImagePicke ...

  3. AFNetworking源码分析

    来源:zongmumask 链接:http://www.jianshu.com/p/8eac5b1975de 简述 在iOS开发中,与直接使用苹果框架中提供的NSURLConnection或NSURL ...

  4. 衣联网络-亿能测试 安全测试沙龙 PPT资料免费下载

    衣联网络-亿能测试 安全测试沙龙 PPT资料免费下载http://automationqa.com/forum.php?mod=viewthread&tid=2304&fromuid= ...

  5. 利用java开发一个双击执行的小程序

    之前我们利用java写了很多东西,但是好像都没有什么实际意义. 因为有意义桌面小程序怎么都得有个界面,可是界面又不太好搞.或者 了解到这一层的人就少之又少了. 呀,是不是还得开辟一些版面来介绍awt和 ...

  6. c语言,strcmp(),字符串比较,看Asic 码,str1>str2,返回值 > 0;两串相等,返回

    #include<stdio.h> #include<string.h> int main() {  char *buffer1="aaa",*buffer ...

  7. (转)Excel导出及数据格式化处理

    原文地址:http://www.cnblogs.com/cnaspnet/archive/2008/05/24/1206263.html public void ToExcel(System.Web. ...

  8. linux下如何删除文件夹

    直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思删除文件夹实例: rm -rf /var/l ...

  9. web开发第一周

    第一天:HTML基础内容. 超文本标记语言,Hyper Text Makeup Language. 列表(清单),表格,框架,和表单,四个方法还不是很熟练. 列表,list,分OL和UL,表格的每个单 ...

  10. C++编程注意事项

    1.所有成员变量在构造函数中进行初始化操作,如指针赋值为空,bool赋值为FALSE(默认为TRUE); 2.构造函数与析构函数配对出现,执行反向操作,保证执行析构之后,没有遗留问题存在: 3.如果需 ...