Topic Links:
Hdu 4465 Candy
Title Description:
There are two boxes, each box has n candies, the probability of pumping the first box is p, the probability of pumping another box is 1-p. Each time you choose a box, there is candy to take one, no one to change another box. When asked to change the box, the expectation of sweets remained in the other box.
Problem Solving Ideas:
Note The title description, where any one box does not have candy, the other box is left with the expectation of the number of candies, rather than the first box without sweets. Instead of emptying one of the boxes, the other box has the expectation of candy, but one of the boxes is empty and another box is replaced.
The formula can be drawn according to the desired nature: ans = (n-i) * C (n+i,n) * (p^ (n+1) * (1-p) ^i + (1-p) ^ (n+1) *p^i) (0<=i<=n); The new problem is again, C (n+i,n) can overflow due to n , and p^ (n+1) can be very small and then overflow down. They can be taken Log,log (C (n+i,n)) range smaller, log (p^ (n+1)) becomes negative, convenient to save.
1 /*2 ****** The code of the unhandled precision, the inference is very beautiful and no eggs with ********3 expected Formula Ε=∑p * N P is the number of probability n4 p=p*c (n,m) *p^n* (1-p) ^ (m-n)5 C (m,n) =c (m-1,n) *m/(m-n)6 probability7 m=0 p^ (n+1)8 m=1 p^ (n+1) Q9 m=2 p^ (n+1) q^2Ten */ One#include <cstdio> A#include <iostream> -#include <algorithm> - using namespacestd; the - intMain () - { - intT, I, L =1; + Doubleans, p1, p2, res1, res2; - while(SCANF ("%d%lf", &t, &P1)! =EOF) + { AP2 = (1.0-p1); atRes1 = T *P1; -Ans =1; -Res2 = T *P2; - for(intI=1; i<=t; i++) - { -Ans *= P1 * P2 * (t + i)/i; inRes1 + = ans * (t-i); -Res1 *=P1; toRes2 + = ans * (t-i); +Res2 *=P2; - } theprintf ("Case %d:%6lf\n", l++, res1+res2); * } $ return 0;Panax Notoginseng}
1 /*2 take log processing accuracy problem, exp restore result3 */4#include <cmath>5#include <cstdio>6#include <iostream>7#include <algorithm>8 using namespacestd;9 Const intMAXN =400005;Ten DoubleLOGF[MAXN]; One voidInit () A { -logf[0] =0; - for(intI=1; i<maxn; i++) theLogf[i] = logf[i-1] + log (i*1.0); - } - DoubleCintMintN) - { + returnLOGF[M]-logf[n]-logf[m-n]; - } + intMain () A { at intN, cas =1; - DoubleP1, p2, ans; - init (); - while(SCANF ("%d%lf", &n, &P1)! =EOF) - { -Ans =0; inP2 =1.0-P1; -P1 =log (p1); toP2 =log (p2); + for(intI=0; i<n; i++) - { theAns + = ((n-i) * EXP (C (n+i, N) + (n+1) *p1 + i*p2)); *Ans + = ((n-i) * EXP (C (n+i, N) + (n+1) *P2 + i*p1)); $ }Panax Notoginsengprintf ("Case %d:%f\n", cas++, ans); - } the return 0; +}
Hdu 4465 Candy (fast permutation combination + probability)