30
$\begingroup$

The problem of counting the solutions $(a_1,a_2,\ldots,a_n)$ with integer $a_i\geq0$ for $i\in\{1,2,\ldots,n\}$ such that $$a_1+a_2+a_3+\ldots+a_n=N$$ can be solved with a stars-and-bars argument.

What is the solution if one adds the constraint that $a_i\leq r_{i}$ for certain integers $r_{1},\ldots,r_n$?

e.g. for $n=3$, $N=6$ and $(r_1,r_2,r_3)=(3,3,2)$, the tuple $(a_1,a_2,a_3)=(2,3,1)$ is a solution, but $(2,1,3)$ is not a solution because $a_{3}=3>2=r_3$.

$\endgroup$
2
  • $\begingroup$ If you limit each $a_i\leq1$, then you don't need stars and bars in the first place, as it just becomes counting combinations of $N$ (nonzero $a_i$) chosen from a set of$~r$. However if your imposed limit is larger than$~1$, then the problem is not quite as easy. I suggest that you choose a more representative example in the question. $\endgroup$ Commented Nov 6, 2013 at 6:17
  • $\begingroup$ There are general solution via both iteration & recursion, check answers to this question: stackoverflow.com/questions/76058880 $\endgroup$
    – Eric
    Commented Apr 20, 2023 at 17:30

3 Answers 3

25
$\begingroup$

There is as far as I know no closed formula for this general problem, but there is a formula that allows the number of solutions to be computed in a number of operations independent of $N$. Consider first the case that all limits are equal $r_1=r_2=\cdots=r_n=r$. Then the number is the coefficient of $X^N$ in the polynomial $(1+X+\cdots+X^r)^n$. By writing this as a rational function of$~X$ $$ (1+X+\cdots+X^r)^n=\left(\frac{1-X^{r+1}}{1-X}\right)^n=\frac{(1-X^{r+1})^n}{(1-X)^n} $$ the coeffiecient of $X^k$ in the numerator is zero unless $k$ is a multiple $q(r+1)$ of $r+1$, in which case it is $(-1)^q\binom nq$, and the coefficient of $X^l$ in the inverse of the denominator is $(-1)^l\binom{-n}l=\binom{l+n-1}l$, which is zero unless $l\geq0$ and otherwise equal to $\binom{l+n-1}{n-1}$. It remains to sum over all $k+l=N$, which gives $$ \sum_{q=0}^{\min(n,N/(r+1))}(-1)^q\binom nq\binom{N-q(r+1)+n-1}{n-1}, $$ where the summation is truncated to ensure that $N-q(r+1)\geq0$ (the condition $l\geq0$). Although the summation looks complicated, it has at most $n+1$ easily computed terms, for any$~N$. Just to illustrate, the coefficient for $n=5$, $r=100$ and $N=243$ is easily computed to be $62018665$. An interesting point to remark is that if the summation had not been truncated, then the result would clearly have been a polynomial function of$~N$ of degree${}<n$ (because binomial coefficients $\binom xk$ are polynomial functions of$~x$ of degree$~k$). But on one hand that polynomial function gives the exact values of this problem for $N\geq n(r+1)$ where no truncation takes place, while on the other hand, given the original problem, those values are all$~0$; so the polynomial function will be identically zero! So an alternative formula for the result is to compute the negative of the truncated terms, which formula becomes after some massaging $$ \sum_{q=\lceil\frac{N+n}{r+1}\rceil}^n (-1)^{n-q}\binom nq\binom{q(r+1)-1-N}{n-1}, $$ which is easier to use for large$~N$. For instance in the above example this formula gives a single term $\binom{78}4=1426425$ for $N=426$; it is the same value as obtained for $N=74=500-426$ (from the first formula) which can be understood by the fact that the "remainders" $r_i-a_i$ add up to $nr-N$.

In the general case of distinct limits $r_i$, the approach is the same, but the formula becomes a bit messy. Instead of a numerator $(1-X^{r+1})^n$ one gets a product $P=(1-X^{r_1+1})\ldots(1-X^{r_n+1})$ which in general has more nonzero terms (the number of terms can be up to $\min(\Sigma r_i+n+1,2^n)$), but which can be computed once and for all. With $P=\sum_ic_iX^{e_i}$, the formula for the result becomes $$ \sum_ic_i\binom{N-e_i+n-1}{n-1}, $$ which still is a sum of a number of terms independent of$~N$. But of course computing the polynomial $\frac P{(1-X)^n}$ beforehand, and then for any $N$ just looking up the coefficient of $X^N$, is another essentially constant-time (in $N$) solution.

