2-counting
==========

Example:
    If we draw 37 circles in the plane, such that every two circles intersect
    in two points and such that no three circles intersect in the same point
    ("mutually overlapping circles in general position"), how many regions in
    the plane do we get (counting the region outside all the circles as one of
    the regions)?
Solution:
    Let h_n be the number of regions with n circles.
    So h_1 = 2,
    and for n > 1: when we add the nth circle, it intersects the existing n-1
    circles in 2(n-1) points; so the new circle is divided into 2(n-1) arcs;
    each arc divides a pre-existing region into two. So we get 2(n-1) extra
    regions compared to how many we had before, i.e. compared to h_{n-1}. So
	h_n = h_{n-1} + 2(n-1).

    Applying this recursively, we have
	h_n = h_{n-1} + 2(n-1)
	    = h_{n-2} + 2(n-2) + 2(n-1)
	    = h_{n-3} + 2(n-3) + 2(n-2) + 2(n-1)
	    = ...
	    = h_1 + 2(1) + 2(2) + ... + 2(n-1)
	    = 2 + 2(1+2+...+n-1)
	    = 2 + 2(n(n-1)/2)
	    = 2 + n(n-1)
	    = n^2 - n + 2
    
    (We can check this satisfies the recurrence relation:
	((n-1)^2 - (n-1) + 2) + 2(n-1)
	    = n^2 - 2n + 1 - n - 1 + 2 + 2n - 1
	    = n^2 - n + 2,
    as required.)

    So h_{37} = 37^2 - 37 + 2 = 1334.


Four principles of counting
===========================

We find ways to use descriptions of a finite set to determine its size in
terms of the sizes of the sets appearing in the description.

Addition principle
------------------

Suppose a set S is partitioned by subsets S_1,...,S_n: this means
that the sets cover S, i.e.
    S = S_1 \cup ... \cup S_n,
and are disjoint, i.e.
    S_i \cap S_j = \emptyset
	if i != j.

So S is the disjoint union of the S_i.

Then |S| = |S_1| + ... + |S_n|.

Example:
    In a nim game with three piles of sizes 3, 7, and 27,
    how many possible first moves are there?
Solution:
    The first move has to be in one of the three piles, so the answer is the
    sum of the numbers of moves available when the move is in each of the
    three piles. The number of possible moves from a pile with n coins is n.
    So the answer is 3 + 7 + 27 = 37.

Multiplication principle
------------------------

Let S be a set of ordered pairs (a,b) where the first is picked from a set of
size p, and where for each such a there are q possible choices for b. Then
    |S| = pq

Example:
    How many two-digit numbers have different digits?
Solution:
    Think of a two-digit number as an ordered pair, e.g. 37 <-> (3,7).
    Then the set of two-digit numbers with different digits corresponds to the
    set of ordered pairs of numbers where the first number is from {1,...,9},
    and the second number is then one of the 9 digits which is not equal to
    the first digit. So there are 9*9 = 81 possibilities.

Proof of the multiplication principle:
    Partition S according to the first entry of the pair. Then there are p
    sets each of size q, so by the addition principle
	|S| = q + ... + q  (p times)
	    = pq

Of course it generalises to ordered triples and so on:

Example:
    How many three digit numbers have no repeated digits?
Solution:
    9 choices for the first digit, then 9 for the second, then 8 for the
    third; so 9*9*8 = 648 possibilities.

Example:
    How many factors does 12600 have?
Solution:
    Find prime factorisation:
	12600 = 2^3 * 3^2 * 5^2 * 7
    So any factor has prime factorisation
	2^a * 3^b * 5^c * 7^d
    with 0 <= a <= 3, 0 <= b <= 2, 0 <= c <= 2, 0 <= d <= 1.

    By uniqueness of prime factorisation, each choice of (a,b,c,d) within
    these bounds yields a unique factor of 12600.

    So the number of factors is the number of such (a,b,c,d), which is
	4 * 3 * 3 * 2 = 72

Example:
    How many two digit numbers have the sum of their digits odd, and don't end
    with 7?
Solution:
    We need one digit even and the other odd; the first digit can't be 0, the
    second can't be 7.

    If we pick the first digit first, the number of possibilities for the
    second digit will depend on the parity of the first digit.

    There are 5 possibilities for an odd first digit. The second then has to
    be even: 5 possibilities.

    With an even first digit, we have 4 possibilities for the first, and 4 for
    the second.

    So the answer is 5*5 + 4*4 = 41.
    
Subtraction principle
---------------------

If S is a subset of U, then
    |S| = |U| - |U \\ S|.

Proof: S and U \\ S partition U, so |U| = |S| + |U \\ S|.

Example:
    If we toss a coin 10 times, we get a sequence of Heads and Tails,
    e.g. HTTHHTTHTH.
    How many such sequences contain at least two Heads?
Solution:
    The number of such sequences which *don't* contain at least two Heads is
    much easier to work out; it's 11: 1 for TTTTTTTTTT, and 10 for each of the
    ten positions a single Head could be.

    The total number of sequences is, by the multiplication principle,
	2 * 2 * ... * 2 (10 times)
	= 2^10 = 1024.

    So the answer is
	1024 - 11 = 1013.


Division principle
------------------

If S is partitioned into k sets each with n elements, then
    k = |S|/n.
(Proof: by the multiplication principle, |S| = k*n.)

Example:
    740 pigeons are nesting in some pigeonholes. If there are 5 pigeons in
    each pigeonhole, how many pigeonholes are there?
Solution:
    740/5 = 148.


Finite probability
==================

If we toss a coin 10 times, what's the probability of getting at least two
Heads? Answer: there are 2^10 = 1024 possible outcomes (sequences of Heads and
Tails), each equally likely. 1013 of them end up with at least two heads, so
the probability is 1013/1024.

General setup:
    we have a _sample space_, a finite set S of possible outcomes, each
    equally likely to occur. Then an _event_ is a subset E of S, and we
    define the probability that E occurs to be
	Prob(E) := |E| / |S|

Example: Suppose someone offers to play the following game with you: three
    6-sided dice will be rolled, and you'll set up a nim game with three
    piles, the sizes of the piles being given by the dice roll. You will play
    first. Assuming you both know the winning strategy for nim, what are your
    chances of winning?
Solution:
    The number of possible dice rolls is 6^3 = 216.

    So we want to determine the number of triples (a,b,c) of numbers between 1
    and 6 such that the nim sum is zero,
	a (+) b (+) c = 0.

    Nim-adding c to both sides, this is equivalent to
	a (+) b = c.

    So once a and b are determined, there's at most one choice for c. So we
    want to see for how many (a,b),
	1 <= a (+) b <= 6.

    This can only fail if, in binary,
	a (+) b = 000 or 111
    i.e. if a = b or a+b=7.
    So this precisely rules out two choices for b for each choice of a.

    So there are 6*4 = 24 bad rolls for us, so the probability we win is
	216 - 24 / 216 = 192 / 216 = 8/9.