An infinite series , manipulated modulo — we only ever compute the first coefficients. All the operations below are via NTT and Newton’s method.
Newton’s method — the master technique
To solve for a series , iterate
doubling the number of correct coefficients each step. Since the cost is dominated by the last doubling,
Every operation below is an instance of this. Recognising that is worth more than memorising the formulas.
The operations
Inverse —
Poly inverse(const Poly& a, int n) {
Poly b{ powmod(a[0], MOD - 2, MOD) }; // requires a[0] != 0
for (int k = 1; k < n; k <<= 1) {
Poly t = mulTrunc(a, b, 2 * k);
for (auto& x : t) x = MOD - x;
t[0] = (t[0] + 2) % MOD;
b = mulTrunc(b, t, 2 * k);
}
b.resize(n);
return b;
}Requires .
Logarithm — requires
Poly logSeries(const Poly& a, int n) {
return integrate(mulTrunc(derivative(a), inverse(a, n), n - 1), n);
}Exponential — requires
The most expensive of the basic operations, but still .
Square root — requires to be a quadratic residue
Power
for . Otherwise factor out : , raise the normalised part, and multiply back — being careful that or the result is zero.
regardless of how large is (only matters in the exponent).
The EGF dictionary
Working with exponential generating functions (), these operations have direct combinatorial meanings:
| Series operation | Combinatorial meaning |
|---|---|
| split a labelled set into two parts | |
| a set of -structures (the exponential formula) | |
| a sequence of -structures | |
| invert — recover connected structures from all structures | |
| mark and remove one element | |
| add a distinguished element |
The exponential formula — if is the EGF for connected structures, then is the EGF for all structures — converts “count connected graphs” into “count all graphs, then take a log”. That is a genuinely powerful move.
The standard series
What it is for
| Problem | Method |
|---|---|
| Count objects with a recursive structure | solve the functional equation, extract coefficients |
| Count set partitions, permutations by cycles | EGF + exp/log |
| Count labelled connected graphs | of the all-graphs EGF |
| Solve a linear recurrence | a rational GF; use Bostan-Mori |
| Sum | binomial transform = multiply by |
| Convert between OGF and EGF | multiply/divide by |
| Count with a “at least one of each” constraint | inclusion-exclusion, or |
| Partitions of an integer |
Practical notes
- Everything is modulo — truncate aggressively; carrying extra coefficients is pure waste.
- Precompute factorials and inverse factorials for EGF conversions.
- The base cases matter:
inverseneeds ,logneeds ,expneeds ,sqrtneeds to be a QR. Normalise first. - Use 998244353 so NTT applies directly.
- Truncated multiplication (
mulTrunc) that discards coefficients beyond the needed length saves roughly half the work.
See also: Generating Functions · FFT and NTT · Newton’s Method