$\endgroup$
6
  • $\begingroup$ I posted a question a couple of days ago which appears to be essentially the same question as this one - at least I think it is. If they are the same problem, is the upshot of your post that the most effective way to calculate the numbers in the general case is to check the coefficients of the polynomial? I'm having a little trouble understanding what number the last formula in your post calculates. $\endgroup$
    – Geoffrey
    Commented Nov 7, 2013 at 5:57
  • 1
    $\begingroup$ The last formula just computes the coefficient of $X^N$ in $(\sum_ic_iX^{e_i})(1-X)^{-n}$. It is interesting as a computational method only if the left factor $P$ (which was computed as a product of sparse polynomials) is still sparse. If it is dense, better compute the product polynomial right away, to get the answer for all $N$ at once. $\endgroup$ Commented Nov 7, 2013 at 6:13
  • $\begingroup$ Nice answer. An alternative method for the case where all $r_i = r$ is to use inclusion exclusion. Also, a perhaps clearer way to arrive at the "remainder" formula is to just make the change of variables $a_i = r_i - b_i$. Then you simply need to count the number of solutions to $b_1 + \ldots + b_n = \left( \sum r_i \right) - N$ where each $b_i \leq r_i$. $\endgroup$ Commented Feb 22, 2015 at 1:31
  • $\begingroup$ There are general solution via both iteration & recursion, check answers to this question: stackoverflow.com/questions/76058880 $\endgroup$
    – Eric
    Commented Apr 20, 2023 at 17:30
  • $\begingroup$ @Eric: in this question the problem is one of enumerative combinatorics, in other words counting the number of solutions, not generating them by an algorithm. The idea is to be able to count the solutions even when their number is beyond what can be practically enumerated. When I say there is no closed formula (not something easy to define, but in any case summations and products without a priori bound on the number of terms/factors are excluded), I mean just that. My solution is not a closed formula, even though it is reasonably efficient. The linked question is just (related but) different. $\endgroup$ Commented Apr 22, 2023 at 8:26
19
$\begingroup$

For future reference, to people who are unfamiliar with generating functions, here is a solution using the principle of inclusion exclusion.


Ignoring the constraint $a_i\le r_i$, the number of solutions is $\binom{N+n-1}{n-1}$, by stars and bars. To incorporate these constraints, we subtract the "bad" solutions where some $a_i>r_i$. To count solutions where $a_1>r_1$, we instead count solutions to the equation $$ (a_1-r_1-1)+a_2+a_3+\dots+a_n=N-r_1-1 $$ Now, all summands on the left hand side are nonnegative integers, so the number of solutions is $\binom{N-r_1-1+n-1}{n-1}$. Therefore we subtract $\binom{N-r_i-1+n-1}{n-1}$ for each $i=1,2,\dots,n$.

However, solutions with two variable which were too large have now been subtracted twice, so these must be added back in. Solutions where $a_i>r_i$ and $a_j>r_j$ can be counted by subtracting $r_i+1$ from $a_i$ and $r_j+1$ from $a_j$, leaving a list of integers summing to $N-(r_i+1)-(r_j+1)$, the number of which is $\binom{N-(r_i+1)-(r_j+1)+n-1}{n-1}$.

We must then correct for the solutions with three variables which are too large, then four, and so on. This can be handled systematically using the principle of inclusion exclusion. The result is $$ \sum_{S\subseteq \{1,2,\dots,n\}}(-1)^{|S|}\binom{N+n-1-\sum_{i\in S}(r_i+1)}{n-1} $$ Here, we define $\binom{m}k=0$ whenever $m<0$.


For the special case $r_1=r_2=\dots=r_n=r$ where the upper limit is the same for each variable, the result is $$ \sum_{k=0}^{\lfloor N/(r+1) \rfloor}(-1)^k\binom{n}k\binom{N-k(r+1)+n-1}{n-1}. $$

$\endgroup$
0
2
$\begingroup$

