Блог пользователя Virtual_Contestant

Автор Virtual_Contestant, история, 4 года назад, По-английски

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?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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$$$.