6

This question involves both math and programming, but the main inquiry in nature is mathematical.

There is a LeetCode question called Unique Paths that asks the number of ways that one can reach the lower-right corner of a $m \times n$ grid if you start in the upper-left corner and want to reach the lower-right corner, only being able to move down or right at any step.

Visualization of the $3 \times 7$ case, for example:

LeetCode unique paths 3x7 example

One mathematical approach that was suggested is to compute $\begin{pmatrix}m + n - 2 \\ m - 1\end{pmatrix}$ as the question is equivalent to asking the number of ways to select $m - 1$ downwards moves from a total of $m + n - 2$ moves to reach the lower-right corner.

However, the programming solution that was proposed essentially computes the following:

$$ \left\lfloor \frac{\left\lfloor \frac{\left\lfloor \frac{\left\lfloor \frac{1 \cdot n}{1} \right\rfloor (n + 1)}{2} \right\rfloor (n + 2)}{3} \right\rfloor \cdots (n + m - 2)}{\vdots \atop m - 1} \right\rfloor $$

So, my question is how does the solution guarantee that the above is equivalent to $\left\lfloor \frac{(m + n - 2)!}{(m - 1)!(n - 1)!} \right\rfloor$ without any loss of data from the repeated application of the floor operator?

Edit:

The accepted answer from Sil helped me out the most intuitively. Though, the inductive proof of how each step results in a combination by Mike also helped very much.

2 Answers2

5

The division is applied in cases where denominator already divides numerator, so there is no remainder. This is because $\binom{n+k-1}{k}=\frac{n(n+1)\cdots(n+k-1)}{1\cdot 2\cdots k}$ is an integer for all $k=1,2,\dots$ (see for example Division of Factorials [binomal coefficients are integers] or Proof that a Combination is an integer for some proofs of this fact). Hence the result of the division is an integer and there is nothing to round (floor function).

Using your notation, first it's clear that $1$ divides $n$ and so $\frac{1 \cdot n}{1}=n$ and $\lfloor n \rfloor=n$. Then $n(n+1)$ is a multiple of $2$ (one of two consecutive integers will be even), so $\frac{n(n+1)}{2}$ is an integer and so $\lfloor \frac{n(n+1)}{2} \rfloor= \frac{n(n+1)}{2}$. And so on... In $k$-th step you take the above binomial coefficient about which we already know is an integer, and so $1\cdot 2\cdots k$ divides $n(n+1)\cdots(n+k-1)$, hence $k$ divides $\frac{n(n+1)\cdots(n+k-1)}{1\cdot 2\cdots (k-1)}$ which we have from the previous step.

From more programming point of view, the step you refer to I believe is ans = ans * x / y;, which in C is evaluated as ans = (ans * x) / y;. The division operator here is an integer division so in principle it could also perform some rounding, but as explained above, ans * x is already a multiple of y so this is not an issue here.

Sil
  • 16,612
  • 1
    I now get the intuition. Thank you for that! However, the "And so on..." part confuses me. I see how you get the mod 1 and mod 2 cases, but I find it hard to extrapolate from that point. – Gigi Bayte 2 Nov 02 '22 at 16:30
  • 2
    You continue by picking the next binomial coefficient, for $k=3$ we have $\binom{n+2}{3}$ is an integer (I assume you know this fact, it can be proven on its own if you don't), but $\binom{n+2}{3}=\frac{n(n+1)(n+2)}{1\cdot 2\cdot 3}$, so we know $2\cdot 3$ divides $n(n+1)(n+2)$, and so $3$ divides $\frac{n(n+1)(n+2)}{2}$. By same process you know $k$ divides $\frac{n(n+1)\cdots(n+k-1)}{1\cdot 2\cdots (k-1)}$, which you have from previous step. – Sil Nov 02 '22 at 16:37
3

Let us give names to the intermediate values of this calculation. Let $x_0=1$, and for each $i\in \{1,2,\dots,m-1\}$, let $$ x_{i+1}=\left\lfloor\frac{x_i\cdot (n+i)}{i+1}\right\rfloor $$ We will prove by induction on $i$ that $$x_i=\binom{n+i-1}{i},\qquad i\in \{0,1,\dots,m-1\}$$ In particular, the last value is $x_{m-1}=\binom{m+n-2}{m-1}$, so this would prove the algorithm is computing the correct value.

For the base case, we must prove $x_0=\binom{n-1}{0}$. Since $\binom{n-1}{0}=1$, this matches the definition of $x_0$.

For the inductive step, given $i\ge 0$, we assume $x_i=\binom{n+i-1}{i}$, and must prove $x_{i+1}=\binom{n+i}{i+1}$. \begin{align} x_{i+1} &= \left\lfloor\frac{x_i\cdot (n+i)}{i+1}\right\rfloor \\&= \left\lfloor\binom{n+i-1}{i}\cdot \frac{n+i}{i+1}\right\rfloor \tag{Inductive hypothesis} \\&= \left\lfloor\binom{n+i}{i+1}\right\rfloor \tag{Absorption identity} \\&= \binom{n+i}{i+1} \tag{Floor of integer is self} \end{align} The absorption identity I am using is $$ \binom{t}{k}=\binom{t-1}{k-1}\frac{t}{k},\qquad k\ge 1 $$ which is easy to prove by converting everything to factorials, i.e. using $\binom tk=t!/[k!\cdot (t-k)!]$.

Mike Earnest
  • 75,930
  • 1
    That clears it up quite a bit! Every step is a combination itself as you have mentioned, which makes it an integer. Thank you! Then, the only quandary that this leaves me with is the intuition for why the product from i = 0 to k of (x+i)/(i + 1) ≡ 0 (mod k + 1). I'll probably ask that as a separate question later. – Gigi Bayte 2 Nov 02 '22 at 16:34
  • 1
    @GigiBayte2 To your comment, see the answers to the product of $n$ consecutive numbers is divisible by $n!$. The second answer gives a proof by induction, while the third answer gives a proof by counting prime factors. (Also, here is my version of the prime-counting proof: https://math.stackexchange.com/a/2869484/177399). – Mike Earnest Nov 02 '22 at 20:58