I transplanted this answer from a posted question that is actually a duplicate. So, the duplicate question can now be deleted. This answer, which essentially covers the same analysis as answers already posted to this question, is more long-winded, and is intended for readers totally new to the topics of Stars and Bars and Inclusion Exclusion.


Added an Addendum-2, to make the answer more generic. Addendum-2 covers the situation where the lower bound on each variable is $1$, rather than $0$.


Identify all solutions to $x_1 + \cdots + x_k = n$ subject to the following constraints:

  • $x_1, \cdots, x_k$ are all non-negative integers.
  • $c_1, \cdots, c_k$ are all fixed positive integers.
  • $x_i \leq c_i ~: ~i \in \{1,2,\cdots, k\}.$

To the best of my knowledge, there are two general approaches:

First, I will provide the basic Inclusion Exclusion framework. Then, within this framework, I will apply Stars and Bars theory.


Inclusion Exclusion framework:

Let $A$ denote the set of all solutions to the equation $x_1 + \cdots + x_k = n ~: ~x_1, \cdots, x_k$ are all non-negative integers.

For $i \in \{1,2,\cdots, k\}$, let $A_i$ denote the subset of A, where $x_i$ is restricted to being $> c_i$. That is, $A_i$ represents the specific subset where the upper bound on $x_i$ is violated.

For any finite set $S$, let $|S|$ denote the number of elements in the set $S$.

Then it is desired to enumerate $|A| - |A_1 \cup \cdots \cup A_k|.$

Let $T_0$ denote $|A|$.

For $j \in \{1,2, \cdots, k\}$ let $T_j$ denote

$\displaystyle \sum_{1 \leq i_1 < i_2 < \cdots < i_j \leq k} |A_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_j}|.$

That is, $T_j$ represents the sum of $\binom{k}{j}$ terms.

Then, in accordance with Inclusion - Exclusion theory, the desired enumeration is

$$\sum_{i = 0}^k (-1)^iT_i.$$

This concludes the description of the Inclusion-Exclusion framework.

All that remains is to provide a systematic algorithm for enumerating
$T_0, T_1, \cdots, T_k$.


Stars and Bars theory:

(1)
$T_0 = \binom{n + [k-1]}{k-1}.$

(2)
To enumerate $A_i$, you have to enumerate the number of solutions to $x_1 + \cdots + x_k = n ~: x_i > c_i$.

The standard approach is to set $y_i = x_i - (c_i + 1),$ with all of the rest of the variables $y_1, \cdots, y_k = x_1, \cdots, x_k,$ respectively.

Then, there is a one to one correspondence between the solutions that you are trying to enumerate and the solutions to

$y_1 + \cdots + y_i + \cdots + y_k = n - (c_i + 1) ~: ~$ each variable is restricted to the non-negative integers.

Here, $n - (c_i + 1) < 0$ implies that there are $0$ solutions.

Otherwise, per (1) above, the number of solutions is $\displaystyle \binom{n - [c_i + 1] + [k-1]}{k-1}.$

As defined, $T_1 = \sum_{i = 1}^k |A_i|$, so now, the procedure for enumerating $T_1$ is clear.


(3)
Consider how to enumerate $T_2$ which denotes

$\displaystyle \sum_{1 \leq i_1 < i_2 \leq k} |A_{i_1} \cap A_{i_2}|.$

That is, $T_2$ denotes the summation of $\binom{k}{2}$ terms.

I will illustrate how to specifically enumerate $|A_1 \cap A_2|$, with the understanding that this same approach should also be used on the other
$\displaystyle \left[ \binom{k}{2} - 1\right]$ terms.

The approach will be very similar to that used in (2) above.

Let $y_1 = x_1 - (c_1 + 1).$
Let $y_2 = x_2 - (c_2 + 1).$
Let $y_i = x_i ~: ~3 \leq i \leq k$.

Then, there is a one to one correspondence between $|A_1 \cap A_2|$ and the number of non-negative integer solutions to

$y_1 + y_2 + y_3 + \cdots + y_k = n - (c_1 + 1) - (c_2 + 1).$

Again, if $n - (c_1 + 1) - (c_2 + 1) < 0$, then the number of solutions $= 0$.

Otherwise, again per (1) above, the number of solutions is given by

$\displaystyle \binom{n - [c_1 + 1] - [c_2 + 1] + [k-1]}{k-1}.$


