Number of solutions of $x_1+x_2+\dots+x_k=n$ with $x_i\le r$ 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?
 A: 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.
A: $\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}
