In question B of Chengdu Regional in 2012, it took an hour to release the formula, but after a long time, I found that I could not control the accuracy. Then I suddenly thought that the number of combinations could be optimized using log, after the change, the probability question of comparison with water is AC.
# Include <stdio. h> # include <math. h> # define maxn 200005 double f [2 * maxn]; double c (int a, int B) {return f [a]-f [a-B]-f [B];} int main () {f [0] = 0; for (int I = 1; I <2 * maxn; I ++) {f [I] = f [I-1] + log (1.0 * I);} double ans; double p; int n, cas = 0; while (scanf ("% d % lf", & n, & p) = 2) {ans = 0; double u = log (p ), v = log (1-p); for (int I = 1; I <= n; I ++) {double k1 = c (2 * n-I, n-I) + (n-I) * u + (n + 1) * v; double k2 = c (2 * n-I, n-I) + (n-I) * v + (n + 1) * u; ans + = I * (exp (k1) + exp (k2);} printf ("Case % d: % lf \ n ", ++ cas, ans);} return 0 ;}