题目链接

题意:

  击球训练中, 你击中一个球的概率为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. MySQL【第三篇】数据类型

    一.整型 整型的每一种都有无符号(unsigned)和有符号(signed)两种类型. MySQL数据类型 含义 tinyint(m) 1个字节表示:signed(-128~127):unsigned ...

  2. 读写应用程序数据-CoreData

    coreData数据最终的存储类型可以是:SQLite数据库.XML.二进制.内存里.自定义的数据类型. 和SQLite区别:只能取出整个实体记录,然后分解,之后才能得到实体的某个属性. 1.创建工程 ...

  3. 网格GridLayout建立

    import java.awt.*;public class GridFlowout extends Frame { public GridFlowout (String str){ {setTitl ...

  4. Java基础知识强化之IO流笔记58:内存操作流

    1. 内存操作流: 用来操作处理临时存储的信息的. (1)操作字节数组: ByteArrayInputStream ByteArrayOutputStream 代码示例: package cn.itc ...

  5. Objective-C:runtime

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  6. NC portal怎么重新开始入门,整个配置过程包括配置一个节点

    有一份文档,之后会上传,暂时不上传

  7. Solr4.8.1与Tomcat7整合

    Solr4.8.1和Tomcat7都可以到官方网站去下载,我这里就不多说了,如下图. 这里我们首先解压Solr-4.8.1.zip,再解压Tomcat,解压后,再在当前文件夹下建2个文件夹,一个用来放 ...

  8. OC 消息传递机制

    消息传递(message passing)的概念是Smalltalk语言的核心原则之一,有时Smalltalk和Objective-C被称为面向消息的语言,通常“消息”一词的含义和“方法”是相同的. ...

  9. JavaScript学习笔记 -- ES6学习(二) let 和const

    ES6 中新增了两个命令: let 和const. let命令: let 用于声明变量,和var 类似,但是所声明的变量只在代码块中有效,不存在变量提升,有暂时性死区. 1.只在代码块中有效 和var ...

  10. 04_过滤器Filter_01_入门简述

    [简述] Filter也称之为过滤器.通过Filter技术,对web服务器管理的所有资源(如:Jsp.Servlet.静态图片文件.静态HTML文件等)进行拦截,从而实现一些特殊的功能.例如实现URL ...