Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 251  Solved: 136

Description

Mr. B and Mr. M like to play with balls. They have many balls colored in blue and red. Firstly, Mr. B randomly picks up N balls out of them and put them into a bag. Mr. M knows that there are N+1 possible situations in which the number of red balls is ranged from 0 to N, and we assume the possibilities of the N+1 situations are the same. But Mr. M does not know which situation occurs. Secondly, Mr. M picks up P balls out of the bag and examines them. There are Q red balls and P-Q blue balls. The question is: if he picks up one more ball out of the bag, what is the possibility that this ball is red?
袋子里有N个球,其中红球的个数可能为0~N,且每种情况概率相等。
现在从袋子里拿出P个球,发现其中Q个是红球,P-Q个是蓝球。
不放回球,如果再从袋子里拿出一个球,问拿出的是红球的概率是多少?

Input

Each test case contains only one line with three integers N, P and Q (2 <= N <= 100,000, 0 <= P <= N-1, 0 <= Q <= P).

Output

 
For each test case, display a single line containing the case number and the possibility of the next ball Mr. M picks out is red. The number should be rounded to four decimal places.

Sample Input

3 0 0
4 2 1

Sample Output

Case 1: 0.5000
Case 2: 0.5000

HINT

[Explanation]

For example as the sample test one, there are three balls in the bag. The possibilities of the four possible situations are all 0.25. If there are no red balls in the bag, the possibility of the next ball are red is 0. If there is one red ball in the bag, the possibility is 1/3. If there are two red balls, the possibility is 2/3. Finally if all balls are red, the possibility is 1. So the answer is 0*(1/4)+(1/3)*(1/4)+(2/3)*(1/4)+1*(1/4)=0.5.

Source

数学问题 概率

推出来答案就是(q+1)/(p+2)

可能是近一年写过最短的代码

————————至于怎么推出来的?

http://www.cnblogs.com/neighthorn/p/6440645.html 参考了这里

首先要了解一些公式:

$ P(B)=\sum P(Ai)*P(B|Ai) $ 全概率公式,A是前置条件,B发生的概率是A取遍所有情况下发生B的概率的和

$ P(A|B)=P(AB)/P(B) $ 条件概率公式,B条件下发生A的概率等于“同时发生AB的概率/发生B的概率”

然后看具体题目:

设事件A为:按照题意,再抽出一个球为红色球的概率

设事件B为:抽出P个球,其中有q个球为红色的概率

设事件Ki为:起初有Ki个红球的概率

$ ANS=P(A|B)/P(B)=\frac{P(AB)}{P(B)} $

变形为

$\frac{ \sum_{i=0}^{N} P(AB|Ki)P(Ki)}{ \sum_{i=0}^{N} P(B|Ki)P(Ki)}$

==

$\frac{ \sum_{i=0}^{N} P(A|BKi)*P(B|Ki)*P(Ki)}{ \sum_{i=0}^{N} P(B|Ki)P(Ki)}$

上下形式很像,可惜K不同,不满足分配率,不能全约分掉

红球的个数从0~N等概率,所以每种情况出现的概率都是$ P(Ki)= \frac{1}{n+1} $

所以上式的P(Ki)可以约掉了

剩下的部分怎么办?

$ P(B|Ki)=\frac{C_{k}^{q}*C_{p-q}^{n-k} }{C_{n}^{p}} $

↑ 从红球中选q个的方案乘从蓝球中拿p-q个的方案除以所有拿的方案,看上去没毛病

$ P(A|BKi)=\frac{P(ABKi)}{P(BKi)}=\frac{k-q}{n-p} $

剩下n-p个球中有k-q个红球,看上去没毛病。也可以用条件概率公式约一波

得到:

$ \frac{ \sum_{i=0}^{N} \frac{k-q}{n-p} *\frac{ C_{k}^{q}C_{n-k}^{p-q}}{ C_{n}^{p}} } { \sum_{i=0}^{N} \frac{ C_{k}^{q}C_{n-k}^{p-q}}{ C_{n}^{p}}} $

那个$(k-q)/(n-p)$好麻烦,得想个办法处理掉。

$ C_{k}^{q}=\frac{k!}{q!(k-q)!}=\frac{k!}{q!(k-q-1)!}*(k-q) $

$ C_{k}^{q+1}=\frac{k!}{(q+1)!(k-q-1)!}=\frac{k!}{q!(k-q-1)!}*(q+1) $

于是把式子变形成:

