Test instructions
Each of the two boxes has n sugar, the probability of p per day from the first box to take a sugar, the probability of 1-p from the second box to a sugar. Until one day opening a box suddenly found that there is no sugar, the rest of the box to find the number of sweets expected.
Analysis:
Purple book above has been analyzed very clearly, but also gives a solution to the problem of precision loss, is to take the logarithm first and then the next.
1#include <cstdio>2#include <cmath>3 4 Const intMAXN =200000+5;5 Long DoubleLOGF[MAXN *2+1];6 7 Long DoubleLOGC (intNintm)8 {9 returnLogf[n]-logf[m]-logf[n-m];Ten } One A intMain () - { - //freopen ("In.txt", "R", stdin); the for(inti =1; I <= maxn*2; ++i) -Logf[i] = logf[i-1] +log (i); - - intN, Kase =0; + Doublep; - while(SCANF ("%D%LF", &n, &p) = =2) + { A DoubleAns =0.0; at for(inti =1; I <= N; ++i) - { - Long Doublec = LOGC (2*n-I, n); - Long DoubleV1 = C + (n+1) *log (P) + (n-i) *log (1-p); - Long DoubleV2 = C + (n+1) *log (1-p) + (n-i) *log (p); -Ans + = i * (exp (v1) +exp (v2)); in } -printf"Case %d:%.6f\n", ++Kase, ans); to } + - return 0; the}code June
UVa 1639 (desired) Candy