I will not talk much about the questions.
Here, DP [I] [0] indicates the probability that I will be left in the second box after the first box is obtained. The expected result is DP [I] [0] * I.
DP [I] [1] indicates the probability that I will be left in the first box after the second box is obtained.
DP [I] [0] = P ^ (n + 1) * (1-p) ^ (n-I) * C (2 * n-I, n-I) = P ^ (n + 1) * (1-p) ^ (n-I) * (2 * n-I )! /(N-I )! /N!
DP [I + 1] [0] = P ^ (n + 1) * (1-p) ^ (n-i-1) * C (2 * n-i-1, n-i-1) = P ^ (n + 1) * (1-p) ^ (n-i-1) * (2 * n-i-1 )! /(N-i-1 )! /N!
DP [I] [0] = DP [I + 1] [0] * (1-p) * (2 * n-I)/(n-I)
The same is true for DP [I] [1 ].
If the initial value POW (p, n + 1) is assigned to DP [N] [0] At the beginning, if n is too large, the accuracy problem is 0.
Therefore, when n + 1 P is calculated, when the total number of answers is exceeded, the number of answers is multiplied one by one.
1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 using namespace STD; 5 const int n = 200005; 6 double DP [N] [2]; 7 8 int main () 9 {10 // freopen ("test. in "," rb ", stdin); 11 12 int n, CAS = 0; 13 Double P; 14 While (scanf (" % d % lf ", & N, & P )! = EOF) {15 double ans1 = 0; 16 double ans2 = 0; 17 int last [2]; 18 last [0] = last [1] = n + 1; 19 double Q = 1-P; 20 DP [N] [0] = 1; 21 DP [N] [1] = 1; 22 ans1 + = N; 23 ans2 + = N; 24 For (INT I = n-1; I> = 1; I --) {25 double tmp1 = Q * (2 * n-I)/(n-I ); 26 double tmp2 = p * (2 * n-I)/(n-I); 27 28 // recursive process, because the number is too large, P is directly open, if the data is big, the accuracy is 0 29. // when the total number of answers is exceeded each time, the P value is 30 DP [I] [0] = tmp1 * DP [I + 1] [0]; 31 DP [I] [1] = tmp2 * DP [I + 1] [1]; 32 ans1 + = (DP [I] [0] * I ); 33 while (ans1> N) {34 DP [I] [0] * = P; 35 ans1 * = P; 36 last [0] --; 37} 38 ans2 + = (DP [I] [1] * I); 39 while (ans2> N) {40 DP [I] [1] * = Q; 41 ans2 * = Q; 42 last [1] --; 43} 44} 45 // cout <"DP" <DP [N] <Endl; 46 ans1 * = POW (p, last [0]); 47 ans2 * = POW (Q, last [1]); 48 CAS ++; 49 50 printf ("case % d: %. 6f \ n ", Cas, ans1 + ans2); 51} 52 return 0; 53}
Accuracy of recurrence and double in HDU 4465