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?

InputThere are several test cases. 
For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 10 5) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal). 
Input is terminated by EOF.OutputFor 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

参考博客:https://www.cnblogs.com/xcw0754/p/4753221.html

题意:有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖。直到有一天打开盒子一看,这个盒子没有糖了。输入n,p,求此时另一个盒子里糖的个数的数学期望。

思路:假设没糖的是A盒子,而B盒子还有0~n个糖。由于B盒子还有0个糖的情况的期望必为0,所以省略,只需要计算1~n的。

  (1)当A盒没有糖时,B盒就可能有1~n个糖,概率为C(n+i,i)*(pn+1)*(1-p)n-i。为啥还带个大C?这是情况的种数(想象取糖时还有个顺序,有C种可能的顺序),不然的话,单靠这两个小于1的数是超级小的。

  (2)根据(1)种的概率公式,穷举B盒可能还有 i 个糖,那么对于每种情况,期望值为i*C(n+i,i)*(pn+1)*(1-p)n-i,累加这些期望值就行了。同理,B盒没有糖也是这样算,只是概率换成了(1-p)。两种情况的累加期望就是答案。

  (3)这样还是不行,求C时会爆LL,对p求幂时结果又太小,精度损失严重。C(n+i,i)*(pn+1)*(1-p)n-i这个式子的结果本身是不大的。考虑取这个式子对数,化成相加的形式x=logC(n+i,i)+ log(pn+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)。

  (5)总复杂度是O(n)。时间在500ms以下。

ac代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <string>
5 #include <iostream>
6 #include <cmath>
7 #include <iomanip>
8 using namespace std;
9 typedef long long ll;
10 int n;double p;
11 double solve(int n,double p)
12 {
13 double c=0;
14 double ans=n*exp((n+1)*log(p));
15 // cout<<ans<<endl;
16 for(int i=1;i<n;++i)
17 {
18 c+=log((n+i)*1.0/(i*1.0));
19 ans+=(n-i)*exp(c+(n+1)*log(p)+i*log(1-p));
20 // cout<<ans<<endl;
21 }
22 return ans;
23 }
24 int main()
25 {
26 cout<<setiosflags(ios::fixed)<<setprecision(6);
27 int cas=1;
28 while(cin>>n>>p)
29 {
30
31 double res=solve(n,p)+solve(n,1-p);
32 cout<<"Case "<<cas++<<": "<<res<<endl;
33 }
34 }

hdu 4465 Candy (非原创)的更多相关文章

  1. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  2. hdu 4465 Candy(二次项概率)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465 参考博客:http://www.cnblogs.com/goagain/archive/2012 ...

  3. hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)

    简单概率题,可以直接由剩余n个递推到剩余0个.现在考虑剩余x个概率为(1-p)的candy时,概率为C(2 * n - x, x) * pow(p, n + 1)  *pow(1 - p, n - x ...

  4. hdu 4465 Candy 数学

    思路:易知结果为 ∑(n-k)*C(n+k,k)*(p^(n+1)*q^k+q^(n+1)*p^k). 注意不能直接算,注意点技巧!!!看代码 代码如下: #include<iostream&g ...

  5. hdu 4465 Candy

    题解: 由题意得 需要运用: C(m,n)=exp(logC(m,n)) f[]=; ; i<=; i++) f[i]=f[i-]+log(i*1.0); double logC(int m,i ...

  6. HDU 4465 - Candy(概率与数学优化)

    2012成都Regional的B题,花了一个小时推出了式子,但是搞了好久发现都控制不了精度,后来突然想到组合数可以用log优化,改了之后就AC了 比较水的概率题 #include <stdio. ...

  7. hdu 4465 Candy 2012 成都现场赛

    /** 对于大数的很好的应用,,缩小放大,,保持精度 **/ #include <iostream> #include <cmath> #include <algorit ...

  8. HDU 4465 Candy (数学期望)

    题意:有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖.直到有一天打开盒子一看,这个盒子没有糖了.输入n,p,求此时另一个盒子里糖的个数的数学期望 ...

  9. Linux下high CPU分析心得【非原创】

    非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...

随机推荐

  1. MySQL索引性能分析

    为什么要做性能分析 你有没有这样的情况. 面对一个你没怎么写过的.复杂的业务,你构思了很久,终于开始敲下了第一段代码. 写的过程迷迷糊糊,有的时候还能把自己搞晕了. 但你还是终于把它写完了. 但是点击 ...

  2. 入门OJ:简单的网络游戏

    题目描述 在某款极具技术含量的网络游戏中,佳佳靠着他的聪明智慧垄断了游戏中的油田系统.油田里有许多油井,这些油井排成一个M*N的矩形.每个油井都有一个固定的采油量.每两个相邻的油井之间有一条公路,这些 ...

  3. 服务发现 ap cp 强一致性 最终一致性 dns vip ip

    为什么基于域名 08 | 服务发现:到底是要CP还是AP? https://time.geekbang.org/column/article/208171 为什么需要服务发现?先举个例子,假如你要给一 ...

  4. int ping = 11; 限流 客户端验证与服务端是连接的

    int ping = 11; ZooKeeper Programmer's Guide https://zookeeper.apache.org/doc/r3.1.2/zookeeperProgram ...

  5. super 多重继承 super() function with multilevel inheritance

    Python | super() function with multilevel inheritance - GeeksforGeeks https://www.geeksforgeeks.org/ ...

  6. 浅谈自动化构建之gulp

    一.gulp的基本使用 gulp是目前最流行的前端自动化构建系统,核心特点高效易用.(这块不过多的废话了,直接上干货了,有兴趣的话,可以查下gulp简介) 步骤如下: yarn init -y yar ...

  7. orm(Manager isn't accessible via %s instances" % cls.__name)报错

    报错信息 Manager isn't accessible via %s instances" % cls.__name 解决方法 https://www.jianshu.com/p/5e0 ...

  8. HTML定位

    定位(position) 定位是一种更加高级的布局手段,通过定位可以将元素摆放到元素的任何位置 使用position属性来设置定位 可选值:static 默认值,元素是静止的没有开启定位 relati ...

  9. Git实践笔记(二)

    前一篇中说完了本地仓库的一些操作,本篇将继续进行撤销修改和删除的操作,以及远端仓库的设置.提交.克隆. 撤销修改和删除 现在在test.txt文件里面增加一行,内容为add error msg,先通过 ...

  10. LINUX中备份mysql数据库

    1.在LINUX中创建备份目录 如:/home/backup   2.把sh脚本放到备份目录下   脚本如下 https://wws.lanzous.com/i1oCxlccapa密码:dter   ...