4

Let $n,k,r$ be positive integers. The number of all nonnegative solutions of the Diophantine Equation $x_1+x_2+\dots+x_k=n$ is $\binom{n+k-1}{n}$. Is there a general formula for the number of solutions of the equation $x_1+x_2+\dots+x_k=n$ with $x_i\le r$ for every $i\in \{1,2,\dots,k\}$?

If one defines $A_i$ to be the number of solutions with $x_i>r$ then the answer will be $\binom{n+k-1}{n}-|A_1\cup\dots\cup A_k|$. I think it can give a complicated formula. What is the formula?

joriki
  • 238,052
  • 1
    You're probably wanting the inclusion-exclusion principle: https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle – Yiyuan Lee Sep 10 '15 at 14:41

2 Answers2

9

This sort of problem can be solved using inclusion-exclusion. For your problem, this leads to

$$ \sum_{t=0}^k(-1)^t\binom kt\binom{n-t(r+1)+k-1}{k-1}\;, $$

where, contrary to convention, the binomial coefficient stands for $0$ if the upper index is less than the lower index, and where $t$ counts the number of variables for which the constraint is violated.

For a derivation, including the case of different constraints $x_i\le r_i$, see Balls in Bins with Limited Capacity.

joriki
  • 238,052
  • Are you sure the lower bound is $0$? Why do you say contrary to convention? Wolframalpha agrees: https://www.wolframalpha.com/input/?i=Binomial%5B6%2C10%5D – Minimus Heximus Sep 11 '15 at 04:50
  • @user795571: That's for positive upper index. For negative upper index (which occurs here if $r$ is large enough), the binomial coefficient is conventionally defined to be non-zero; e.g. https://www.wolframalpha.com/input/?i=Binomial[-6%2C10]. I'm not sure I understand your question "Are you sure the lower bound is $0$?". I'm sure the binomial coefficients have to be interpreted as $0$ here when the upper index is less than the lower; this counts cases that can't actually occur. You could set the upper limit of the sum to some ugly complicated expression using the floor function instead. – joriki Sep 11 '15 at 06:35
  • I meant the lower bound of the sum. Because $\binom{k}{0}$ seems strange to me. – Minimus Heximus Sep 11 '15 at 06:46
  • 1
    @user795571: $\binom k0$ is perfectly fine. It's the number of ways of choosing $0$ items from $k$, so $\binom k0=1$. This is in fact the "main" term that counts the number of possibilities without restriction, and the remaining terms enforce the restrictions by inclusion-exclusion. – joriki Sep 11 '15 at 06:50
  • @joriki you can avoid the problem with $r$ negative, by taking the symmetric, i.e. writing ${{n+k-1+t(r+1)} \choose {n+t(r+1)}}$ ! See also this post – G Cab Aug 01 '17 at 21:49
4

$\newcommand{\bbx}[1]{\,\bbox[8px,border:1px groove navy]{\displaystyle{#1}}\,} \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace} \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack} \newcommand{\dd}{\mathrm{d}} \newcommand{\ds}[1]{\displaystyle{#1}} \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,} \newcommand{\ic}{\mathrm{i}} \newcommand{\mc}[1]{\mathcal{#1}} \newcommand{\mrm}[1]{\mathrm{#1}} \newcommand{\pars}[1]{\left(\,{#1}\,\right)} \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}} \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,} \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}} \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

Is there a general formula for the number of solutions of the equation $\ds{x_{1} + \cdots + x_{k} =n}$ with $\ds{0 \leq x_{i} \leq r\quad}$ for every $\ds{\quad i \in \braces{1,\ldots,k}}$ ?.


  • The solution is given by $\ds{\mc{N} \equiv \sum_{x_{\large 1} = 0}^{r}\ldots\sum_{x_{\large k} = 0}^{r}\bracks{z^{n}} z^{x_{\large 1} + \cdots + x_{\large k}}}$.
  • $\ds{\bracks{z^{n}}\mrm{f}\pars{z}}$ denotes the coefficient of $\ds{z^{n}}$ in the $\ds{\mrm{f}\pars{z}}$ power expansion.
  • With the above definition, $\ds{\bracks{z^{n}} z^{x_{\large 1} + \cdots + x_{\large k}}}$ is equal to $\ds{\color{#f00}{1}}$ whenever $\ds{x_{1} + \cdots + x_{k} = n}$ and $\ds{\color{#f00}{0}}$ otherwise. Indeed, it's equivalente to the Kronecker Delta $\ds{\delta_{n,x_{\large1} + \cdots + x_{\large k}}}$.
  • Then, each term adds $\ds{\color{#f00}{1}}$ to the sum whenever the condition $\ds{x_{1} + \cdots + x_{k} = n}$ is satisfied.


Therefore, \begin{align} \mc{N} & \equiv \sum_{x_{\large 1} = 0}^{r}\ldots \sum_{x_{\large k} = 0}^{r}\bracks{z^{n}} z^{x_{\large 1} + \cdots + x_{\large k}} = \bracks{z^{n}}\pars{\sum_{x = 0}^{r}z^{x}}^{k} = \bracks{z^{n}}\pars{z^{r + 1} - 1 \over z - 1}^{k} = \bracks{z^{n}}{\pars{1 - z^{r + 1}}^{k} \over \pars{1 - z}^{k}} \\[5mm] & = \bracks{z^{n}}\bracks{\sum_{i = 0}^{k}{k \choose i}\pars{-z^{r + 1}}^{i}} \bracks{\sum_{j = 0}^{\infty}{-k \choose j}\pars{-z}^{j}} \\[5mm] & = \bracks{z^{n}}\sum_{i = 0}^{k}\pars{-1}^{i}{k \choose i} \sum_{j = 0}^{\infty} \bracks{\pars{-1}^{j}{k + j - 1 \choose j}\pars{-1}^{j}}z^{\pars{r + 1}i + j} \\[5mm] & = \sum_{i = 0}^{k}\pars{-1}^{i}{k \choose i}\sum_{j = 0}^{\infty} {k + j - 1 \choose k - 1}\bracks{\pars{r + 1}i + j = n} \qquad\pars{~\bracks{\cdots}\ \mbox{is the}\ Iverson\ Bracket~} \\[5mm] & = \sum_{i = 0}^{k}\pars{-1}^{i}{k \choose i}\sum_{j = 0}^{\infty} {k + j - 1 \choose k - 1}\bracks{j = n - \pars{r + 1}i} \\[5mm] & = \sum_{i = 0}^{k}\pars{-1}^{i}{k \choose i} {k + n - \pars{r + 1}i - 1 \choose k - 1}\bracks{n - \pars{r + 1}i \geq 0} \\[5mm] & = \sum_{i = 0}^{k}\pars{-1}^{i}{k \choose i} {k + n - \pars{r + 1}i - 1 \choose k - 1}\bracks{i \leq {n \over r + 1}} \\[5mm] & = \bbox[15px,#ffe,border:1px dotted navy]{\ds{% \sum_{i = 0}^{m}\pars{-1}^{i}{k \choose i} {k + n - \pars{r + 1}i - 1 \choose k - 1}\,,\qquad m \equiv \min\braces{k,\left\lfloor\,{n \over r + 1}\,\right\rfloor}}} \end{align}
Felix Marin
  • 89,464