HDU4465 Candy
Candy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3069 Accepted Submission(s): 1415
Special Judge
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?
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.
Any answer with an absolute error less than or equal to 10-4 would be accepted.
10 0.400000 100 0.500000 124 0.432650 325 0.325100 532 0.487520 2276 0.720000
Case 1: 3.528175 Case 2: 10.326044 Case 3: 28.861945 Case 4: 167.965476 Case 5: 32.601816 Case 6: 1390.500000
有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖。直到有一天打开盒子一看,这个盒子没有糖了。输入n,p,求此时另一个盒子里糖的个数的数学期望。
xcw0754的题解
思路:假设没糖的是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以下。
#include<iostream>
#include<cmath>
#define il inline
#define co const
template<class T>T read(){
T data=0,w=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
return data*w;
}
template<class T>il T read(T&x) {return x=read<T>();}
typedef long double LD;
co int N=4e5+1;
LD ln[N];
LD binom(int n,int m){
return ln[n]-ln[m]-ln[n-m];
}
LD solve(int n,LD p){
LD ans=0;
for(int i=0;i<=n;++i){
LD c=binom(2*n-i,n);
LD v1=c+(n+1)*log(p)+(n-i)*log(1-p);
LD v2=c+(n+1)*log(1-p)+(n-i)*log(p);
ans+=i*(exp(v1)+exp(v2));
}
return ans;
}
int main(){
for(int i=1;i<N;++i) ln[i]=ln[i-1]+log(i);
int kase=0,n;
LD p;
while(~scanf("%d%Lf",&n,&p))
printf("Case %d: %.6Lf\n",++kase,solve(n,p));
return 0;
}
HDU4465 Candy的更多相关文章
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- POJ - 1666 Candy Sharing Game
这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...
随机推荐
- TensorFlow.训练_资料(有视频)
ZC:自己训练 的文章 貌似 能度娘出来很多,得 自己弄过才知道哪些个是坑 哪些个好用...(在CSDN文章的右侧 也有列出很多相关的文章链接)(貌似 度娘的关键字是"TensorFlow ...
- Python 解leetcode:48. Rotate Image
题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...
- ElasticSearch中term和match探索
一.创建测试数据 1.创建一个index curl -X PUT http://127.0.0.1:9200/student?pretty -H "Content-Type: applica ...
- 怎样使用yum安装nginx
yum install -y nginx 以上.
- js 怎样判断用户是否在浏览当前页面
有些时候我们需要在项目中判断用户是否在浏览当前页面,或者当前页面是否处于激活状态.然后再进行相关的操作.浏览器中可通过window对象的onblur.onfocus判断,或者document的hidd ...
- 几个主流浏览器 Window.open打开新窗口 、模拟a标签打开新窗口的 表现
Window.open打开新窗口 1.常用浏览器打开新窗口(正常打开window.open)的的不同表现形式(PC/移动端) 2.Window.open在异步处理中打开(_blank) a标签在异步处 ...
- LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数) 题目: 求出大于或等于 N 的最小回文素数. 回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数. 例如,2,3,5,7 ...
- where用法
where 子句用于指定类型约束. 1.接口约束 public class MyGenericClass<T> where T:IComparable { } 2.基类约束: 指出某个类 ...
- ORM框架之EntityFramework介绍
ORM框架之EntityFramework介绍 1. 简介 大家好!我是高堂. 作为一位伪前端程序猿,我给大家介绍一下微软的自家的 ORM框架. ADO.NET Entity Framework 以下 ...
- Spark读取HDFS文件,任务本地化(NODE_LOCAL)
Spark也有数据本地化的概念(Data Locality),这和MapReduce的Local Task差不多,如果读取HDFS文件,Spark则会根据数据的存储位置,分配离数据存储最近的Execu ...