题目地址: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----数学题的更多相关文章

  1. HDU4465 Candy

    Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  4. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  7. ytu 2558: 游起来吧!超妹!(水题,趣味数学题)

    2558: 游起来吧!超妹! Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 7  Solved: 3[Submit][Status][Web Board ...

  8. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. 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 ...

  10. [LintCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. MAC环境下生成Apple证书教程

    在MAC操作系统下,生成Apple证书比较简单,全图形化操作. 一.使用Keychain Access(钥匙串访问) MAC操作系统对证书的处理都采用了“Keychain Access”(中文系统名为 ...

  2. CPU核心数

    Process.ProcessorAffinity 属性: public IntPtr ProcessorAffinity { get; set; }属性值:位掩码,表示关联进程内的线程可以在其上运行 ...

  3. 转: ES6异步编程: co函数库的含义与用法

    转: ES6异步编程: co函数库的含义与用法 co 函数库是著名程序员 TJ Holowaychuk 于2013年6月发布的一个小工具,用于 Generator 函数的自动执行. 比如,有一个 Ge ...

  4. sim卡中的汉字存储格式

    Sim卡中的ucs2格式 Sim卡中的中文都是以ucs2格式存储的,ucs2和unicode只是字节序不同,unicode是小头在前,ucs2是大头在前. Ucs2与GB2312互换可以用VC中的Wi ...

  5. [原]CAS和Shiro在spring中集成

    shiro是权限管理框架,现在已经会利用它如何控制权限.为了能够为多个系统提供统一认证入口,又研究了单点登录框架cas.因为二者都会涉及到对session的管理,所以需要进行集成. Shiro在1.2 ...

  6. 深入分析MFC文档视图结构(项目实践)

    k_eckel:http://www.mscenter.edu.cn/blog/k_eckel 文档视图结构(Document/View Architecture)是MFC的精髓,也是Observer ...

  7. openstack 的 policy 问题。

    想写nova的policy的实现, 但是发现网上,有人写的很不错了. ref: http://blog.csdn.net/hackerain/article/details/8241691 但是,po ...

  8. 菜鸟级SQL Server21天自学通(文档+视频)

    SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言.SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据 ...

  9. 面向对象程序设计-C++_课时12访问限制

    private: 只有这个类(相同的类,不同的对象也可以)的成员函数可以访问这些成员变量或函数 public: 任何人都可以访问 protected: 只有这个类以及它的子子孙孙可以访问

  10. kafka学习(四)-Topic & Partition

    topic中partition存储分布 Topic在逻辑上可以被认为是一个queue.每条消费都必须指定它的topic,可以简单理解为必须指明把这条消息放进哪个queue里.为了使得 Kafka的吞吐 ...