Encode a sequence as the formal power series

Operations on sequences become algebra on the series, and the coefficient extraction recovers the answer.

The dictionary

Sequence operationSeries operation
(convolution)
(shift right)
(shift left)
(prefix sums)
choose any number of copies of one item
choose at most one copy

The essential series

Why they help

Counting with constraints

“How many ways to make using coins , unlimited?”

and the answer is of that. Each factor encodes one coin’s choices; multiplying combines them. This makes coin change a mechanical derivation rather than a DP to be invented.

“…using each coin at most twice?” → replace with .

Solving recurrences

For with :

and partial fractions give Binet’s formula. In general, a linear recurrence corresponds to a rational generating function, whose denominator is the reversed characteristic polynomial. That correspondence is what Bostan-Mori exploits to compute the -th term in .

Deriving identities

gives Vandermonde’s identity by comparing coefficients. Most binomial identities fall out this way.

Exponential generating functions

For labelled structures, use . Then the product corresponds to splitting a labelled set into two parts:

StructureEGF
Any set
Non-empty set
Permutations
Cycles
Derangements
Set partitions (Bell)
Involutions

The exponential formula — “if is the EGF for connected structures, then is the EGF for all structures” — is remarkably powerful: it turns “count graphs” into “count connected graphs” and back.

Computing with them

OperationCost
Multiply two series to terms with NTT
Inverse by Newton iteration
,
,
Extract of a rational function (Bostan-Mori)
Composition (or better)

See Formal Power Series for the implementations.

When to reach for them

  • The problem is “count the ways to combine independent choices” → multiply the per-choice series.
  • The recurrence is linear with constant coefficients → rational GF, then Bostan-Mori.
  • The objects are labelled and split into independent parts → EGF and the exponential formula.
  • You need for one huge → find the closed form, then extract.
  • You need all coefficients up to → NTT-based series operations.

Even when you end up writing an ordinary DP, deriving the generating function first often reveals the right state and transition.

See also: Formal Power Series · Combinatorics · Bostan-Mori