KonkurStudy
همه کارت‌ها

Generating Functions for Counting

Encode a sequence a0,a1,…a_0,a_1,\ldots as the coefficients of a power series ∑anxn\sum a_n x^n, then turn counting and recurrence problems into algebra: multiply to combine choices, and extract [xn][x^n] to read off the answer.

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

Definition and intuition

The ordinary generating function (OGF) of a sequence a0,a1,a2,…a_0, a_1, a_2, \ldots is the formal power series

A(x)=∑n≥0anxn=a0+a1x+a2x2+⋯A(x) = \sum_{n \ge 0} a_n x^{n} = a_0 + a_1 x + a_2 x^{2} + \cdots

Think of xnx^{n} as a bookmark for "size nn": the coefficient ana_n records how many objects have size nn. We never plug in a number for xx — it is a bookkeeping variable. Writing [xn]A(x)[x^{n}]A(x) for "the coefficient of xnx^{n}," the whole game is: build A(x)A(x) in closed form, then read off an=[xn]A(x)a_n = [x^{n}]A(x).

Key formulas

Closed forms come from the geometric and binomial series. The two you must know cold:

11−x=∑n≥0xn,1(1−x)k=∑n≥0(n+k−1k−1)xn.\frac{1}{1-x} = \sum_{n \ge 0} x^{n}, \qquad \frac{1}{(1-x)^{k}} = \sum_{n \ge 0} \binom{n+k-1}{k-1} x^{n}.

Replacing x→−xx \to -x gives the alternating-sign versions, e.g. 1(1+x)2=∑n≥0(−1)n(n+1) xn\dfrac{1}{(1+x)^{2}} = \sum_{n \ge 0} (-1)^{n}(n+1)\,x^{n}. The general negative binomial / Newton rule is [xn] (1+x)−k=(−1)n(n+k−1k−1)[x^{n}]\,(1+x)^{-k} = (-1)^{n}\binom{n+k-1}{k-1}.

Product = convolution

Multiplying two OGFs combines independent choices: if A(x)B(x)=C(x)A(x)B(x) = C(x) then

cn=∑i=0nai bn−i.c_n = \sum_{i=0}^{n} a_i\, b_{n-i}.

So "pick an AA-object of size ii and a BB-object of size n−in-i" is exactly a product. This is why 1(1−x)k\dfrac{1}{(1-x)^{k}} counts the ways to write nn as an ordered sum of kk nonnegative integers — it is (11−x)k\left(\tfrac{1}{1-x}\right)^{k}.

Operation on sequence Effect on A(x)A(x)
Shift right by 1 (an−1a_{n-1}) x A(x)x\,A(x)
Partial sums ∑i≤nai\sum_{i\le n} a_i A(x)/(1−x)A(x)/(1-x)
Convolution / combine choices A(x) B(x)A(x)\,B(x)
Scale an→cnana_n \to c^{n} a_n A(cx)A(cx)

How to apply it

  1. Model: write the OGF as a product/sum of simple pieces (one factor per independent choice or item type).
  2. Simplify: factor and cancel to reach a known closed form. Algebraic simplification before expanding usually saves all the work.
  3. Extract: expand each factor with the series above, then read off [xn][x^{n}] using convolution.

The same recipe solves linear recurrences: multiply the recurrence by xnx^{n}, sum, solve for A(x)A(x), do partial fractions, and extract coefficients.

Mini-example (konkur 1402, Q111)

Find a9a_9 where f(x)=1+x3(1+x)3f(x) = \dfrac{1 + x^{3}}{(1 + x)^{3}}. Simplify first using 1+x3=(1+x)(1−x+x2)1 + x^{3} = (1+x)(1 - x + x^{2}):

f(x)=(1+x)(1−x+x2)(1+x)3=1−x+x2(1+x)2.f(x) = \frac{(1+x)(1 - x + x^{2})}{(1+x)^{3}} = \frac{1 - x + x^{2}}{(1+x)^{2}}.

Expand 1(1+x)2=∑(−1)n(n+1)xn\dfrac{1}{(1+x)^{2}} = \sum (-1)^{n}(n+1)x^{n} and convolve with 1−x+x21 - x + x^{2}:

an=(−1)n[(n+1)+n+(n−1)]=(−1)n (3n)(n≥2),a_n = (-1)^{n}\big[(n+1) + n + (n-1)\big] = (-1)^{n}\,(3n) \quad (n \ge 2),

so a9=(−1)9⋅27=−27a_9 = (-1)^{9}\cdot 27 = -27.

Exam trick: Always factor and cancel before expanding. A scary rational function like 1+x3(1+x)3\frac{1+x^3}{(1+x)^3} usually collapses to a tiny polynomial over a low power of (1±x)(1\pm x), so the coefficients grow only polynomially in nn — distrust any answer choice that grows like 2n2^{n}.

فرمول‌های کلیدی

Ordinary generating function
A(x)=∑n≥0anxnA(x) = \sum_{n \ge 0} a_n x^{n}

Encodes the sequence (a_n) as the coefficients of a formal power series; a_n = [x^n] A(x).

Geometric series
11−x=∑n≥0xn\frac{1}{1-x} = \sum_{n \ge 0} x^{n}

The base building block; counts one of each size.

Negative-binomial / k-fold series
1(1−x)k=∑n≥0(n+k−1k−1)xn\frac{1}{(1-x)^{k}} = \sum_{n \ge 0} \binom{n+k-1}{k-1} x^{n}

Number of ways to write n as an ordered sum of k nonnegative integers; replace x by -x for alternating signs.

Newton coefficient extraction
[xn] (1+x)−k=(−1)n(n+k−1k−1)[x^{n}]\,(1+x)^{-k} = (-1)^{n}\binom{n+k-1}{k-1}

General rule for reading off a coefficient from a negative power of (1+x).

Convolution (product of OGFs)
[xn] A(x)B(x)=∑i=0nai bn−i[x^{n}]\,A(x)B(x) = \sum_{i=0}^{n} a_i\, b_{n-i}

Multiplying generating functions combines independent choices.

#generating-functions#ordinary-generating-function#power-series#binomial-series#coefficient-extraction#convolution#negative-binomial#recurrences#combinatorics

سوالات تمرینی · 8/9

همهٔ 9 سؤال →