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[2 * MAXN] = {0.0};
int main()
{
    repu(i, 1, 2 * MAXN) LogB[i] = LogB[i - 1] + log(i);
    int n, kase = 0;
    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, 1, n + 1)
                {
                    long double t = LogB[2 * n - i] - LogB[n] - LogB[n - i];
                    v1 = t + (n + 1) * logp + (n - i) * log_p;
                    v2 = t + (n - i) * logp + (n + 1) * log_p;
                    e += (exp(v1) + exp(v2)) * (i);
                }

            }
        printf("Case %d: %.6lf
", ++kase, e);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/sunus/p/4504563.html