1639 - Candy

Time limit: 3.000 seconds

1639 Candy
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?
Input
There are several test cases. 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.
Output
For 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

根据期望的定义,不妨设最后打开第1个盒子,此时第2个盒子有i颗,则这之前打开过n+n-i次盒子,其中有n次取得时盒子1,其余n-i次取得是盒子2,概率为C(2n - i, n)p^(n+1)*(1-p)^(n-i)。注意p的指数是n+1,因为除了前面打开过n次盒子1之外,最后又打开了以此,因此C(2n-i, n)可能非常大,而p^(n+1)和(1-p)^(n-i)却非常接近0.如果分别计算这三项再乘起来,会损失很多精度。所以利用对数处理,设v1(i)=ln(C(2n-i, n))+(n+1)ln(p)+(n-i)ln(1-p),则“最后打开第1个盒子”对应的数学期望为e^v1(i)。

同理,当最后打开第2个盒子,对数为v2(i)=ln(C(2n-i, n))+(n+1)ln(1-p)+(n-i)ln(p),概率为e^v2(i)。根据数学期望的定义,答案为sum{ i ( e^v1(i) + e^v2(i) ) }.

ps: long double 的使用linux下,%Lf与%llf皆可,%lf不可

此题需用long double

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 200005
long double LogB[ * MAXN] = {0.0};
int main()
{
repu(i, , * MAXN) LogB[i] = LogB[i - ] + log(i);
int n, kase = ;
double p;
while(~scanf("%d%lf", &n, &p))
{
double e = 0.0;
if(p == 0.0 || p == 1.0) e = n;
else
{
long double v1, v2;
long double logp = log(p), log_p = log(1.0 - p);
repu(i, , n + )
{
long double t = LogB[ * n - i] - LogB[n] - LogB[n - i];
v1 = t + (n + ) * logp + (n - i) * log_p;
v2 = t + (n - i) * logp + (n + ) * log_p;
e += (exp(v1) + exp(v2)) * (i);
} }
printf("Case %d: %.6lf\n", ++kase, e);
}
return ;
}

uva 1639--精度处理方法之取对数(uva 1639)的更多相关文章

  1. 【每日一题】 UVA - 11809 Floating-Point Numbers 阅读题+取对数处理爆double

    https://cn.vjudge.net/problem/UVA-11809 题意:很长orz 题解:算一下输入范围,发现用double是读不进来的,在这里wa了半天,(double 1e300  ...

  2. 【取对数】【哈希】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem J. Bobby Tables

    题意:给你一个大整数X的素因子分解形式,每个因子不超过m.问你能否找到两个数n,k,k<=n<=m,使得C(n,k)=X. 不妨取对数,把乘法转换成加法.枚举n,然后去找最大的k(< ...

  3. hdu 1568 (log取对数 / Fib数通项公式)

    hdu 1568 (log取对数 / Fib数通项公式) 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]= ...

  4. poj2661 Factstone Benchmark(大数不等式同取对数)

    这道题列出不等式后明显是会溢出的大数,但是没有必要写高精度,直接两边取对数(这是很简明实用的处理技巧)得: log2(n!)=log2(n)+log2(n-1)+...+log2(1)<=log ...

  5. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  6. 【转载】C#使用Except方法求取两个List集合的差集数据

    在C#语言的编程开发中,针对List集合的运算有时候需要计算两个List集合的差集数据,集合的差集是取在该集合中而不在另一集合中的所有的项.A集合针对B集合的差集数据指的是所有在A集合但不在B集合的元 ...

  7. 取对数求阶乘位数——lightoj1045

    /* 求 n! 在base进制下的位数 取对数,用换底公式,预处理对数前缀和 b^x = n! x = log_b(n!) = log_10(n!)/log_10(b) 对x向上取整即可 */ #in ...

  8. 【取对数+科学计数法】【HDU1060】 N^N

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. java中小数的处理:高精度运算用bigDecimal类,精度保留方法,即舍入方式的指定

    一. 计算机的小数计算一定范围内精确,超过范围只能取近似值: 计算机存储的浮点数受存储bit位数影响,只能保证一定范围内精准,超过bit范围的只能取近似值. java中各类型的精度范围参见:http: ...

随机推荐

  1. 序列的方法(str,list,tuple)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值 ...

  2. idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用

    HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath(); 前几天在使用idea的时候发现这个方 ...

  3. JSP学习——语法

    JSP模版元素 JSP表达式 JSP脚本片断 JSP注释JSP指令JSP标签 JSP内置对象如何查找JSP页面中的错误 1:JSP模版元素 : JSP页面中的HTML内容称之为JSP模版元素. JSP ...

  4. [转载] YouCompleteMe

    原文: http://blog.marchtea.com/archives/161#rd?sukey=fc78a68049a14bb2ba33c15948d34749e1eb616df07efe977 ...

  5. IO端口和IO内存的区别及分别使用的函数接口

    每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类.根据访问外设寄存器的不同方式,可以把CPU分成两大类.一类CPU(如M68K,Po ...

  6. Python学习(11)字典

    目录 Python 字典 访问字典中的值 修改字典 删除字典元素 字典键的特性 字典内置函数&方法 Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. ...

  7. 如何让Service自动重启而不被kill掉

    重写service的onStartCommand方法. @Override    public int onStartCommand(Intent intent, int flags, int sta ...

  8. Android中的启动模式(下)

    在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上 ...

  9. C++——并发编程

    一.高级接口:async()和Future 1.1 async()和Future的第一个用例 假设需要计算两个操作数的总和,而这两个操作数是两个函数的返回值.寻常加法如下: func1() + fun ...

  10. PHP SPL标准库之SplFixedArray使用实例

    SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快. 看看我本机的Benchmark测试: i ...