天天看點

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\n", ++kase, e);
    }
    return 0;
}      

View Code

轉載于:https://www.cnblogs.com/sunus/p/4504563.html