Problem. Candidate A gets votes and B gets votes, counted in random order. What is the probability that A is strictly ahead throughout the count?
Answer: (Bertrand). The number of such orderings is .
long long ballotCount(int a, int b) { // orderings of a A-votes and b B-votes (a > b) with A strictly ahead throughout
long long c = 1; for (int i = 1; i <= b; i++) c = c * (a + i) / i; // C(a + b, b)
return c * (a - b) / (a + b);
}Proof: the reflection principle
Count lattice paths with up-steps and down-steps that stay strictly above after the first step. Such a path must start with an up-step. Bad paths starting with an up-step touch at some point; reflecting the part before the first touch gives a bijection with all paths starting with a down-step, of which there are . Good paths: . Divide by for the probability. Checked by enumerating all orderings for . With and “ahead or tied” this gives the Catalan numbers (Catalan and Lattice Paths).