题目链接

题意:

  击球训练中, 你击中一个球的概率为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. android114 c转换成c++

    ##C向C++改造 . 把c文件后缀名换成cpp . Android.mk文件中的hello.c也要换成hello.cpp . c++的使用的环境变量结构体中,访问了c使用的结构体的函数指针,函数名全 ...

  2. 【邮件】imap与pop3的区别

    文:铁乐猫 2015 10月14日 今天替一位在外出差的用户安装和设置完foxmail用于收发邮件,到下午被告知对方用foxmail发完邮件后,在网页上登录邮箱后并没有看到在foxmail中" ...

  3. IT牛人博客

    IT牛人博客,参见:http://blog.csdn.net/freebird_lb/article/details/8210276 团队技术博客 淘宝UED淘宝用户体验团队 淘宝核心系统淘宝核心系统 ...

  4. what is delta simulation time

    In digital logic simulation, a delta cycles are evaluation of expressions, followed by value updates ...

  5. Unity3D 之UGUI 切换开关(toggle)

    (toggle)开关能够实现通过点击实现开和关. 开关下面有两个游戏元素,一个是背景,一个是文字,背景是开关按钮的背景. 可以通过 Is ON开控制开关的开与关. Group -->分组,能够实 ...

  6. Linq常用查询运算符

    Linq一共包含五十几个查询运算符,常用的根据类型来区分一共有5类左右,这五类里面一些事在项目查询中经常用到的.不过linq运算符的命名十分规范,基本从字面意思就能猜测出来是干嘛用的,下面我们挑选一些 ...

  7. Asp.net 上传文件小叙(修改FileUpload显示文字等)

    想要在asp.net网站上上传文件就得用到FileUpload,可是这个控件中“浏览”没法修改,可以使用html中<input type="file" 来解决该问题. 首先页 ...

  8. PHP mysql_real_escape_string() 函数防SQL注入

    PHP MySQL 函数 定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' &quo ...

  9. ios Objective-C的动态特性

    这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语 ...

  10. iOS 添加占位符

    添加占位符: 首先占位符的大小要比textView 的大小要小一些 1.添加一个取消键盘的通知 2.添加一个代理事件 1. // removeKeyBoard 添加通知收回键盘 [[NSNotific ...