Description:
At present, there are n bowls of A and B noodles each, and the method of throwing coins is used to decide which noodles to eat. You do not need to throw a coin when there is only one noodle. Enter n to ask for the mathematical expectation of throwing coins after eating all the noodles. (N <= 1000)
Analysis:
This phrase must be recognized:Mathematical expectation(Mathematical expectation ).
The general method of recalling mathematical expectations,E = Σ pi × xi. That is, the probability of all possible values X and then sum.
We only want to finish noodle A, and the result is as follows:Multiply by 2You can. The number of C (I, n) pairs is C [N] [I], and the N power of 2 is pow2 [N].
So after eating n bowl a noodles, I bowl B noodles (0 <= I <n)ProbabilityC [n-i-1] [I]/pow2 [n + I ];TimesN + I;
The expected formula is E =2 × SIGMA (C [n-i-1] [I]/pow2 [n + I]) x (n + I )), (0 <= I <n)
It can be understood as follows: a total of N + I bowl noodles are eaten, and a total of N + I coins are required, so there is a total possibility of pow2 [n + I. However, to meet the requirements of eating n bowls a and I bowl B, the last bowl must be a noodle (because the coin is just when a is finished ), therefore, the total number of conditions is, n-1 a and I B in the full arrangement, that is, the n-i-1 position to choose I placed B, the rest placed, A total of C [n-i-1] [I] cases.
The expected value is the sum of the number of coin-throwing times X probability in all cases, multiplied by 2.
Here, we can say that the question is just half done. Since the range of N is 1000, the power of the combination number C [N] [I] and 2 is very large. Required?Large NumberWhat about it?
The final output result of the analysis only requires two decimal places, and the answer will not exceed 2000. That is to say, we tryCalculate with double, As long as the calculation process does not exceed the range, the final output accuracy must be sufficient.
Check the value range of double, which is about± 1e308It is about 2 to the power of 1024.
Just look at pow2 [n + I]. The value of N + I is 2000, which is out of the range. After careful observation, the formula can be deformed, and pow2 [N] is mentioned outside the formula:
E =(2/pow2 [N]) × SIGMA (C [n-i-1] [I]/pow2 [I]) × (n + I )), (0 <= I <n)
In this way, the out of the formula and the pow2 [] array in it are all in the double range. However, I tried to calculate C [N] [I] in the way of Yang Hui triangle, and it was still later ......
SHP and SHP have been tossing for a long time. We can optimize it here. Observe the formula, because the items corresponding to C [x] [I] will be divided by pow2 [I], then we can calculate the edge division when calculating C [x] [I. Maybe it won't be out of scope.
Yang Hui triangle formula, C [N] [I] = C [n-1] [I] + C [n-1] [I-1].
NowNew C [N] [I]C [N] [I]/pow2 [I] is saved. deformation formula:C [N] [I] = C [n-1] [I] + C [n-1] [I-1]/2.
Why only in C [n-1] [I-1] This Division 2? Since C [n-1] [I] has been removed from pow2 [I], C [n-1] [I-1] has been removed from pow2 [I-1], to enable the denominator of C [N] [I] to be pow2 [I], remove 2 in C [n-1] [I-1.
Here, the question is basically solved perfectly (the actual derivation process takes a lot of time )......
Use nowNewly defined C [N] [I]To describe the final formula:
E =(2/pow2 [N]) × SIGMA (C [n-i-1] [I] × (n + I )), (0 <= I <n)
Highlights:
This is a set of zju competition questions held by SHP at Lijian on Saturday, mainly to prepare for the warm-up of the pearl sea competition.
The first three water questions were completed in about two hours, and the questions were pushed for about three hours. There were hundreds of twists and turns, and the formula was wrong several times ......
SHP's initial practice was not to calculate C [N] [I] in advance, but to calculate in real time. The time complexity increased to O (N ^ 3), but actually only O (N ^ 2) is required ), although the answer is correct, it still does not get rid of the fate of TLE ......
I changed it in my own way, and modified the formula with SHP again. In the next five hours, the AC finally fell ~ A burst of ecstasy ......
After a rough look at the ranking, we had a total of 4 questions in the 200-Person Competition, which should be about 40 people. We can get the Silver Prize. I'm glad to go to hongxingyuan for a meal ......
Although the program AC, but watching the scene, there are also several teams that soon fell through, I do not know is there a better formula? It's common for ZJU's cool guys to use this formula ...... Confused ing, looking forward to better solutions ..
Bytes ---------------------------------------------------------------------------------------------------------
# Include <stdio. h>
# Define N 1005
Double c [N + N] [N + N];
Double pow2 [N];
Double ans [N];
Int main ()
{
Int I, j, k, m, n, T;
// Pow2 []
Pow2 [0] = 1;
For (I = 1; I <N; I ++ ){
Pow2 [I] = pow2 [I-1] * 2.0;
}
// C [] []
For (I = 0; I <n; I ++ ){
C [I] [0] = 1;
C [I] [I + 1] = 0;
}
For (I = 1; I <n + N; I ++ ){
For (j = 1; j <= I; j ++ ){
C [I] [J] = C [I-1] [J-1]/2 + C [I-1] [J];
}
}
// Ans []
For (n = 1; n <= 1000; n ++ ){
Ans [N] = 0;
For (I = 0; I <n; I ++ ){
Ans [n] + = (c [n + I-1] [I]) * (n + I );
}
Ans [n]/= pow2 [n-1];
}
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
Printf ("%. 2lf/n", ans [n]);
}
Return 0;
}