#P5147. 随机数生成器

随机数生成器

Description

HKE recently wrote a function rand(l,r)\text{rand}(l,r), where l,rl,r are positive integers and lrl \le r. This function returns, with equal probability, any positive integer in the interval [l,r][l, r]. Then he wrote another function:

int work(int x){
    if(x==1) return 0;
    else return work(rand(1,x))+1;
}

In Pascal, this code is:

function work(x:integer):integer;
begin
    if x=1 then exit(0);
    else exit(work(rand(1,x))+1);
end;

Given a positive integer nn, what is the expected value of the return value of work(n)\text{work}(n)?

Definition of expectation: Suppose all possible return values of work(n)\text{work}(n) are x1,x2,,xkx_1, x_2, \dots , x_k with probabilities p1,p2,,pkp_1, p_2, \dots, p_k, then the expectation is:

E=i=1kxipi.\mathbb{E}=\sum_{i=1}^{k}x_i p_i.

Input Format

A positive integer nn.

Output Format

A real number representing the expected value of work(n)\text{work}(n). Keep 55 decimal places.

2
2.00000
3
2.50000
100000
13.09014

Hint

[Sample 1 Explanation]
work(2)\text{work}(2) returns 11 with probability 1/21/2, returns 22 with probability 1/41/4, returns 33 with probability 1/81/8, and so on.
Therefore, the expectation is 1/2+2/4+3/8+=21/2+2/4+3/8+\dots=2.

Constraints
For 30%30\% of the testdata, n9n \le 9;
For 50%50\% of the testdata, n1000n \le 1000;
For 70%70\% of the testdata, n1000000n \le 1000000;
For 100%100\% of the testdata, 1n<2311 \le n < 2^{31}.

Translated by ChatGPT 5