Square of Fortune#12336

Square of Fortune (Editorial)

The random variable is uniformly distributed on the segment . For n > 0, its probability density is constant on this segment and equal to zero outside it:

f(x) = \frac{1}{n}, 0 ≤ x ≤ n

The payoff W = X^2 is a function of the random variable . Therefore, its expected value is computed as the integral of x^2 multiplied by the density over the entire range of possible values of :

E[W] = E[x^2] = ∫_0^n x^2 f(x)dx = \frac{1}{n} ∫_0^n x^2 dx = \frac{1}{n} \cdot \frac{n^3}{3} = \frac{n^2}{3}

As a check, the result can be obtained another way, using the variance. For the uniform distribution on , it is known that E[X] = n / 2 and Var[X] = n^2 / 12. Then

E[X^2] = Var[X] + (E[X])^2 = n^2 / 12 + n^2 / 4 = n^2 / 3

Both approaches give the same answer.

Thus, the answer is computed by the formula n^2 / 3 in time.

Example

For n = 10 the answer is

E[x^2] = \frac{10^2}{3} = 33.333333

Algorithm implementation

Read the input value .

scanf("%lld", &n);

Compute and print the answer.

res = n * n / 3.0;
printf("%lf\n", res);