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 ...
随机推荐
- javascript外部ファイル
function myFunction() { document.getElementById("demo").innerHTML = "Paragraph cha ...
- Linux下安装QT和OpenGL后QT无法使用OpenGL的解决方法
我的系统为Ubuntu14.04,用apt-get安装了实现了OpenGl的mesa,QT则是用官网下载的run文件来安装的. 好了,现在两个都分别有了,所以要在qt下尝试写OpenGl代码. 之前试 ...
- SQL Server 判断数据库是否存在,表是否存在
if DB_ID('testdb') is not null -- 如果这个数据库已经存在了 drop database testdb; create database testdb; if OBJE ...
- Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql
select msi.segment1 装配件编码 , msi.description 装配件描述 , msi.item_type ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
- JS学习笔记(一)基本数据类型和对象类型
js是一种弱类型的语言,所有的变量都用var进行声明,字符串用双引号或单引号括起来,常见基本数据类型为number,string,boolean等.如 var num = 123;或var num = ...
- 转载:SQL语句Where中使用别名作为判断条件
原文地址:http://www.cnblogs.com/dwfbenben/p/3307941.html 当我们使用某个表达式作为输出的一列时,我们无法再Where条件中直接使用该列作判断条件. ...
- .aspx.cs传值与取值
1:.aspx中post传值 $.post("ABP_ExchangeRatelz.aspx", { option: "isdelete", Ori_Curre ...
- win7(32 bit) + IE8 环境,IE8无法弹窗(错误提示:“此网页上的错误可能会使它无法正确运行”),有关的系统注册信息损坏——解决方法
错误截图如下: IE有关的系统注册信息损坏,导致IE无法正常弹窗. 解决办法:重新注册与IE有关的DLL文件,具体如下: 1.以管理员身份运行附件脚本(新建txt文件,将下面代码复制到txt文 ...
- 基础知识——Cocos2d-x学习历程(三)
1.场景与流程控制 我们把一些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在Cocos2d-x中,场景的实现是Scene. ...