题目链接

题意:

  击球训练中, 你击中一个球的概率为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. RHCA442学习笔记-Unit13网络性能调整

    UNIT 13 Essential Network Tuning 网络性能调整        目标:1. 应用队列技术最大化网络吞吐量            2. 调整TCP和non-TCP网络soc ...

  2. oc学习笔记2

    .oc中的BOOL类型 oc中的BOOL类型的值为YES和NO,有点小奇怪 在oc中YES不等于1,但是NO一定等于0,所以不要把1和YES来比较 .消息发送 在oc中,经常使用这样的术语:将allo ...

  3. JNI 学习笔记系列(一)

    JNI全称是Java native interface,它是一个中间件,通过JNI可以使Java和C语言之间互相调用,在android开发中,像wifi热点的开启,像极品飞车中重力加速,碰撞效果的模拟 ...

  4. iOS之应用程序国际化

    一.程序国际化 准备工作: 1.首先我们要先创建一个工程,我们给他命名Internationalization-Demo,然后添加语言. 从代码中分离文本: 目前,应用展示的所有文本都是以硬编码字符串 ...

  5. iOS 并行编程:Thread

    1 创建线程 1.1 NSThread       使用 NSThread 来创建线程有两个可以使用的方法: 1) 使用 detachNewThreadSelector:toTarget:withOb ...

  6. [置顶] gridview中嵌套gridview(并实现子gridview的数据绑定),页面传值,加密,数据绑定

    先来张效果图 gridview 中嵌套gridview的原理是这样的,在父gridview中建一个摸板列,然后再模版列当中在放入子gridview,然后再父gridview的OnRowDataBoun ...

  7. Import user's Environment path into Linux cron task

    How to use "cron" to create scheduled task Minimum time cycle: 1 minute Use crontab -e edi ...

  8. mysql 备份还原数据库

    备份和还原都在bin目录下操作 1.备份 mysqldump -u 用户名 -p 密码  --default-character-set=utf8  数据库名称 >d:/temp.sql 2.还 ...

  9. VS2012 直接浏览网页时报错

    VS2012 直接浏览网页时报错  "托管管道模式不能为集成" 只要在configuration文件里面添加   <system.webServer>     < ...

  10. 安卓百度地图开发so文件引用失败问题研究

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 问题 首先,下面的问题基本都是在Android Studio下使用不当导致,eclipse是百 ...