题目链接

题意:

  击球训练中, 你击中一个球的概率为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. cocos2d-x之CCMotionStreak类——2013-08-25 16

      在游戏的实现过程中,有时会需要在某个游戏对象上的运动轨迹上实现渐隐效果.这种感觉就好像是类似飞机拉线的拖尾巴,在视觉上感觉很好,比如子弹的运动轨迹等,如果不借助引擎的帮助,这种效果往往需要通过大量 ...

  2. locale 详解

    locale 是国际化与本土化过程中的一个非常重要的概念,个人认为,对于中文用户来说,通常会涉及到的国际化或者本土化,大致包含三个方面: 看中文,写中文,与 window中文系统的兼容和通信.从实际经 ...

  3. TCP/IP协议原理与应用笔记15:网络连接设备

    1. 网络连接设备: (1)转发器 Repeater/ 集线器 Hub (2)网桥 Bridge / 交换机 Switch (3)路由器 Router (4)网关 Gateway 2. 从通信角度看待 ...

  4. web 电子商务网站开发笔记整理

    js只保留整数,向上取整,四舍五入,向下取整等函数 来源:ab蓝学网整理 时间:2014-07-09 点击:30131 简介:WEB前端|1.丢弃小数部分,保留整数部分parseInt(5/2)2.向 ...

  5. Sun开发的JINI技术在网络中的应用

    一.概述 Sun公司开发的Jini系统是一种创新技术,听说它的人很多但较少被人理解.Jini系统可利用Java扩展网络计算的范围,并有可能成为最佳解决方案. Jini是Sun公司的研究与开发项目,它能 ...

  6. 在masterpage中添加对usercontrol的引用

    在masterpage中添加对usercontrol的引用的方式: <%@ Register Src="/_controltemplates/15/Excellent Employee ...

  7. TCP调试助手

    网络开发经常要用到一些TCP&UDP的调试工具,搜集一些备用. 目前总结工具有(不分先后): chrome等自带调试器调试HTTP Fiddler(.NET)和Charles debugger ...

  8. Angularjs2——TypeScript学习网站

    https://zhongsp.gitbooks.io/typescript-handbook/content/index.html

  9. linux 学习一

    linux 命令 ls -al ls -l cal

  10. 07_MyBatis原始的Dao编写方法

    [UserDao.java ] package com.Higgin.Mybatis.dao; import com.Higgin.Mybatis.po.User; public interface ...