HDU 4465 Candy ( 数学期望 )
2015-10-03 19:44
489 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465
Problem Description
LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability
(1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?
Input
There are several test cases.
For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 105) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.
Any answer with an absolute error less than or equal to 10-4 would be accepted.
Sample Input
10 0.400000
100 0.500000
124 0.432650
325 0.325100
532 0.487520
2276 0.720000
Sample Output
Case 1: 3.528175
Case 2: 10.326044
Case 3: 28.861945
Case 4: 167.965476
Case 5: 32.601816
Case 6: 1390.500000
Source
2012 Asia Chengdu Regional Contest
题意:
一个人有两个相同的盒子,最开始每个盒子里都有n颗糖果,然后他每天选择其中的一个盒子(概率是P),从所选的那个盒子里拿出一颗糖果吃掉!
求当其中一个盒子里没有糖果的时候,另一个盒子剩余的糖果数的期望!
PS:
一开始看到题目以为是一道求期望的概率DP,当时由于n太大,二维根本就开不了!
所以就用只有写出数学公式!
但是数学公式,也会爆 long long! 然后 然后就只有用log取对数,然后exp !
首先给出数学公式:
其中的C(n+k,k)表示一共去取 n+k 次,其中有 k 次是取得另一个盒子(还有剩余糖果的盒子)!
我们在第一个盒子取了n+1次糖,最后那次取空时并没有取到糖果,
所以我们算期望值时的概率还是要用C(n+k,k)!
因为只有当第一个盒子被取了 n+1 次后我们才知道第一个盒子取空了!
所以是P^(n+1)!然后枚举剩余的糖果数n-i!
转:
思路:假设没糖的是A盒子,而B盒子还有0~n个糖。由于B盒子还有0个糖的情况的期望必为0,
所以省略,只需要计算1~n的。
(1)当A盒没有糖时,B盒就可能有1~n个糖,概率为C(n+i,i)*(p^n+1)*(1-p)n-i。
为啥还带个大C?这是情况的种数(想象取糖时还有个顺序,有C种可能的顺序),
不然的话,单靠这两个小于1的数是超级小的。
(2)根据(1)种的概率公式,穷举B盒可能还有 i 个糖,那么对于每种情况,
期望值为i*C(n+i,i)*(p^(n+1))*(1-p)(n-i),累加这些期望值就行了。
同理,B盒没有糖也是这样算,只是概率换成了(1-p)。两种情况的累加期望就是答案。
(3)这样还是不行,求C时会爆LL,对p求幂时结果又太小,精度损失严重。
C(n+i,i)*(p^(n+1))*(1-p)n-i这个式子的结果本身是不大的。考虑取这个式子对数,
化成相加的形式x=logC(n+i,i)+ log(p^(n+1))+log(1-p)^(n-i),(注意指数可以提到前面作为乘的形式)
,求出x作为指数来求ex这样就OK了(这个函数是exp(x) )。
(4)这个C还是很难求,比如当n=200000时,i 还没有到10时,C(200000+10, 10)就爆了。
对此,由于在穷举i时,C(n+i,i)是可以递推的,那么我们可以先将C给逐步取对数,
再相加就行了。递推是这样的,c+=log((n+i)/i)。
代码如下:
Problem Description
LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability
(1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?
Input
There are several test cases.
For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 105) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.
Any answer with an absolute error less than or equal to 10-4 would be accepted.
Sample Input
10 0.400000
100 0.500000
124 0.432650
325 0.325100
532 0.487520
2276 0.720000
Sample Output
Case 1: 3.528175
Case 2: 10.326044
Case 3: 28.861945
Case 4: 167.965476
Case 5: 32.601816
Case 6: 1390.500000
Source
2012 Asia Chengdu Regional Contest
题意:
一个人有两个相同的盒子,最开始每个盒子里都有n颗糖果,然后他每天选择其中的一个盒子(概率是P),从所选的那个盒子里拿出一颗糖果吃掉!
求当其中一个盒子里没有糖果的时候,另一个盒子剩余的糖果数的期望!
PS:
一开始看到题目以为是一道求期望的概率DP,当时由于n太大,二维根本就开不了!
所以就用只有写出数学公式!
但是数学公式,也会爆 long long! 然后 然后就只有用log取对数,然后exp !
首先给出数学公式:
其中的C(n+k,k)表示一共去取 n+k 次,其中有 k 次是取得另一个盒子(还有剩余糖果的盒子)!
我们在第一个盒子取了n+1次糖,最后那次取空时并没有取到糖果,
所以我们算期望值时的概率还是要用C(n+k,k)!
因为只有当第一个盒子被取了 n+1 次后我们才知道第一个盒子取空了!
所以是P^(n+1)!然后枚举剩余的糖果数n-i!
转:
思路:假设没糖的是A盒子,而B盒子还有0~n个糖。由于B盒子还有0个糖的情况的期望必为0,
所以省略,只需要计算1~n的。
(1)当A盒没有糖时,B盒就可能有1~n个糖,概率为C(n+i,i)*(p^n+1)*(1-p)n-i。
为啥还带个大C?这是情况的种数(想象取糖时还有个顺序,有C种可能的顺序),
不然的话,单靠这两个小于1的数是超级小的。
(2)根据(1)种的概率公式,穷举B盒可能还有 i 个糖,那么对于每种情况,
期望值为i*C(n+i,i)*(p^(n+1))*(1-p)(n-i),累加这些期望值就行了。
同理,B盒没有糖也是这样算,只是概率换成了(1-p)。两种情况的累加期望就是答案。
(3)这样还是不行,求C时会爆LL,对p求幂时结果又太小,精度损失严重。
C(n+i,i)*(p^(n+1))*(1-p)n-i这个式子的结果本身是不大的。考虑取这个式子对数,
化成相加的形式x=logC(n+i,i)+ log(p^(n+1))+log(1-p)^(n-i),(注意指数可以提到前面作为乘的形式)
,求出x作为指数来求ex这样就OK了(这个函数是exp(x) )。
(4)这个C还是很难求,比如当n=200000时,i 还没有到10时,C(200000+10, 10)就爆了。
对此,由于在穷举i时,C(n+i,i)是可以递推的,那么我们可以先将C给逐步取对数,
再相加就行了。递推是这样的,c+=log((n+i)/i)。
代码如下:
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 400000+10 double fac[maxn];//阶乘对数 void init() { fac[0] = 0;//wocao for(int i = 1; i <= maxn-5; i++) { fac[i] = fac[i-1]+log(i*1.0); } } int main() { init(); int n; int cas = 0; double p; while(~scanf("%d%lf",&n,&p)) { double ans = 0; double tt; for(int i = 0; i < n; i++) { tt = (n-i); ans+=tt * exp((fac[n+i]-fac -fac[i])+(n+1.0)*log(p*1.0)+(i*1.0)*log(1.0-p)); ans+=tt * exp((fac[n+i]-fac -fac[i])+(i*1.0)*log(p*1.0)+(n+1.0)*log(1.0-p)); } printf("Case %d: %.6lf\n",++cas,ans); } return 0; } /* 10 0.400000 100 0.500000 124 0.432650 325 0.325100 532 0.487520 2276 0.720000 */
相关文章推荐
- C++ 基础知识点 一第2章 基本数据类型、运算符与表达式
- 四则运算
- 组队编码规范
- Mysql数据库常用操作
- NP难问题求解综述
- 带你玩转Visual Studio——带你高效管理代码
- MRR(Mean Reciprocal Rank)简介
- 对于测试我们需要知道啥?
- Android SDK中资源的下载
- Leetcode catalogue
- 用循环链表解决约瑟夫循环
- 如何通过微信公众号/微信号获取对应的二维码
- [MYSQL]基础知识总结
- ubuntu下启动tomcat
- JAVA从学会配置开发环境开始
- DirectX11 创建和启用纹理
- 怎样判断平面上一个矩形和一个圆形是否有重叠?
- java线程池学习(三) —— ThreadPoolExecutor
- HDOJ 1181 变形课 DFS & BFS & Floyd
- linux 查找文件