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 operationCombinatorial 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

ProblemMethod
Count objects with a recursive structuresolve the functional equation, extract coefficients
Count set partitions, permutations by cyclesEGF + exp/log
Count labelled connected graphs of the all-graphs EGF
Solve a linear recurrencea rational GF; use Bostan-Mori
Sum binomial transform = multiply by
Convert between OGF and EGFmultiply/divide by
Count with a “at least one of each” constraintinclusion-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: inverse needs , log needs , exp needs , sqrt needs 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