(4)
Now, consider how to enumerate $T_j ~: ~3 \leq j \leq k$ which denotes

$\displaystyle \sum_{1 \leq i_1 < i_2 < \cdots < i_j \leq k} |A_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_j}|.$

That is, $T_j$ denotes the summation of $\binom{k}{j}$ terms.

I will illustrate how to specifically enumerate $|A_1 \cap A_2 \cap \cdots \cap A_j|$, with the understanding that this same approach should also be used on the other
$\displaystyle \left[ \binom{k}{j} - 1\right]$ terms, when $j < k$.

The approach will be very similar to that used in (3) above.

Let $y_1 = x_1 - (c_1 + 1).$
Let $y_2 = x_2 - (c_2 + 1).$
$\cdots$
Let $y_j = x_j - (c_j + 1).$
Let $y_i = x_i ~: ~j < i \leq k.$

Then, there is a one to one correspondence between $|A_1 \cap A_2 \cap \cdots \cap A_j|$ and the number of non-negative integer solutions to

$y_1 + y_2 + y_3 + \cdots + y_k = n - (c_1 + 1) - (c_2 + 1) - \cdots - (c_j + 1).$

Again, if $n - (c_1 + 1) - (c_2 + 1) - \cdots - (c_j + 1) < 0$, then the number of solutions $= 0$.

Otherwise, again per (1) above, the number of solutions is given by

$\displaystyle \binom{n - [c_1 + 1] - [c_2 + 1] - \cdots - (c_j + 1) + [k-1]}{k-1}.$

This completes the Stars and Bars theory.


Addendum
Shortcuts

Suppose, for example, that $c_1 = c_2 = \cdots = c_k$.

Then, to enumerate $T_j$, all that you have to do is enumerate
$|A_1 \cap A_2 \cdots \cap A_j|$.

When $j < k$, the other $\displaystyle \left[\binom{k}{j} - 1\right]$ terms will be identical.

Therefore, $T_j$ will enumerate as

$\displaystyle \binom{k}{j} \times |A_1 \cap A_2 \cdots \cap A_j|$.

Further, if only some of the variables $c_1, c_2, \cdots, c_k$ are equal to each other, you can employ similar (but necessarily sophisticated) intuition to presume that some of the $\binom{k}{j}$ terms will have the same enumeration.

Also, in practice, you typically don't have to manually enumerate each of $T_1, T_2, \cdots, T_k.$

The following concept is based on the assumption (which may be false) that
$(c_1 + 1) + (c_2 + 1) + \cdots + (c_k + 1) > n.$

Re-order the scalars $c_1, \cdots c_k$ in ascending order, as
$m_1 \leq m_2 \leq \cdots \leq m_k.$

Find the smallest value of $j$ such that $(m_1 + 1) + \cdots + (m_j + 1) > n$.

Then $T_j, T_{j+1}, \cdots, T_k$ must all equal $0$.


Addendum-2

This section is being added to make the answer more generic. This section is not on point re the originally posted problem, because the originally posted problem (presumably) intended that each variable have a lower bound of $0$.

Suppose that you are asked to identify the number of solutions to the following problem:

  • $x_1 + x_2 + \cdots + x_k = n ~: ~n \in \Bbb{Z^+}.$

  • $x_1, x_2, \cdots, x_k \in \Bbb{Z^+}.$

  • For $~i \in \{1,2,\cdots,k\}, ~x_i \leq c_i ~: ~c_i \in \Bbb{Z^+}.$

Basic Stars and Bars theory assumes that the lower bound of (for example) $~x_1, x_2, \cdots, x_k~$ is $~0~$ rather than $~1.$

The easiest adjustment is to take the preliminary step of converting the problem into the desired form, through the change of variables

$$y_i = x_i - 1 ~: ~i \in \{1,2,\cdots, k\}.$$

Then, the revised problem, which will have the same number of solutions, will be:

  • $y_1 + x_2 + \cdots + y_k = (n-k).$

  • $y_1, y_2, \cdots, y_k \in \Bbb{Z_{\geq 0}}.$

  • For $~i \in \{1,2,\cdots,k\}, ~y_i \leq (c_i - 1) ~: ~c_i \in \Bbb{Z^+}.$

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .