\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{latexsym}
\usepackage{verbatim}
\usepackage[pdftex]{graphicx}
\usepackage{wasysym}
\usepackage{url}
\usepackage{mathrsfs}
\usepackage{hyperref}

\hypersetup{colorlinks=true}
\setlength{\oddsidemargin}{.25in}
\setlength{\evensidemargin}{.25in}
\setlength{\textwidth}{6in}
\setlength{\topmargin}{-0.4in}
\setlength{\textheight}{8.5in}

\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\input{preamble.tex}

\begin{document}

\lecture{2: Generating Functions}{Weeks 2-3}


This week's notes are on \textbf{generating functions}, a new way to count objects!  We start with the basics here:

\section{Generating Functions}
If you've taken Calculus BC or an equivalent class in high school, you've probably ran into \textbf{power series} before.  In case you haven't, here's a quick definition:

\begin{defn}
Suppose we have a sequence $\{a_n\}_{n=0}^\infty = (a_0, a_1, a_2, \ldots)$ of numbers.  We can form the \textbf{formal power series} associated to this sequence as follows:
\begin{align*}
A(x) = \sum_{n=0}^\infty a_n x^n.
\end{align*}

By a formal power series, we simply mean that we are considering this object above not as something that is a function of $x$, but rather as a collection of convenient placeholders to index our values $a_1, a_2, a_3, \ldots $ with.  In other words, we're usually not going to worry about ``plugging in values for $x$;'' instead, we're going to take this object and just pretend that all of the $x^i$'s are placeholders that allow us to tell $a_1$ and $a_2$ and $a_3$ and so on apart.

Given a formal power series, we can manipulate it in various ways!  For example, we can scale it by a number:
\begin{align*}
c \cdot A(x) = \sum_{n=0}^\infty c \cdot a_n x^n.
\end{align*}
We can add two formal power series:
\begin{align*}
A(x) &= \sum_{n=0}^\infty a_n x^n, B(x) = \sum_{n=0}^\infty b_n x^n\\
\Rightarrow \quad A(x) + B(x) &= \sum_{n=0}^\infty (a_n+ b_n) x^n.
\end{align*}
We can define a formal notion of ``derivative,'' where we replace each $x^n$ with $nx^{n-1}$:
\begin{align*}
\frac{d}{dx} A(x) = \sum_{n=1}^\infty a_n \cdot n x^{n-1} = \sum_{n=0}^\infty (n+1)a_{n+1} x^n. 
\end{align*}
We can also take the product of two formal power series!  This is a little more involved: to calculate the power series that is equal to
\begin{align*}
\left(\sum_{n=0}^\infty a_n x^n \right)\cdot \left(\sum_{n=0}^\infty b_n x^n\right),\\
\end{align*}
we basically need to do the same process that we used to FOIL polynomials when we were younger!  In other words, we need to take every term on the left, and multiply each one of those by every term on the right.  Because there are infinitely many, this might seem awful to write down; so, to help, let's try to restrict ourselves to a somewhat easier problem.  Suppose we want to calculate our product, but we are just trying to figure out how many $x^m$'s we get on the right-hand side, for some fixed value $m$.  How does this work?

Well: let's go through the product $\left(\sum_{n=0}^\infty a_n x^n \right)\cdot \left(\sum_{n=0}^\infty b_n x^n\right)$ term-by-term.  If we just look at the $a_0 x^0$ term from the left part, we can see that we'll have to multiply this by the $b_mx^m$ term from the right part to get a $x^m$.  Similarly, if we look at $a_1 x^1$, we'll need to multiply this by $b_{m-1} x^{m-1}$ from the right to get to $x^m$; in general, if we're looking at the $a_k x^k$ term on the left, we need to multiply it by $b_{m-k}x^{m-k}$ to have the result be a multiple of $x^m$.

Therefore, if we're trying to find \textbf{all} of the $x^m$'s, we're actually just calculating the sum
\begin{align*}
\left(\sum_{k=0}^m a_k b_{m-k}\right) x^m.
\end{align*}

But if we know the coefficients of each $x^m$ for every $m$, that gives us all of the terms in our product!  In other words, we've shown the following:

\begin{align*}
\left(\sum_{n=0}^\infty a_n x^n \right)\cdot \left(\sum_{n=0}^\infty b_n x^n\right) = \sum_{n=0}^\infty \left( \sum_{k=0}^n a_k b_{n-k} \right)x^n 
\end{align*}
So we can multiply power series as well!

In general, we say that two formal power series are equal if and only if each term is equal: i.e. $\left(\sum_{n=0}^\infty a_n x^n \right) =  \left(\sum_{n=0}^\infty b_n x^n\right)$ if and only if $a_n = b_n$ for all $n$.
\end{defn}


So: if you've seen power series before, you may remember that most of the times where they come up, they've been objects where you've used your knowledge about how \textbf{sequences} work to study them!  In other words, your proofs have probably looked like the following:
\begin{align*}
\Bigg(\textrm{knowledge of }\{a_n\}_{n=1}^\infty \Bigg) \Rightarrow \left(\textrm{knowledge of } \sum_{n=1}^\infty a_nx^n\right).
\end{align*}
We typically do this in calculus classes because, usually, we understand sequences \textit{better} than we understood power series.  However, this is not necessarily true!  Given enough time in calculus/analysis classes, you will develop a lot of intuition for power series and Taylor series.  Given this,  it is perhaps natural to ask if we can \textbf{reverse} the method described above.  In other words: suppose that we have a sequence that we want to study.  What if we turned it into a power series, and used our knowledge of how that power series works to answer questions about the original series?  I.e. can we make proofs that look like
\begin{align*}
 \left(\textrm{knowledge of } \sum_{n=1}^\infty a_nx^n\right) \Rightarrow\Bigg(\textrm{knowledge of }\{a_n\}_{n=1}^\infty \Bigg)  ?
\end{align*}

The answer to this question is a resounding \textbf{yes}!  In mathematics, this process is called the method of \textbf{generating functions}.  This works as follows:
\begin{itemize}
\item Take some sequence $\{a_n\}_{n=1}^\infty$ that you want to study.
\item Look at the associated power series $\sum_{n=1}^\infty a_nx^n$.
\item Find a nice closed form (i.e. like $\sum_{n=0}^\infty x^n = \frac{1}{1-x}$) for this power series, using algebra/our operations on power series / clever identities from calculus /etc.
\item Use this closed form somehow to regain information about your original sequence.  I.e. your closed form may have a different expansion that you can figure out, via Taylor series: therefore, because power series are unique, you know that the terms in this different expansion have to be equal to the terms  $\sum_{n=1}^\infty a_nx^n$ in your original expansion!  In other words, you've found new information about your sequence $\{a_n\}_{n=1}^\infty$!
\end{itemize}

We illustrate this with an object we studied last week:
\subsection{Fibonacci numbers.}

