Candy----HDU4465----数学题
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465
题目意思:
有两个箱子,每个箱子装有N个糖果
打开第一个箱子的概率是P,另外一个就是1-P
当小明打开一个箱子的时候发现有糖果就会吃掉
有一天,小明打开其中的一个箱子,发现没有糖果了,求另外一个箱子的糖果数量的期望
这个公式其实是很好推的,枚举另外一个箱子剩余的数量来算就OK

这里的n经过了+1处理,这样我们枚举没有拿空的那个箱子里面拿了i个,那么剩下的就是n-i-1个
算概率的话,就是在前面的n+i-i次中我要在前面把拿空的箱子中拿n-1次再乘以概率p^n放,为什么前面是n-1,后面又是n呢?
因为最后一次一定要拿空的这个才可以,同理如果是另一个箱子
但是在算的时候不好算,因为要么是p^n后太小,要么是前面的组合数太大
所以只能边组合边乘
每次组合数可以用之前一个*(N+i-1)/i得到,为了避免爆double,
当数大于N(因为最终结果不可能大于N)的时候,乘以概率来减小,记录乘了多少次概率,最后算的时候少乘。
下面上代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const double eps = 1e-12; double fun(double s,double p,int n)
{
while(n)
{
if(s<eps)
return 0;
if(n&1)
s*=p;
n = n>>1;
p = p*p;
}
return s;
} int main()
{
int n;
double p;
int ca = 1;
while(~scanf("%d%lf",&n,&p))
{
double tmp1,tmp2;
tmp1 = tmp2 = 1;
double ans = 0;
n++;
int d=0;
for(int i=0;i<n;i++)
{
if(i)
{
tmp1 = tmp1*(1-p)*(n+i-1)/i;
tmp2 = tmp2*(p)*(n+i-1)/i;
while(tmp1>n || tmp2>n)
{
tmp1 = tmp1*p;
tmp2 = tmp2*(1-p);
d++;
}
}
ans += fun((n-i-1)*tmp1,p,n-d);
ans += fun((n-i-1)*tmp2,1-p,n-d);
}
printf("Case %d: %.6f\n",ca++,ans);
}
return 0;
}
除了这种方法以外,还有一种方法,神方法
就是在计算的时候会出现p^n这一步,太小,那么我们可以利用ln把n拿下来,然后再用e^n拿回去
具体见代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int n;
double p;
double p1,p2,s1,s2;
int ca = 1; while(~scanf("%d%lf",&n,&p))
{
double c = 0;
double ans = 0;
p1 = log(p);
p2 = log(1-p); //exp之后就是p^(n+1) or (1-p)^(n+1)
s1 = (n+1)*p1;
s2 = (n+1)*p2; for(int i=0;i<n;i++)
{
if(c+s1>-30 || c+s2>-30)//这一步一定要加,计算没意义,还会导致TLE
ans += (exp(c+s1)+exp(c+s2))*(n-i);
c+=log(n+i+1)-log(i+1);
s1+=p2;
s2+=p1;
}
printf("Case %d: %lf\n",ca++,ans);
} return 0;
}
Candy----HDU4465----数学题的更多相关文章
- HDU4465 Candy
Candy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- [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 ...
- ytu 2558: 游起来吧!超妹!(水题,趣味数学题)
2558: 游起来吧!超妹! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3[Submit][Status][Web Board ...
- 【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 ...
随机推荐
- codeforces 13E . Holes 分块
题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...
- 使用Unity开发HoloLens应用
https://developer.microsoft.com/en-us/windows/holographic/install_the_tools 导读:开发者们在陆续收到HoloLens开发者版 ...
- ThinkPHP中 按条件查询后列表显示
最近在项目中遇到了需要根据下拉框的条件筛选出符合条件的数据,然后进行列表显示的问题. 在ThinkPHP中进行列表显示的传统过程:通过在后台控制器中查询出数据,然后通过$this->assign ...
- android jar 第三方包
工程交叉了,做相互引用 1.单纯的代码jar 不引用res http://terryblog.blog.51cto.com/1764499/564558 1.2.连带源码一起打包出来 http:// ...
- 蓝牙1.1、蓝牙1.2、蓝牙2.0(蓝牙2.0+EDR)区别
蓝牙1.2版本相对于1.1版本: 1.Adaptive Frequency Hopping(AFH):即所谓适应性跳频技术,主要的功能是用来减少蓝牙产品与其它无线通讯装置之间所产生的干扰问题 2.Ex ...
- Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)
因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...
- 最终有SpringMvc与Struts2的对照啦
眼下企业中使用SpringMvc的比例已经远远超过Struts2,那么两者究竟有什么差别,是非常多刚開始学习的人比較关注的问题,以下我们就来对SpringMvc和Struts2进行各方面的比較: 1. ...
- li span兼容性问题
li与span的搭配使用所产的浏览器兼容性问题 1.ls两位,设定了width还是没用.2.总结了一下就是,里面的标签漂浮以后,就不能撑起外层的容器了. 3.li要设至少一个宽度或高度,还要加上ove ...
- Ueditor和CKeditor 两款编辑器的使用与配置
一丶ueditor 百度编辑器 1.官方文档,演示,下载地址:http://ueditor.baidu.com/website/index.html 2.百度编辑器的好:Editor是由百度web前端 ...
- SQL 2008升级SQL 2008 R2完全教程或者10.00.4000升级10.50.1600
今天将由于需要就将我的SQL 2008升级到SQL 2008 R2. 说到为什么要升级是因为,从另一台机器上备份了一个数据库,到我的机器上还原的时候提示“System.Data.SqlClient.S ...