UVa 1639 Candy (mathematical expectation + combinatorial math + high-precision storage)

Source: Internet
Author: User

Test instructions: There are two boxes each with n sugars, randomly selected one at a time (probability is p,1-p), and then eaten, until one time, you open the box to find, no sugar!

Enter N,p to find the mathematical expectation of the number of sugars in the other box.

Analysis: First do not say this problem more pits, the first to use a long double to achieve high precision, I first use the double has WA, and later looked at the puzzle is a long double,

Changed, can always change the wrong, how the output is 2.00000, made a night, really no language, because I input the output data type is a long double,

The result has been wrong, maybe my compiler is C89, and C, input and output format is different, but I changed to C99 is not right ...

The last look at the puzzle understand that the input and output does not need so high precision, double is enough, can be considered a waste of the night time ... Tragic

The following analysis of the problem:

First of all we don't know which box to open last, so we need to consider that when the last Open is the first one, when the other one has I, then it must have been opened before

n+ (n-i) times box, where n times is the first box, n-i Times is the second box, which is obviously a two-item distribution, so the probability is C (2n-i, N) pn+1 (1-p) n-i, this place

Be sure to note that it is n+1, not n, because we have to count the last one.

The equation is obviously correct, but there is a big problem, that is, C (2n-i, N) The data is too big (because N is too big), it must not be preserved with plastic,

It cannot even be saved with a floating point, so we handle it with high precision, and the large number is chosen by the logarithmic method, so that v1 (i) = ln (C (2n-i, N)) + (n+1) ln (p) + (n-i) ln (1-p).

This place must be add and subtract, not multiplication, this is logarithm, just start again forget, result has been wrong ...

So the last one to open is the first box. The mathematical expectation is EV1 (i).

Similarly, the last one opened is the second box, with the logarithm of v2 (i) = ln (C (2n-i, N)) + (n+1) ln (1-p) + (n-i) ln (p), the probability is Ev2 (i).

Then the total mathematical expectation is sum{i* (EV1 (i) +ev2 (i)} ((EV1 (i) +ev2 (i)) this is the probability).

This problem requires high precision, so use a long double to store, really pit .... It's hard to think.

The code is as follows:

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < Cmath>using namespace Std;typedef long double ld;const int maxn = 400000 + 10; LD log_ld[maxn];void init () {    log_ld[1] = 0.0;    for (int i = 2; i < MAXN; ++i)        log_ld[i] = log_ld[i-1] + (LD) log ((LD) i);} double Solve (int n, ld p) {    ld ans = 0.0;    for (int i = 1; I <= n; ++i)        ans + = (LD) I * exp (log_ld[2*n-i]-log_ld[n]-log_ld[n-i] + (LD) (n+1) *log (p) + (LD) (N- i) *log (1.0-p));    for (int i = 1; I <= n; ++i)        ans + = i * exp (log_ld[2*n-i]-log_ld[n]-log_ld[n-i] + (LD) (n+1) *log (1.0-p) + (LD) (N- i) *log (p));    return ans;} int main () {//    freopen ("In.txt", "R", stdin);    int N, kase = 0;    Double p;    Init ();    while (~scanf ("%d%lf", &n, &p)) {        Double ans = solve (n, p);        printf ("Case%d:%.6f\n", ++kase, ans);    }    return 0;}

UVa 1639 Candy (mathematical expectation + combinatorial math + high-precision storage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.