\begin{defn}
The \textbf{Fibonacci sequence} is the following sequence of numbers, defined as follows:
\begin{itemize}
\item Base cases: $f_0 = 0$ and $f_1 = 1$.
\item Recursive definition: for any $n \geq 2$, we define $f_n = f_{n-1} + f_{n-2}$.
\end{itemize}
\begin{align*}
\end{align*}
So, for example, the first nine entries of the  Fibonacci sequence are the following:
\begin{align*}
0,1,1,2,3,5,8,13,21,34,55, 89, \ldots
\end{align*}
We let $f_n$, the $n$-th Fibonacci number, be the $n$-th element of this sequence.
\end{defn}

As defined, it is reasonably difficult to calculate these numbers.  I.e. to find $f_{1000}$, we need to find the Fibonacci numbers $f_{999}, f_{998}$, for which we also need the Fibonacci number $f_{997}$, for which we need the next Fibonacci number $f_{996}$\ldots all the way down to $f_1, f_0$.  In other words, we have to find \textbf{all} of the Fibonacci numbers from 1 to 999 to find $f_{1000}$. 

This seems\ldots wasteful!  I.e.\ if all we care about is $f_{1000}$, it seems somewhat silly to have to calculate every number along the way to get to $f_{1000}$.  This is certainly not how we work with other arithmetical operations; that is, to calculate $1000\cdot x$, we don't need to calculate $n \cdot x$ for every value of $n$ between 1 and 999 first!

Therefore, a natural question to ask here is the following:\ can we find a \textbf{closed form} for these numbers $f_n$?  In other words, can we a way of calculating $f_n$ without having to find $f_{n-1}$ and $f_{n-2}$?


\noindent\textbf{Answer:}  Yes!  Specifically, we can do this with \textbf{generating functions}!  

To start, let's look at the power series
\begin{align*}
\sum_{n=0}^\infty f_nx^n.
\end{align*}

The only thing we know about the constants $f_n$, at first, is their recurrence relation $f_{n} = f_{n-1} + f_{n-2}.$  So: let's plug that in to our power series!  Specifically, let's plug that into all of the terms $f_n$ with $n \geq 2$, as those are the terms where this recurrence relation holds:
\begin{align*}
\sum_{n=0}^\infty f_nx^n &= f_0 \cdot x^0 + f_1 \cdot x^1 + \sum_{n=2}^\infty f_n x^n\\
&= 0 + x + \sum_{n=2}^\infty (f_{n-1} + f_{n-2}) x^n \\
&= x + \sum_{n=2}^\infty f_{n-1}x^n + \sum_{n=2}^\infty f_{n-2}x^n\\
&= x + x\sum_{n=2}^\infty f_{n-1}x^{n-1} + x^2\sum_{n=2}^\infty f_{n-2}x^{n-2}\\
&= x + x\sum_{n=1}^\infty f_{n}x^{n} + x^2\sum_{n=0}^\infty f_{n}x^{n}\\
\end{align*}
where we justfied this last step by just shifting our indices (i.e. the sum starting at 2 of $f_{n-1}x^{n-1}$ is just the sum starting at 1 of $f_nx^n$.)  Finally, if we notice that because $f_0 = 0$, we have $x\sum_{n=1}^\infty f_{n}x^{n} = x\sum_{n=0}^\infty f_{n}x^{n}$, we finally have
\begin{align*}
\sum_{n=0}^\infty f_nx^n &= x + x\sum_{n=0}^\infty f_{n}x^{n} + x^2\sum_{n=0}^\infty f_{n}x^{n}\\
\Rightarrow \sum_{n=0}^\infty f_nx^n - x\sum_{n=0}^\infty f_nx^n - x^2\sum_{n=0}^\infty f_nx^n &= x\\
\Rightarrow (1-x-x^2)\sum_{n=0}^\infty f_nx^n &= x\\
\Rightarrow \sum_{n=0}^\infty f_nx^n &= \frac{x}{1-x-x^2}.\\
\end{align*}

Sweet!   A closed form.  So: according to our blueprint, we want to use this closed form to find information about our original series, possibly by finding another way to expand it.  

To start this, we first notice that with the quadratic formula, we can see that $1-x-x^2$ has roots $-\frac{ 1 \pm \sqrt{5}}{2}$, and therefore that we can factor $1-x-x^2$ as follows:
\begin{align*}
1-x-x^2 = -\left(x + \frac{1 + \sqrt{5}}{2}\right)\left(x + \frac{1 - \sqrt{5}}{2}\right).
\end{align*}
You may recognize the fraction $\frac{1+\sqrt{5}}{2}$ as the \textbf{golden ratio} $\varphi$, a famous mathematical constant that shows up in all sorts of odd places!  This has a few useful properties, especially in relation to the other root $\frac{1-\sqrt{5}}{2}$, which we list here for convenience's sake:
\begin{align*}
-\frac{1}{\varphi} &= -\frac{2}{1+\sqrt{5}} = -\frac{2(1-\sqrt{5})}{(1+\sqrt{5})(1-\sqrt{5})} = -\frac{2(1-\sqrt{5})}{-4} = \frac{1-\sqrt{5}}{2},\\
1 - \varphi &= 1 - \frac{1 + \sqrt{5}}{2} = \frac{1 - \sqrt{5}}{2} = -\frac{1}{\varphi},\\
1 - \varphi - \varphi^2 &= 0 \Rightarrow \varphi^2 = 1-\varphi = -\frac{1}{\varphi}
\end{align*}

We use these observations to further modify our polynomial:
\begin{align*}
1-x-x^2 &= -\left(x - \frac{1 + \sqrt{5}}{2}\right)\left(x - \frac{1 - \sqrt{5}}{2}\right)\\
&= -( x + \varphi)(x + (-1/\varphi))\\
&= -(x + \varphi) \cdot \frac{1}{\varphi} \cdot \varphi \cdot (x + (-1/\varphi))\\
&= -((1/\varphi)x + 1)(\varphi x - 1)\\
\end{align*}

We can use this observation to cleverly split our expression for the Fibonacci series' power series, using the technique of \textbf{partial fractions}\footnote{If you haven't seen this before: this is just the mathematical technique where we replace a fraction of the form $\frac{1}{A(x)B(x)}$ with some clever expression $\frac{C(x)}{A(x)} + \frac{D(x)}{B(x)}$, by ``undoing'' the common-denominator step that occurs when we add two fractions.}:
\begin{align*}
\frac{x}{1-x-x^2} &= \frac{x}{(1-x\varphi) \cdot (1 - x(-1/\varphi))} \\
&= \frac{1}{\varphi - (-1/\varphi)} \cdot \left(\frac{1}{1-x\varphi} - \frac{1}{1-x(-1/\varphi)} \right)\\
&= \frac{1}{\sqrt{5}} \cdot  \left(\frac{1}{1-x\varphi} - \frac{1}{1-x(-1/\varphi)} \right)\\
\end{align*} 


To deal with these two fractions, we now notice the following power series identity: for any $y$, we have as a formal power series the equation
\begin{align*}
\sum_{n=0}^\infty y^n = \frac{1}{1-y}.
\end{align*}
This is not hard to check; if we multiply the LHS by $(1-y)$, we can see that we get
\begin{align*}
(1-y) \sum_{n=0}^\infty y^n &= \sum_{n=0}^\infty y^n - \sum_{n=0}^\infty y^{n+1} \\
&= \sum_{n=0}^\infty y^n - \sum_{n=1}^\infty y^{n}\\
&= 1 + \sum_{n=1}^\infty y^n - \sum_{n=1}^\infty y^{n}\\
&= 1.\\
\Rightarrow\qquad \qquad \sum_{n=0}^\infty y^n &= \frac{1}{1-y}.\\
\end{align*}

