At first glance this question is bit computing, in fact, a probability distribution and related topics.
There y0 y1 y2 series 32bits unsigned number
x is initially 0, then the series and the number of exclusive-or operation, the operation N times, until x is 2 ^ 32-1, in fact, all bits are 1 x.
N demand expectations.
How to calculate expectations?
For each n is calculated corresponding to the probability p (n), then the weighted sum.
We now look one by one analysis
N is 1, then that y0 every bits are 1, the probability is 1 / (2 ^ 32)
N 2, were calculated for each bit, the first bit, the result is the probability that 1 minus the probability is not 1, the probability is not 1 is the first bit is not 0, the first bit y0 y1 is not 0, the probability is 1 / (2 ^ 2), then p (2) = (1-1 / (2 ^ 2)) ^ 32 - P (. 1)
N is 3, p (3) = ( 1-1 / (2 ^ 3)) ^ 32 - p (1) - p (2)
so
topic after ten decimal required, if the p (n) is small, the results are not affected by multiplying n, that is, p (n) * 10 ^ n is less than ten.
When I do a calculation of the probability of a data structure stored in the numerator and denominator. After the answer I came out, looked at the analysis of others, feel the direct use double to save the probability can give the correct answer, I did not try, uncertain.
Here is my code:
|
|