uva 1639--精度处理方法之取对数(uva 1639)
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)的更多相关文章
- 【每日一题】 UVA - 11809 Floating-Point Numbers 阅读题+取对数处理爆double
https://cn.vjudge.net/problem/UVA-11809 题意:很长orz 题解:算一下输入范围,发现用double是读不进来的,在这里wa了半天,(double 1e300 ...
- 【取对数】【哈希】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(< ...
- hdu 1568 (log取对数 / Fib数通项公式)
hdu 1568 (log取对数 / Fib数通项公式) 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]= ...
- poj2661 Factstone Benchmark(大数不等式同取对数)
这道题列出不等式后明显是会溢出的大数,但是没有必要写高精度,直接两边取对数(这是很简明实用的处理技巧)得: log2(n!)=log2(n)+log2(n-1)+...+log2(1)<=log ...
- 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 ...
- 【转载】C#使用Except方法求取两个List集合的差集数据
在C#语言的编程开发中,针对List集合的运算有时候需要计算两个List集合的差集数据,集合的差集是取在该集合中而不在另一集合中的所有的项.A集合针对B集合的差集数据指的是所有在A集合但不在B集合的元 ...
- 取对数求阶乘位数——lightoj1045
/* 求 n! 在base进制下的位数 取对数,用换底公式,预处理对数前缀和 b^x = n! x = log_b(n!) = log_10(n!)/log_10(b) 对x向上取整即可 */ #in ...
- 【取对数+科学计数法】【HDU1060】 N^N
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- java中小数的处理:高精度运算用bigDecimal类,精度保留方法,即舍入方式的指定
一. 计算机的小数计算一定范围内精确,超过范围只能取近似值: 计算机存储的浮点数受存储bit位数影响,只能保证一定范围内精准,超过bit范围的只能取近似值. java中各类型的精度范围参见:http: ...
随机推荐
- [SAP ABAP开发技术总结]BAPI调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- hdu 5673 Robot 卡特兰数+逆元
Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 转一篇讲camera的 mb好多年不搞3d 都忘光了
Camera定义 游戏中,Camera用来向用户展示场景,Camera就像一个摄像机,摄像机里面的景象就是Camera的展示范围,如下图所示: 在3D空间中Camera被定义为一个位置,有一个单位“方 ...
- Android中利用SharedPreferences保存信息
package com.example.sharepreferen; import android.content.Context; import android.content.SharedPref ...
- Andoid java文件中的Log检查工具
AndroidLogChecker 由于发布软件版本的时候我们需要把Log注释掉,此工具可以检查java类中的Log所在行以及是否已经注释. Github: https://github.com/cu ...
- Javascript链式调用案例
jQuery用的就是链式调用.像一条连接一样调用方法. 链式调用的核心就是return this;,每个方法都返回对象本身. 下面是简单的模拟jQuery的代码, <script> win ...
- Android 自定义Toast
自定义Toast 其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多 上图上代码 public class MainActivity extends Activity { ...
- select动态增加option
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...