#P5147. Expected Value of Recursive Function Work
Expected Value of Recursive Function Work
Expected Value of Recursive Function Work
HKE has defined a random function \(\text{rand}(l, r)\) that picks an integer uniformly from the interval \([l, r]\) (inclusive), and then defined a recursive function work(x)
as below:
work(x) =
if x == 1 then return 0
else return work(rand(1, x)) + 1
The task is, given an integer \(n\), to compute the expected return value of work(n)
. Formally, if the possible return values are \(x_1, x_2, \dots, x_k\) with corresponding probabilities \(p_1, p_2, \dots, p_k\), the expectation is defined as:
\(\mathbb{E} = \sum_{i=1}^{k} x_i p_i\)
By analyzing the recurrence, we can derive that for \(n \ge 2\), the expected value \(E(n)\) satisfies:
[ E(1)=0,\quad E(n)=1+\frac{1}{n}\sum_{i=1}^{n}E(i)\quad (n>1). ]
After some algebra, it turns out that for \(n \ge 2\):
[ E(n)=E(n-1)+\frac{1}{n-1}, \quad \text{with } E(2)=2. ]
This recurrence resolves to:
[ E(n)=1+\sum_{i=1}^{n-1}\frac{1}{i} \quad (n \ge 2), \quad \text{and } E(1)=0. ]
Your task is to calculate \(E(n)\) for a given positive integer \(n\). Print the result as a floating-point number with 6 decimal places.
inputFormat
The input consists of a single line with one positive integer \(n\) (\(1 \le n \le 10^6\)).
outputFormat
Output the expected value of work(n)
with exactly 6 decimal places.
sample
1
0.000000