Virtual_Contestant's blog

By Virtual_Contestant, history, 3 years ago, In English

We know that the solution of x1 + x2 + x3 .. xk = n, where xi >= 0 is given by nCr(n + k — 1, n). I was trying to solve a problem in which I had the constraints that all the xi were bounded by ai meaning (xi >= 0 and xi <= ai for all i from 1 to k). I know how to solve this problem using hand (by findind the coefficients and all) but how do I write a code for it or is it possible to write a code for it?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Do you have some constraints on $$$n$$$ and $$$k$$$? If they are low, you can use dynamic programming: let $$$\mathrm{dp}[i][j]$$$ be the number of sequences with length $$$i$$$ and sum $$$j$$$.