1
$\begingroup$

Possible Duplicate:
Number of combinations with repetitions (Constrained)

Given an equation of form $Z_1+Z_2+\cdots+Z_m = n$ , where each $X_i \leq Z_i \leq Y_i$ and $(0\leq X_i , Y_i \leq 100$), each Zi is an integer. Calculate no of possible solutions of given equation.Brute force method that one can think of is to put the values for each $Z_i$ in its respective range,no of solutions which satisfy the equations can be counted but it is very tedious.Is there any theorem or method of mathematics which can come to my rescue? Please give me some idea.

  • 0
    Are those bits integers? Reals? What?2011-10-01
  • 0
    They are integers.2011-10-01
  • 1
    Is this the same as [this problem](http://math.stackexchange.com/questions/68995/number-of-combinations-with-repetitions-constrained) that I just answered?2011-10-01
  • 0
    How can we calculate the coefficient in that series? Is there any formula for this?2011-10-01
  • 1
    Funny, @robjohn. I just saw that migrated question, and voted to close that as an exact duplicate :-). It is probably an ongoing programming contest. If I read the timing data correctly this questions arrived a few minutes sooner?2011-10-01
  • 0
    @code_hacker The recurrence formula in my answer computes the coefficients of robjohn's series one factor at the time. Is that not good enough? Test it with a few examples!2011-10-01
  • 0
    @JyrkiLahtonen Okay,just give me sometime to think about your solution>If I find some difficulty then I'll let you know and thanks for the help.2011-10-01
  • 0
    @Jyrki: I voted to close this one, since it was asked later. Perhaps its "time asked" here is reflective of its pre-migration "time asked".2011-10-01
  • 0
    @robjohn Hmm. May be I looked at the wrong time stamp (like the time of edit or something)? Doesn't matter much. Both contestants (if that's what it is about) can take a look at both, and be equally informed.2011-10-01

2 Answers 2

1

The problem doesn't make sense unless the variables are integers, so I assume that.

I would iteratively (as a function of $m$) build a table containing numbers $S(j,k)$. Here $S(j,k)$ is the number of solutions of the system $$ Z_1+Z_2+\cdots+Z_j=k. $$

The initialization in the case $j=1$ is easy: $S(1,k)=1$, if $k$ is a possible value of $Z_1$, and $S(1,k)=0$ otherwise.

Assume that we have computed all the numbers $S(\ell,k)$ for some number of variables $\ell$. Then we have the recurrence relations for all $k$ $$ S(\ell+1,k)=\sum_{\text{$t$ is an allowed value for $Z_{\ell+1}$}}S(\ell,k-t). $$ That should do it. The number $S(m,n)$ is your answer.

Of course, you have to do a little bit of computation to decide on the lower and upper bounds of the indices in your table. It may or may not be easier, if you subtract $X_i$ from $Z_i$ so that the lower bound for the (partial) sums becomes zero. If you do that, you also have to subtract $\sum_i X_i$ from $n$.

  • 0
    I read your solution carefully and tried to implement it.Link to my implementation http://pastebin.com/mZN0PJ66 Can you please tell me if I'm doing it in the same way as you told or am I wrong? If I'm doing right is there anyway to improve it? Thanks in advance.2011-10-02
  • 0
    @code_hacker: Can't you test your program? For example, if $X_i=0, Y_i=1$ for all $i$, then you should get the binomial coefficient ${m\choose n}$ as the answer. I think that robjohn's answer gives you more test cases. Also, the case $m=2$ you can surely do by hand, and check that your code gives the same answers. The point is that such testing is a way more reliable check than any cursory look I can perform :-)2011-10-02
  • 0
    Thanks Jyrki Lahtonen for your help.I followed your advice and checked my implementation on several test cases.It worked fine on these test cases :-)2011-10-02
1

A slightly less tedious method than brute force enumeration is to first convert your problem into one of the form $Z'_1+Z'_2+\cdots+Z'_m = n'$ , where each $0 \leq Z'_i \leq Y_i - X_i$, (here $Z'_i = Z_i-X_i$ and $n' = n - \sum_i X_i$. Now your count is the $n'$th degree term of $\prod_i(1+x+x^2+\cdots+x^{Y_i-X_i})$. (This is essentially equivalent to what Jyrki did.)

  • 0
    Correct. Alternative you can start counting possible values of $Z_1+Z_2+\cdots+Z_\ell$ from the minimum value $X_1+\cdots+X_\ell$ onwards. Probably your approach has more predictable memory requirements.2011-10-01
  • 0
    This is essentially what I did in my answer to [the other question](http://math.stackexchange.com/questions/68995/number-of-combinations-with-repetitions-constrained).2011-10-01
  • 0
    @robjohn I did not see the other question. Note that http://math.stackexchange.com/questions/69000/iterating-over-all-matrices-with-fixed-row-and-column-sums/69019#69019 is also essentially a dup (though somewhat disguised)2011-10-01