/* each bottle contains N sweets; the probability of getting from the first bottle is P. When you get another candy, the expected calculation process of the remaining number of sweets in another bottle may cause overflow and overflow. */# include
# include
double lognjie [1, 400010]; double logc (int n, int m) {return lognjie [N]-lognjie [m]-lognjie [n-M]; // C (n, m) = n! /(N-m )! * M !) Log (C (n, m) = Log (N !) -Log (M !) -Log (m-N )!)} Int main () {int I, n, Index = 1; double p, q; lognjie [0] = 0; for (I = 1; I <= 400000; + + I) {lognjie [I] = lognjie [I-1] + Log (1.0 * I); // log (N !) = Log (n-1 )!) + Log (n)} while (scanf ("% d % lf", & N, & P )! = EOF) {double ret = 0; q = 1-P; for (I = 0; I <= N; ++ I) // The second box contains I. The other box has n + 1 times. {RET + = (n-I) * (exp (logc (n + I, I) + (n + 1) * log (1.0 * P) + I * log (1.0 * q) + exp (logc (n + I, I) + (n + 1) * log (1.0 * q) + I * log (1.0 * p);} printf ("case % d: %. 6lf \ n ", index ++, RET);} return 0 ;}
/* Expected formula percentile = Σ p * n p is the probability n is the number p = p * C (n, m) * PN * (1-p) m-n c (m, n) = C (m-1, n) * m/(m-N) probability m = 0 P ^ (n + 1) M = 1 P ^ (n + 1) QM = 2 P ^ (n + 1) q ^ 2q power through the loop, how to control P also needs to be supplemented */# include <math. h> # include <stdio. h> double Pro (int n, Double P) {double Zhong = 1, ret = N * P; For (int m = 1; m <= N; ++ m) // take M from the second bottle {Zhong * = p * (1-p) * (m + n)/m; RET + = (n-m) * Zhong; RET * = P;} return ret;} int main () {int N, Index = 1; Double P; while (~ Scanf ("% d % lf", & N, & P) {double ret = Pro (n, p) + Pro (n, 1-p ); printf ("case % d: %. 6lf \ n ", index ++, RET);} return 0 ;}