P3978: [TJOI2015] Probability Theory — Solution
Derivation
First time doing this type of problem. Badass, badass (fist-palm salute).
First consider how to compute the denominator: if \(f_i\) is the number of distinct trees of size \(i\), then we obviously get \(f_i = \sum_{j = 0}^{n - 1} f_j f_{n - j - 1}\). Having got this far, divide-and-conquer FFT? No, the data range reaches an astonishing \(10^9\), so divide-and-conquer FFT is obviously impractical. We can keep deriving:
\[ \begin{aligned} F(x) &= 1 + xF^2(x), x \in [0, 1) \\ &= \frac{1 \pm \sqrt{1 - 4x}}{2x} = \frac{1 - \sqrt{1 - 4x}}{2x} \end{aligned} \]
Nothing jumps out, so we keep going and expand with the generalized binomial theorem:
\[ \begin{aligned} F(x) &= \frac{1 - \sqrt{1 - 4x}}{2x} \\ &= \frac{1 - (1 - 4x)^{\frac{1}{2}}}{2x} = \frac{1 - \sum_{i = 0}^{\infty} (-1)^i {\frac{1}{2} \choose i} (4x)^i}{2x} \\ &= \frac{1}{2x}(1 - \sum_{i = 0}^{\infty} (-4)^i {\frac{1}{2} \choose i} x^i) \\ &= \frac{1}{2x} - \sum_{i = 0}^{\infty} (-4)^i {\frac{1}{2} \choose i} x^{i - 1} = \frac{1}{2x} - (\frac{1}{2x} + \sum_{i = 1}^\infty (-4)^i {\frac{1}{2} \choose i} x^{i - 1}) \\ &= \sum_{i = 0}^\infty (-4)^{i + 1} \frac{\prod_{j = 0}^{i + 1}(\frac{1}{2} + i)}{(i + 1)!} x^{i} \end{aligned} \]
Expanding further, we find the coefficients are Catalan numbers, and then we can just plug them into the formula and compute.
Code
// P3978.cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
scanf("%d", &n);
printf("%.10lf\n", 1.0 * n * (n + 1) / double(2.0 * (2.0 * n - 1)));
return 0;
}