---
id: 2000
lang: en
translation_of: index.md
source_sha256: 7c6485834692221735d2179307d0e09d78560e7d2348273f26918fed5413f063
title: "P3978: [TJOI2015] Probability Theory — Solution"
description: "Counting trees by size gives a generating function that the generalized binomial theorem expands into Catalan numbers, leading to a closed formula."
tags: ["OI","Generating Function"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "坐到这种类型的题 is read as a typo for 做到 (“did / ran into”), rendered “doing this type of problem”."
  - "广义二项式 (generalized binomial) rendered “the generalized binomial theorem”."
---

## Derivation

First time doing this type of problem. <span class="slang" data-zh="社会社会" data-gloss="shèhuì shèhuì, “society, society”. Meme slang from 社会人 (tough gangster types): calling something “very society” means it is badass, said with mock respect.">Badass, badass</span> (<span class="slang" data-zh="抱拳" data-gloss="bàoquán, “cupping the fist”. A traditional martial-arts salute of one fist in the other palm; a mock-respectful gesture.">fist-palm salute</span>).

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:

<!--more-->

$$
\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

```cpp
// 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;
}
```