$  \frac{ \sum_{i=0}^{N} \frac{q+1}{n-p} *\frac{ C_{k}^{q}C_{n-k}^{p-q}}{ C_{n}^{p}} } { \sum_{i=0}^{N} \frac{ C_{k}^{q}C_{n-k}^{p-q}}{ C_{n}^{p}}} $

这样就消除了又一处k的影响

从网上找来一个公式:

于是得到

$\frac{C_{n+1}^{p+2}}{C_{n+1}^{p+1}} *\frac{q+1}{n-p}$

再一化简就是(q+1)/(p+2)

————————

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
int n,p,q;
int main(){
int cas=;
while(scanf("%d%d%d",&n,&p,&q)!=EOF){
printf("Case %d: %.4f\n",++cas,(q+1.0)/(p+2.0));
}
return ;
}

Bzoj3093 [Fdu校赛2012] A Famous Game的更多相关文章

  1. BZOJ 3093: [Fdu校赛2012] A Famous Game

    3093: [Fdu校赛2012] A Famous Game Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 242  Solved: 129[Subm ...

  2. 【BZOJ】3093: [Fdu校赛2012] A Famous Game

    http://www.lydsy.com/JudgeOnline/problem.php?id=3093 题意:n个球(红和蓝两种),等概率有1~n个红球.首先取出p个球且这p个球里边有q个红球,问从 ...

  3. 【BZOJ】【3093】【FDU校赛2012】A Famous Game

    概率论 神题不会捉啊……挖个坑先 orz 贾教 & QuarterGeek /********************************************************* ...

  4. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  5. SCNU省选校赛第二场B题题解

    今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...

  6. 2014上半年acm总结(1)(入门+校赛)

    大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干=  = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...

  7. 2017CUIT校赛-线上赛

    2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...

  8. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  9. 校赛F

    问题描述 例如对于数列[1 2 3 4 5 6],排序后变为[6 1 5 2 4 3].换句话说,对于一个有序递增的序列a1, a2, a3, ……, an,排序后为an, a1, an-1, a2, ...

随机推荐

  1. php中==和===的含义及区别

    ===比较两个变量的值和类型:==比较两个变量的值,不比较数据类型. 比如 $a = '123'; $b = 123; $a === $b为假: $a == $b为真: 有些情况下不能使用==,可以使 ...

  2. <Docker学习>3. docker镜像命令使用

    镜像提供容器运行时所需要的程序,资源.配置文件等,是一个特殊的文件系统.是容器运行的基础.镜像是多层文件系统组成的,是一个分层存储的架构,在镜像的构建中,会一层层的构建,每一层构建完成就不会发生改变, ...

  3. 20145202马超 《Java程序设计》第六周学习总结

    进程:是一个正在执行中的程序,每一个进程都有一个执行程序,该顺序是一个执行路径,或者说是一个控制单元. 线程:就是进程中的一个独立的控制单元,线程在控制着进程的执行. 一个进程至少有一线程. Java ...

  4. python re模块实现计算器

    def mul_div(exp): #计算乘除 while True: ret = re.search('[\d\.]+[\*\/]-?[\d\.]+', exp) if ret: atom_exp ...

  5. Dapper.Extension的基本使用

    前言    上一篇随笔写了Dapper的简单的使用,这次写一下Dapper.Extension的使用,它是Dapper的简单的封装扩展,可以通过实例化的对象赋值后进行增删改的操作以及分页,但是却不能进 ...

  6. elasticsearch索引和映射

    目录 1. elasticsearch如何实现搜索 1.1 搜索实例 1.2 es中数据的类型 1.3 倒排索引 1.4 分析与分析器 1.4.1 什么是分析器 1.4.2 内置分析器种类 1.4.3 ...

  7. flask 基础ssti注入

    源代码地址 (请用python2.7运行,python3有点出入) 注入点: 不是返回的静态模板而是反回模板字符串变得让客户端可以控制. XSS 这里直接 http://39.105.116.195: ...

  8. Python导出sql语句结果到Excel

    本文档是因为每周需要统计线上数据库中客户新增资源,手动执行实在是麻烦,就写了个脚本导出到Excel,顺便发一封邮件. (当然这不是线上的真实脚本,不过根据个人需求稍微修改下,还是可以直接用的.拿去不谢 ...

  9. FTP2

    FTP: 环境:windows, python 3.5功能:1.用户加密认证,可自行配置家目录磁盘大小2.多用户登陆3.查看当前目录(家目录权限下)4.切换目录(家目录权限下)5.上传下载,进度条展示 ...

  10. crmsh语法

    .查看配置信息 crm(live)# configure crm(live)configure# show node node1 node node2 property cib-bootstrap-o ...