In particular, if we let $y = \varphi x$ or $y = (-1/\varphi)x$, we get
\begin{align*}
\frac{1}{1-x\varphi} = \sum_{n=0}^\infty (x\varphi)^n, \quad \frac{1}{1-x(-1/\varphi)} = \sum_{n=0}^\infty (x(-1/\varphi))^n.
\end{align*}

Plugging this into our work earlier gives us
\begin{align*}
\frac{x}{1-x-x^2} &= \frac{1}{\sqrt{5}} \cdot \left( \sum_{n=0}^\infty (x\varphi)^n - \sum_{n=0}^\infty (x(-1/\varphi))^n \right) \\
&= \frac{1}{\sqrt{5}} \cdot \left( \sum_{n=0}^\infty (\varphi^n - (-1/\varphi)^n)x^n \right) \\
\end{align*}



So: we found a new way to expand our series!  In particular, because power series are unique, we know that the coefficients of this different way to expand our series must be the same as the coefficients of our original power series $\sum f_nx^n$:
\begin{align*}
\sum_{n=0}^\infty f_nx^n &= \frac{1}{\sqrt{5}} \cdot \left( \sum_{n=0}^\infty (\varphi^n - (-1/\varphi)^n)x^n \right) \\
\Rightarrow f_n &= \frac{\varphi^n - (-1/\varphi)^n}{\sqrt{5}}.
\end{align*}
So we have a closed form for the $f_n$'s.  In other words, it worked!



We now turn to study a second, particularly beautiful example of the method of generating functions:\ \textbf{nonstandard dice}!
\subsection{Nonstandard dice.}

\begin{definition}
Define a $k$-sided die as a $k$-sided shape on which symbols $s_1, \ldots s_k \in \mathbb{N}^+$ are drawn.  Analogously, we can define a $k$-die to be a bucket with $k$ balls in it, each stamped with a symbol $s_i \in \mathbb{N}^+$.  In this sense, ``rolling'' our die corresponds to picking a ball out of our bucket; for intuitive purposes, pick whichever model makes more sense and feel free to use it throughout this lecture.

For our lecture, we restrict all of our symbols to be positive integers: i.e. elements from the set $\{1,2,3,4,\ldots\}$.

A \textbf{standard} k-sided die $D$ is just a $k$-sided die with faces $\{1,2,3\ldots k\}$.  For example, a standard 6-die is just the normal 6-sided dice that you play most board games with.
\end{definition}

The motivating question of this lecture is the following:
\begin{question}
Can you find two $6$-sided dice $B,C$ with the following property: for any $n$, the probability that rolling $B$ and $C$ together and summing them yields $n$ is the same as the probability that rolling two standard 6-sided dice together and summing them yields $n$? 

For example, the probability that $(B+C = 7)$ would have to be $\frac{6}{36}$,  because there are 36 different ways for a pair of two 6-sided dice to be rolled, and there are precisely 6 different ways for a pair of standard 6-sided dice to sum to 7.  Similarly, the probablity for $(B+C= 2)$ would have to be $\frac{1}{36}$, because there's only one way for a pair of standard 6-sided dice to sum to 2.
\end{question}

To answer this, surprisingly, we can use language of generating functions\footnote{Well, you could try brute force and checking all $10^{12}$ possible pairs of dice with faces from $\{1, \ldots 11\}$, but that would make for a very long and boring lecture.}!  To do this, let's use the following method of turning dice into sequences:

\begin{definition}
Given a $k$-sided die $D$, let $d_n$ denote the number of ways in which rolling $D$ yields a $n$.  In this sense, the die $D$ and the sequence $\{d_n\}_{n=1}^\infty$ are equivalent.

For a standard $k$-die $D$, the associated sequence $\{d_n\}_{n=1}^\infty$ is just
\begin{align*}
\underbrace{1,1,1\ldots 1}_\text{k 1's}, 0,0,\ldots
\end{align*}
\end{definition}

\begin{question}
Take two dice $B = \{b_n\}_{n=1}^\infty, C = \{c_n\}_{n=1}^\infty$, and let 
\begin{align*}
d_n = \textrm{the number of ways that rolling }B,C\textrm{ and summing yields } n.
\end{align*}
What is $\{d_n\}_{n=1}^\infty$ in terms of the coefficients $b_n, c_n$?
\end{question}
\noindent\textbf{Answer:} How many ways can rolling $B,C$ and summing give you $n$?  Well: suppose you've already rolled $B$ and gotten a $k$.  Then you need to roll a $n-k$ on $C$ to get a sum of $n$!  In other words,
\begin{align*}
d_n &= \textrm{the number of ways that rolling }B,C\textrm{ and summing yields } n\\
&=\sum_{k=1}^n(\textrm{ways to roll }B\textrm{ and get }k) \cdot (\textrm{ways to roll }C\textrm{ and get }n-k)\\
&=\sum_{k=1}^n b_kc_{n-k}. 
\end{align*}\\


So: let $A = \{a_n\}_{n=1}^\infty =  \{1,1,1,1,1,1,0,0\ldots\}$ be a standard 6-sided die.  In the language of sequences, then, we're trying to find a pair of dice-sequences $\{b_n\}_{n=1}^\infty, \{c_n\}_{n=1}^\infty$ such that for every $n$, we have
\begin{align*}
\sum_{k=1}^n b_kc_{n-k}  = \sum_{k=1}^n a_ka_{n-k}.
\end{align*}

This looks\ldots awful, right?  In other words, we have a problem, and in the language of sequences, it's terrible.  So: let's use the method of generating functions to study these sequences!  After all, they can't get much worse \ldots

\begin{question}
If $A = \{a_n\}_{n=1}^\infty$ is a standard $k$-die, what is the power series $\sum_{n=1}^\infty a_n x^n$ associated to $A$?
\end{question}
\noindent\textbf{Answer:} As mentioned earlier, we have
\begin{align*}
\{a_n\}_{n=1}^\infty = \{\underbrace{1,1,1\ldots 1}_\text{k 1's}, 0,0,\ldots\}.
\end{align*}
Therefore, the associated power series to this sequence is just the polynomial
\begin{align*}
x + x^2  + x^3 + \ldots + x^k.
\end{align*}

Notice that any power series associated to a $k$-sided dice $D$ is just a polynomial, as any $k$-sided dice has only finitely many faces, and therefore finitely many nonzero elements in its associated sequence $\{d_n\}_{n=1}^\infty$.

\begin{question}
Let  $B = \{b_n\}_{n=1}^\infty, C = \{c_n\}_{n=1}^\infty$, be a pair of dice, and let $B(x) = \sum b_n x^n, C(x) = \sum c_nx^n$ be their associated power series.

Let $\{d_n\}$ be the sequence associated to rolling both $B,C$ and summing the result, as discussed before.  What is the power series associated to $\{d_n\}$?
\end{question}
\noindent\textbf{Answer:} If we use our earlier observation about how we can formulate the $d_n$'s in terms of the $b_n, c_n$'s, we have
\begin{align*}
\sum_{n=1}^\infty d_nx^n = \sum^\infty_{n=0} \left( \sum_{k=0}^n b_kc_{n-k} \right) x^n.
\end{align*}
But this is just the product of the two polynomials $B(x), C(x)$!  Specifically, you can check by multiplying terms out via FOIL that
\begin{align*}
\left(  \sum_{n=1}^\infty b_n x^n \right) \cdot \left(  \sum_{n=1}^\infty c_n x^n \right) =  \sum_{n=1}^\infty \left( \sum_{k=0}^n b_kc_{n-k} \right) x^n,
\end{align*}
and therefore that
\begin{align*}
\sum_{n=1}^\infty d_nx^n &= \left(  \sum_{n=1}^\infty b_n x^n \right) \cdot \left(  \sum_{n=1}^\infty c_n x^n \right)\\
&= B(x) \cdot C(x).\\
\end{align*}

In other words, to get the generating function for the sum of two dice, we can simply take the product of their individual generating functions! 

So, in the language of generating functions, our question is now the following:
\begin{quest}
Find a pair of polynomials with integer coefficients $B(x), C(x)$ such that 
\begin{itemize}
\item $B(x), C(x)$ both correspond to $6$-sided dice: i.e. $B(0) = C(0) = 0$ [no 0-faces], $B(1) = \sum b_i = 6, C(1) = \sum c_i = 6$ [they're 6-sided], and all of the coefficients of $B(x), C(x)$ are positive [you can't have a negative number of ways to roll a certain result.]
\item Rolling $B, C$ and summing is equivalent to rolling two standard 6-sided dice and summing: i.e. via our earlier work
\begin{align*}
B(x) \cdot C(x) &= (\textrm{rolling }B,C\textrm{ and summing, interpreted as a polynomial})\\
 &= (\textrm{rolling 2 standard 6-dice and summing, interpreted as a polynomial})\\
&= (x + x^2 + x^3 + x^4 + x^5 + x^6)^2.\\
\end{align*}
\item Neither $B$ or $C$ are standard dice: i.e. neither $B(x)$ or $C(x)$ are equal to $x + x^2 + x^3 + x^4 + x^5 + x^6$.
\end{itemize}
\end{quest}

Now our question is just one about algebra!  I.e. we're just looking for a pair of polynomials whose product is some specific polynomial, whose coefficients are all positive, and that when you plug in 0 yield 0 and when you plug in 1 yield 6.  This is \textbf{doable}!

Specifically: after playing around with the above polynomial, or talking to an algebraicist, you'll realize that
\begin{align*}
(x + x^2 + x^3 + x^4 + x^5 + x^6)^2 &= (x)^2 (x+1)^2 (x^2+x+1)^2 (x^2-x+1)^2.\\
\end{align*}
More specifically, none of the terms $(x), (x+1), (x^2+ x + 1), (x^2 -x + 1)$ can be broken up into smaller polynomials, and there is no way to break up this polynomial into different integer polynomials.  (In this sense, these polynomials $(x), (x+1), (x^2+ x + 1), (x^2 -x + 1)$  are thought of as \textbf{irreducible} polynomials: you cannot break them into smaller parts, and you cannot break anything made of these polynomials into different parts that does not use them.  A good analogy here is to the role of \textbf{prime numbers} in the integers: just like any number can be broken up into a bunch of prime factors, any integer polynomial can be broken up into a bunch of irreducible factors.)


So: the only thing for us to do now is find out if we can split these factors $(x), (x+1), (x^2+ x + 1), (x^2 -x + 1)$  into two polynomials, so that they both correspond to 6-sided nonstandard dice.  

Because $x+1$ is 2 at $x=1$, $x^2+x+1$ is 3 at $x=1$, and $x^2-x+1$ is 1 at $x=1$, we know that each $A_i(x)$ has to have exactly one copy of both $x+1$ and $x^2+x+1$ in it in order for $A_i(1)$ to be 6.  As well, because they both need to be 0 at $x=0$, we need to give each polynomial a copy of $x$.  Consequently, the only way we can have both of these dice not be standard is if
\begin{align*}
B(x) &= x (x+1)(x^2+x+1)(x^2-x+1)^2 = x^8 + x^6+x^5+x^4+x^3+x,\\
C(x) &= x (x+1)(x^2+x+1) = x^4 + 2x^3 + 2x^2 + x;\\
\end{align*}
i.e. we have one die with faces $\{8,6,5,4,3,1\}$ and one die with faces $\{4,3,3,2,2,1\}$.

Check this: they actually work!  For example, there are precisely 6 ways in which rolling these two dice yields 7, just like for a pair of standard 6-sided dice. 


Generating functions can also give us a new way to understand an object we've already studied: the binomial coefficients!
\subsection{Binomial coefficients.}


\begin{question}
Let $f(n,k)$ denote the number of ways of picking $k$ elements out of the set $\{1, 2, \ldots n\}$, where we don't care about order.  What is an explicit form for $f(n,k)$?
\end{question}

\noindent\textbf{Answer}:  So: temporarily, for the purposes of this question, forget that we know that $f(n,k) = \binom{n}{k}$.  Instead: how can we create a generating function for these objects?

Well: first, notice that we have the recurrence relation
\begin{align*}
f(n,k) = f(n-1, k) + f(n-1, k-1).
\end{align*}

Why is this?  Well, pick some way of choosing $k$ elements out of $\{1 ,2, \ldots n\}$.  There are two possibilities: either we picked $n$, or we didn't!  If we did, then ignoring the $n$ gives us a way of picking $k-1$ elements out of a set of $n-1$ objects; if we didn't, then we simply picked $k$ objects out of a set of $n-1$ elements.  Summing over all of the ways of choosing $k$ elements out of $\{1 ,2, \ldots n\}$ then gives us our desired result.

Also: notice that $f(n,0) = 1,$ for all positive $n$, (as there's always exactly one way to not pick anything from a set), that $f(n,k) = 0$ for all negative $n,k$ (as there's no way to pick a negative number of things, or have a set with a negative number of elements,) and that $f(n,k) = 0$ if $k > n$ (as there's no way to pick more than $n$ things out of a set of $n$ elements.)

So: look at the generating function acquired by fixing $n$, 
\begin{align*}
B_n(x) = \sum_{k=0}^\infty f(n,k)x^k.
\end{align*}

Applying our recurrence relation to the above, then, yields
\begin{align*}
B_n(x) &= \sum_{k=0}^\infty f(n,k)x^k \\
& = \sum_{k=0}^\infty (f(n-1, k) + f(n-1, k-1))x^k \\
& = \sum_{k=0}^\infty f(n-1, k)x^k  +  x\sum_{k=0}^\infty f(n-1, k-1)x^{k-1} \\
& = \sum_{k=0}^\infty f(n-1, k)x^k  +  \sum_{k=-1}^\infty f(n-1, k)x^k \qquad \qquad \qquad \text{(b/c }f(-1, k) = 0) \\
& = B_{n-1}(x) + xB_{n-1}(x)  \\
& = (1+x)B_{n-1}(x).
\end{align*}

Then, because $B_0(x) = 1$ (shown via our boundary conditions,) we have via induction that
\begin{align*}
B_n(x) = (1+x)^n.
\end{align*}

Using this, then, we can find $f(n,k)$ by extracting the coefficient of $x^k$ in this power series, as follows:
\begin{itemize}
\item Simply take $k$ derivatives of $B_n(x)$ to kill off all of the terms with degree $< k$.
\item Then, evaluate the resulting power series at 0 to eliminate all of the terms with degree $> k$.
\item Finally, divide by $k!$ to cancel out the constant factor acquired by taking $k$ derivatives of $x^k$.  
\end{itemize}
(It bears noting that this process will work on any power series!  As such, it's a useful trick to have up your sleeve.)

So: doing this to $B_n(x)$ yields the following:
\begin{align*}
\frac{d^k}{dx^k} \left(B_n(x)\right) \Bigg|_0 \cdot \frac{1}{k!} &= \frac{d^k}{dx^k} \left((1+x)^n\right) \Bigg|_0 \cdot \frac{1}{k!} \\
&= (n)(n-1) \cdots (n-k+1) \cdot (1+x)^{n-k} \Bigg|_0 \cdot \frac{1}{k!} \\
&= (n)(n-1) \cdots (n-k+1) \cdot (1) \cdot \frac{1}{k!} \\
&= \frac{(n)(n-1) \cdots (n-k+1)}{k!}\\
&= \frac{n!}{k! \cdot (n-k)!}\\
\end{align*}

We've rederived the binomial coefficient!  Awesome.


We close with one last example problem on generating functions:
\subsection{Coins}

We close with one last example problem on generating functions.  On its surface, this may look like something you could have done (with some tedium) back in elementary school:
\begin{question}
In how many ways can you get to $50\cent$ with some combination of pennies, nickels, dimes, quarters and half-dollars?
\end{question}
\begin{answer}
On one hand, we could simply brute-force the answer to this problem.  This wouldn't be too satisfying, however; it wouldn't really tell us anything about the general problem we're studying (how to decompose some natural number $n$ into a sum of 1's, 5's, 10's, 25's and 50's), nor does it seem very ``efficient'' in terms of the number of calculations we'd have to make.

Instead, let's try applying the one tool we have in this lecture: \textbf{generating functions}!  To make things easier, let's think about all of the ways to make \textbf{any} amount of change, not just $50\cent$, and suppose that we are using only pennies.

While at first glance you might think that the answer here is trivial --- there are infinitely many ways, of course! --- the insight is in how we can represent all of those ways.  Specifically, let's represent $P$, the total number of ways to use pennies to create some amount of change, as follows:
\begin{align*}
P &= \circled{$\emptyset$} + \circled{1} + \circled{1}\circled{1} + \circled{1}\circled{1}\circled{1}+ \circled{1}\circled{1}\circled{1}\circled{1} + \ldots\\
&= \circled{$\emptyset$} + \circled{1} + \circled{1}^2 + \circled{1}^3 + \circled{1}^4 + \ldots,\\
\end{align*}
where $\circled{1}^n$ denotes $n$ pennies\footnote{The reason we use use exponents like $\circled{1}^n$ to denote this, instead of $n\circled{1}$, is because we want to keep our various ways of creating amounts of change separate for different amounts of change.  In other words, if we have one way to make $5\cent$, it does not necessarily follow that we would have 5 ways to make $1\cent$!  Therefore, we would not want to use notation like $5\circled{1}$ to represent the number of ways to make $5\cent$, as this notation would suggest that we can split up this way to make $5\cent$ into other ways of making change --- when in fact, we often cannot do this!  (I.e. it depends on whether that way was by using 5 pennies or one nickel!)}.

If we now let $N$ denote the number of ways of making some amount of change with either nickels or pennies, we can write
\begin{align*}
N &= P + \circled{5}P + \circled{5}\circled{5}P + \circled{5}\circled{5}\circled{5}P+ \circled{5}\circled{5}\circled{5}\circled{5}P + \ldots\\
&= P + \circled{5}P + \circled{5}^2P + \circled{5}^3P + \circled{5}^4P + \ldots.\\
\end{align*}
This is relatively easy to see: if we're using nickels and pennies, we can group any way we have of making change into the number of pennies we use ($ \circled{$\emptyset$} + \circled{1} + \circled{1}^2 + \ldots = P$) and the number of nickels we use ($\circled{$\emptyset$} + \circled{5}+ \circled{5}^2+ \ldots$)

If we let $D$ equal the number of ways of using dimes, nickels, and pennies, $Q$ be the number of ways of using quarters through pennies, and $H$ be the number of ways of using half-dollars through pennies, we can get similar expressions:
\begin{align*}
D &= N + \circled{10}N + \circled{10}^2N + \circled{10}^3N + \circled{10}^4N + \ldots,\\
Q &= D + \circled{25}D + \circled{25}^2D + \circled{25}^3D + \circled{25}^4D + \ldots,\\
H &= Q + \circled{50}Q + \circled{50}^2Q + \circled{50}^3Q + \circled{50}^4Q + \ldots.\\
\end{align*}

Now, consider the following clever trick: take each of our expressions $P,N, D, Q,H$ and turn them into power series in the variable $z$ by replacing each coin $\circled{k}$ with $z^k$.  In other words, set
\begin{align*}
P(z) &= 1 + z + z^2 + z^3 + z^4 + \ldots \\
N(z) &= P(z) \cdot \left(1 + z^5 + z^{10} + z^{15} + z^{20} + \ldots\right)\\
D(z) &= N(z) \cdot \left(1 + z^{10} + z^{20} + z^{30} + z^{40} + \ldots\right)\\
Q(z) &= D(z) \cdot \left(1 + z^{25} + z^{50} + z^{75} + z^{100} + \ldots\right)\\
H(z) &= Q(z) \cdot \left(1 + z^{50} + z^{100} + z^{150} + z^{200} + \ldots\right)\\
\end{align*}

Once this is done: think about the coefficient of $z^n$ in $H(z)$ above, for any $n$.  With some time, you should be able to persuade yourself of the following fact: The coefficient of $z^n$ is precisely the number of ways to make $n\cent$ using pennies, nickels, dimes, quarters, and half-dollars!  This is because we've just replaced each of our coins $\circled{k}$ with $z^k$; therefore any collection of coins that sum to $n\cent$ has been transformed into a $z^n$.

Therefore, if we can just find the coefficient of $z^{50}$ in $H(z)$ above, we've answered our original problem!  If we wanted, we could simply do this out right now by just multiplying our series together; however, this seems difficult (as noted earlier in our notes, the product of two series is hard to calculate!) and not like the most elegant solution.

Instead, let's try to get a cleaner and more general solution.  First, recall the following identity from calculus:
\begin{align*}
\sum_{n=0}^\infty a^nx^n = \frac{1}{1-ax}.
\end{align*}
If you haven't seen this before, well, you weren't paying attention earlier in these notes!  We've used this in our earlier generating functions problems.  However, we never formally justified it as something we can do with formal power series: we fix this here, using only generating function techniques:
\begin{align*}
\sum_{n=0}^\infty a^nx^n &= a^0 x^0 + \sum_{n=1}^\infty a^nx^n= 1 + \sum_{n=0}^\infty a^{n+1}x^{n+1}= 1 + ax \sum_{n=0}^\infty a^nx^n\\
\Rightarrow \quad (1-ax)\sum_{n=0}^\infty a^nx^n &= 1\\
\Rightarrow \quad \sum_{n=0}^\infty a^nx^n &= \frac{1}{1-ax}.\\
\end{align*}

With this established, plugging in $x=z, z^5, z^{10}, z^{25}, z^{50}$ yields the identities
\begin{align*}
&\sum_{n=0}^\infty z^n =  \frac{1}{1-z},\quad \sum_{n=0}^\infty (z^{5})^{n} =  \frac{1}{1-z^5}, \quad \sum_{n=0}^\infty (z^{10})^{n} =  \frac{1}{1-z^{10}}, \\
&\sum_{n=0}^\infty (z^{25})^{n} =  \frac{1}{1-z^{25}}, \quad \sum_{n=0}^\infty (z^{50})^{n} =  \frac{1}{1-z^{50}},\\
\end{align*}
which when applied to our $P(z), N(z), D(z), Q(z)$ and $H(z)$ yields the identities
\begin{align*}
P(z) &= \frac{1}{1-z},\\
N(z) &= P(z)\frac{1}{1-z^5} = \frac{1}{(1-z)(1-z^5)},\\
D(z) &= N(z)\frac{1}{1-z^{10}} = \frac{1}{(1-z)(1-z^5)(1-z^{10})},\\
Q(z) &= D(z)\frac{1}{1-z^{25}} = \frac{1}{(1-z)(1-z^5)(1-z^{10})(1-z^{25})},\\
H(z) &= Q(z)\frac{1}{1-z^{50}} = \frac{1}{(1-z)(1-z^5)(1-z^{10})(1-z^{25})(1-z^{50})}.\\
\end{align*}

Again, from here we could simply expand the denominator of $H(z)$ into the degree-91 polynomial that it is, decompose this into its 91 roots, and use partial fractions to break this up into a ton of fractions, each one of which we could use the $\sum_{n=0}^\infty a^nx^n = \frac{1}{1-ax}$ identity to turn into tractable power series.  Summing all of those up would give us our answer, and if you think that's a good idea go and give it a shot!

In the meantime, the sane people will continue with these notes, and try to find a solution that doesn't involve factoring a degree-91 polynomial to make change.  In particular, there is one clever idea that we can use here that bears special attention!  First, recall that throughout all of these notes, we've repeatedly used proof structures that model the following:
\begin{center}
Given a recurrence relation, we can find a closed form for a generating function!
\end{center}
The situation we're in now, however, is the opposite; we have generating functions, but we don't have a recurrence relation for our change problem!  Accordingly, we're motivated to flip the above method:
\begin{center}
Given a generating function, can we find a recurrence relation for its coefficients?
\end{center}

The answer to this question is yes!  In particular, notice that if we take our earlier expressions for $P(z), N(z), D(z), Q(z)$ and $H(z)$ and multiply through, we get

\begin{align*}
(1-z)P(z) &= 1,\\
(1-z^5)N(z) &= P(z),\\
(1-z^{10})D(z) &= N(z),\\
(1-z^{25})Q(z) &= D(z),\\
(1-z^{50})H(z) &= Q(z).\\
\end{align*}

For notation's sake, let $P_n, N_n, D_n, Q_n, H_n$ denote the coefficients of $P(z), N(z), D(z), Q(z)$ and $H(z)$ above.  Then, for $P(z)$, we specifically have
\begin{align*}
(1-z)P(z) = (1-z) \sum_{n=0}^\infty P_n z^n &=  1\\
\Rightarrow \sum_{n=0}^\infty P_n z^n - z\sum_{n=0}^\infty P_n z^n &= 1\\
\Rightarrow P_0 + \sum_{n=1}^\infty P_n z^n - \sum_{n=0}^\infty P_n z^{n+1} &= 1\\
\Rightarrow \sum_{n=1}^\infty (P_{n} - P_{n-1})z^n = 1 - P_0 &= 0,\\
\end{align*}
because $P_0 = 0 =$ the number of ways to use zero pennies. However: we know that two formal power series are equal if and only if their coefficients are equal!  Therefore, the coefficients of the left-hand-side --- the expression $(P_{n} - P_{n-1})$ --- is equal to the coefficients of the right hand side, which are all zero!  In other words, we have
\begin{align*}
P_0 = 1, P_{n} = P_{n-1}.
\end{align*}
A recurrence relation!  A really boring one; it in fact just says that $P_n = 1$ for every $n$.  But this is what we would expect, because $P(z) = 1 + z + z^2 + z^3 + \ldots$ is indeed the power series where all of its coefficients are 1!

More usefully, we can use the exact same logic above to deduce recurrence relations for $N(z), D(z), Q(z)$ and $H(z)$:
\begin{align*}
(1-z^5)N(z) &= P(z)\\
\Rightarrow \sum_{n=0}^\infty N_n z^n - z^5\sum_{n=0}^\infty N_n z^n &= \sum_{n=0}^\infty P_n z^n\\
\Rightarrow N_0 + N_1 z + N_2 z^2 + N_3 z^3 + N_4z^4 + \sum_{n=5}^\infty N_n z^n - \sum_{n=0}^\infty N_n z^{n+5} &=  \sum_{n=0}^\infty P_n z^n\\
\Rightarrow N_0 + N_1 z + N_2 z^2 + N_3 z^3 + N_4z^4 + \sum_{n=5}^\infty (N_{n} - N_{n-5})z^n &=\sum_{n=0}^\infty P_n z^n.\\
\end{align*}
By equating the left and right-hand sides, we have the recurrence relation
\begin{align*}
N_0 = P_0, N_1 = P_1, N_2 = P_2, N_3 = P_3, N_4 = P_4; \\N_n = N_{n-5} + P_n, \forall n \geq 5.
\end{align*}
Using similar methods to the above two derivations gives us
\begin{align*}
D_0 = N_0, \ldots D_9 = N_9,\quad &D_n = D_{n-10} + N_n, \forall n \geq 10,\\
Q_0 = D_0, \ldots Q_{24} = D_{24}, \quad&Q_n = Q_{n-25} + D_n, \forall n \geq 25,\\
H_0 = Q_0, \ldots H_{49} = Q_{49}, \quad&H_n = H_{n-50} + Q_n, \forall n \geq 50.\\
\end{align*}
Recurrence relations!  We can use these to populate a table of values for our coefficients, by simply applying the relations above to the ``base-case'' values $P_n =1, \forall n$:
\begin{align*}
\begin{array}{c|ccccccccccc}
n & 0 & 5 & 10 & 15 & 20 & 25 & 30 & 35 & 40 & 45 & 50\\\hline
P_n & 1 & 1 & 1 & 1 &1 & 1& 1 & 1 & 1 & 1 &1\\
N_n & 1 & 2 & 3 & 4 &5 & 6& 7 & 8 & 9 & 10 &11\\
D_n & 1 & 2 & 4 & 6 &9 & 12& 16 & 20 & 25 & 30 &36\\
Q_n & 1 & 2 & 4 & 6 &9 & 13& 18 & 24 & 31 & 39 &49\\
H_n & 1 & 2 & 4 & 6 &9 & 13& 18 & 24 & 31 & 39 &\boxed{50}\\
\end{array}
\end{align*}
So there are exactly fifty ways to make $50\cent$ using our change!  That's remarkably nice, actually.
\end{answer}


The methods above generalize fairly beautiful to answer the following problem about \textbf{partitions}:
\begin{defn}
Take any natural number $n$.  A \textbf{partition} of $n$ is any way to write $n$ as a sum of smaller natural numbers, where we do not care about the order in which we write our sum.  For example, 5 can be partitioned in six different ways:
\begin{align*}
1+1+1+1+1, 1+1+1+2, 1+1+3, 1+2+2, 2+3, 1+4, 5
\end{align*}
We let $p_n$ denote the number of partitions of $n$.  
\end{defn}
\begin{thm}
The generating function for the sequence $\{p_n\}_{n=0}^\infty$ is
\begin{align*}
P(x) = \prod_{n=1}^\infty \frac{1}{1-x^n}.
\end{align*}
\end{thm}
\begin{proof}
We use the exact same ideas as above.  Let $p_n^{(i)}$ denote the number of partitions of $n$ that use only natural numbers that are at most $i$.  For example, $p_n^{(1)}$ is 1 for every $n$, as there is exactly one partition of any $n$ if we only can use 1's.  

Then, if we let $P^{(m)}(x)$ denote the generating function for the sequence $\{p_n^{(m)}\}_{i=0}^\infty,$ we have that

\begin{align*}
P^{(1)}(x) = \sum_{n=0}^\infty p_n^{(1)}x^n = \sum_{n=0}^\infty 1\cdot x^n = \frac{1}{1-x}.
\end{align*}

I claim that in general, 
\begin{align*}
P^{(m)}(x) = \prod_{n=1}^m \frac{1}{1-x^n},
\end{align*}
and prove it by induction.  We have already established our base case above; so let's move to the inductive step.  Assume that 
\begin{align*}
P^{(m)}(x) = \prod_{n=1}^m \frac{1}{1-x^n};
\end{align*}
we seek to prove that this relation holds for $m+1$ as well.  To do this, we do the same trick we did for coins: think of all of the ways to make natural numbers using unordered sums of natural numbers no greater than $m+1$.  If we let $M$ denote the collection of all of these ways that use only numbers up to $m$, we can group these ways by the number of ``m+1'' elements we use as follows:
\begin{align*}
M + \circled{m+1}M + \circled{m+1}\circled{m+1}M + \circled{m+1}\circled{m+1}\circled{M+1}M+\ldots
\end{align*}
As before, if we replace each $\circled{k}$ with $x^k$, we replace each way to make $n$ with $x^n$; therefore, the coefficients of the resulting power series are precisely the coefficients of $P^{(m+1)}(x)$, the series we want to study!

In particular, if we do this here, we would replace $M$ with $P^{(m)}(x)$ by definition, and get
\begin{align*}
P^{(m+1)}(x) &= P^{(m)}(x) + x^{m+1}P^{(m)}(x) + x^{2m+2}P^{(m)}(x) + \ldots\\
&= P^{(m)}(x)(1+x^{m+1} + x^{2m+2} + \ldots)\\
&= P^{(m)}(x) \cdot \frac{1}{1-x^{m+1}}\\
&=  \prod_{n=1}^{m+1} \frac{1}{1-x^n}.
\end{align*}

Our claim was about $\{p_n\}_{n=0}^\infty$, however; i.e. the sequence of partitions where we do not restrict the sizes of our terms.  But this is simply the limit of the $P^{(m+1)}(x)$'s as $m$ goes to infinity, as letting $m$ go to infinity is just letting our terms be arbitrarily large!  Therefore, we have
\begin{align*}
P(x) = \prod_{n=1}^\infty \frac{1}{1-x^n}.
\end{align*}
\end{proof}

\section{Sieves and Generating Functions}
In the above four subsections, we studied many individual examples of generating functions and how they work; along the way, we saw several useful techniques (partial fractions, using recurrence relations to get generating functions, using generating functions to recurrence relations, when plugging in values to a generating function is a useful idea, how derivatives work with generating functions.)  

In this section, we reverse things a bit to a more top-down approach: rather than introduce an idea with an example, we start with some abstraction first (and then make it concrete with an example!)


\begin{concept}
Consider the following set of general ``things:''
\begin{itemize}
\item $\Omega$, a finite set of objects.  
\item $P$, a collection of properties.
\end{itemize}
\end{concept}

To give a concrete example, you could have
\begin{itemize}
\item $\Omega = \{1,2,3,4\}$, and
\item $P = \{$odd, prime$\}$, 
\end{itemize}
In general, this is a sort of situation we will often find ourself in:\ we will have a set of objects and they will have properties.  In this setting, a very common combinatorial question to ask is the following:
\begin{quest}
For a given subset of our properties $P$, how many of our elements satisfy precisely those properties and no others?
\end{quest}

In our example earlier, we can simply answer this question via brute-force; for instance, there are two odd elements, two prime elements, one odd prime element, and one element that is neither odd nor prime.  In general, however, this can be a difficult question to answer directly; it is often hard to say \textbf{exactly} how many elements have $r$ properties.  Instead, it's often easier to count how many objects have at \textbf{least} $r$ properties!  To give an example, consider the following problem:

\begin{exmp}
A \textbf{permutation} of $\{1,2,\ldots n\}$ is any bijection $\pi: \{1,2,\ldots n\} \to \{1,2,\ldots n\}$ from the set $\{1, \ldots , n\}$ to itself.  A \textbf{fixed point} of a permutation $\pi$ is any element $k \in \{1,\ldots n\}$ such that$\pi(k) = k$.

Take any subset $S$ of $\{ 1,2\ldots n\}$.  How many permutations fix every element of $S$ but no others?  In particular, what happens when $S = \emptyset$?
\end{exmp}

Trying to answer this directly seems\ldots hard (if you don't believe me, try it!)  Conversely, suppose that we had the following different question:
\begin{exmp}
Take any subset $S$ of $\{ 1,2\ldots n\}$.  How many permutations fix at least all of the elements of $S$?
\end{exmp}
\begin{answer}
This is pretty straightforward!  By definition, any such permutation $\pi$ must have $\pi(s) = s$ for every $s \in S$.  This leaves us with the remaining $n-|S|$ elements to make decisions on, and we can do whatever we want to these remaining elements as long as we stay a bijection.  Therefore, we have a total of $(n-|S|)!$ many such permutations (as we have $n-|S|$ many choices for where to send the first non-$S$ element, $n-|S| - 1$ choices for where to send the second, and so on.)
\end{answer}

Can we use this to our advantage?  In other words, can we turn knowledge about the amount of objects possessing at least $r$ properties into knowledge about objects possessing exactly $r$ properties? 

The answer is yes, and the method should be unsurprising: we use generating functions!
\begin{concept}
Take any pair $\Omega, P$, where $\Omega$ denotes some set of objects and $P$ a set of properties.  For any subset $S$ of $\Omega$, let $A(S)$ be the number of elements in $\Omega$ that satisfy all of the properties in $S$ (and possibly others too, but at least these properties.)   Let $n_r$ denote the following sum:
\begin{align*}
n_r = \sum_{S \subseteq P: |S| = r} A(S).
\end{align*}

Given any $x \in \Omega$, let $f(x)$ denote the subset of $P$ corresponding to the collection of properties that $x$ satisfies.  Then, we have that
\begin{align*}
n_r & = \sum_{S \subseteq P: |S| = r} A(S) \\
& = \sum_{S \subseteq P: |S| = r} \left(\sum_{x \in \Omega: S \subseteq f(x)} 1\right) \\
& = \sum_{x \in \Omega} \left( \sum_{S \subseteq P: |S| = r, S \subseteq f(x)}1 \right) \\
& = \sum_{x \in \Omega} \binom{|f(x)|}{r}. \\
\end{align*}
The clever trick we did above here is ``switching the order of summation:'' that is, instead of first summing over all of the subsets $S$ of $P$ and then summing over the elements of $\Omega$, we are instead grouping things by elements in $\Omega$ and then summing over subsets $S$ of $P$.  This sort of trick is very useful/will come up lots!

If we look at what we've proven above, we can see that every element of $\Omega$ with exactly $t$ properties contributes $\binom{t}{r}$ to the quantity $n_r$.  Let $e_t$ denote the number of elements in $\Omega$ with exactly $t$ properties.  Then, I claim that 
\begin{align*}
n_r = \sum_{t=0}^\infty \binom{t}{r} e_t.
\end{align*}

To see why this is true, consider any element $x$ of $\Omega$ with $t$ properties $\{p_1, \ldots p_r\}$.  There are precisely $\binom{t}{r}$ subsets of these properties that $x$ satisfies, and for each such subset we counted $x$ once in the earlier sum for $n_r$!  This justifies the claim above.

Given this claim, let $N(x)$ be the generating function for the $n_r$'s, and $E(x)$ be the generating function for the $e_t$'s.  Then, if we stick our results from earlier together and again switch the order of summation, we get the following surprising identity:
\begin{align*}
N(x) &= \sum_{r=0}^\infty n_r x^r\\
&= \sum_{r=0}^\infty \left(\sum_{t=0}^\infty \binom{t}{r} e_t x^r\right)\\
&= \sum_{t=0}^\infty e_t \cdot \left(\sum_{r=0}^\infty \binom{t}{r} x^r \right)\\
&= \sum_{t=0}^\infty e_t (1+x)^t\\
& = E(x+1).
\end{align*}
\end{concept}
In other words: with the tools given to us by generating functions, we can convert back and forth between ``exact'' counting and ``at-least'' counting with absolutely no effort!  

Specifically, this method -- the method of ``sieves''  -- gives us the following remarkably useful trick:
\begin{obs}
 Because $E(x) = N(x-1)$, we have that $e_t$ is just the coefficient of $x^t$ in $N(x-1)$.  To be specific: for any power series $G(x)$, let $[x^t]G(x)$ denote the coefficient of $[x^t]$ in $G(x)$.  Then, we have
\begin{align*}
e_t & = [x^t] \sum^\infty_{r=0} n_r(x-1)^r\\
&= \sum^\infty_{r=0} n_r \cdot [x^t](x-1)^r\\
&= \sum^\infty_{r=0} (-1)^{r-t}\binom{r}{t} n_r.\\
\end{align*}
So:\ if you know the $n_r$'s, you can use them to determine the $e_t$'s!  That's nice.  
\end{obs}
\begin{comment}
\item By our earlier work,
\begin{align*}
n_r & = \sum_{t=0}^\infty \binom{t}{r} e_t\\
\Rightarrow \qquad n_1 & = \sum_{t=0}^\infty t\cdot e_t\\
\Rightarrow \qquad \frac{n_1}{|\Omega|} &= \frac{1}{|\Omega|}\sum_{t=0}^\infty t\cdot e_t.\\
\end{align*}

But what is the right-hand side above?  Well: it's the sum over all sizes $t$ of the number of elements of $\Omega$ with exactly $t$ properties, divided by the size of $\Omega$ itself.  In other words, it's the \textbf{average number} of properties held by any element of $\Omega$!  (Think about this for a while if you don't see why this is true.)  So we have a nice way to calculate average numbers of properties held by objects!
\end{comment}
To illustrate the power of what we've just done, we return to our earlier example:

\begin{quest}
Of the $n!$ permutations of $\{1, \ldots , n\}$, how many have no fixed points?  
\end{quest}
\begin{solution}
So: in our language of sets and properties, let
\begin{itemize}
\item $\Omega = $ the collection of all permutations on $\{1,2,\ldots n\}$, and
\item $P = \{P_1, \ldots P_n\}$, where $P_i$ is the property that $i$ is a fixed point of our permutation.
\end{itemize}
As discussed earlier, for any $S \subset P$, the number of permutations $A(S)$ satisfying $S$ is just $(n-|S|)!$.

Consequently, we have that if $r \leq n$, that
\begin{align*}
n_r = \sum_{|S| = r} A(S) = \sum_{|S| = r} (n-|S|)! = \binom{n}{r} (n-r)! = \frac{n!}{r!}.
\end{align*}
Trivially, we have that $r_n = 0$ for $r > n$, as it is impossible for a permutation on $n$ points to have more than $n$ fixed points. 

If we look at the power series $N(x)$ for the $n_r$'s, we get
\begin{align*}
&  N(x)  = \sum_{r=0}^n \frac{n!}{r!} x^r = n! \cdot \sum_{r=0}^n \frac{x^r}{r!}\\
\Rightarrow \qquad& E(x)  = N(x-1) = n! \cdot \sum_{r=0}^n \frac{(x-1)^r}{r!}\\
\Rightarrow \qquad&  e_0 = E(0) = N(-1) = n! \cdot \sum_{r=0}^n \frac{(-1)^r}{r!}\\
\Rightarrow \qquad& e_0 \approx \frac{n!}{e},\\
\end{align*}
where that last step is justified by remembering that $e^x$'s Taylor series is $\displaystyle \sum_{r=0}^\infty \frac{x^r}{r!}$, and therefore that for any decently-sized value of $n$ the sum $\displaystyle\sum_{r=0}^n \frac{(-1)^r}{r!}$ is approximately $e^{-1}$.

Consequently, we have proven that the number of permutations with no fixed points is approximately $\frac{n!}{e}$.  Cool!
\end{solution}


\end{document}