You can define your function by assuming the limits c[0] , c[1] , ... c[m-1] as fixed, and then writing a recursive formula that returns the number of ways you can distribute n balls into cells , starting at index k . With this approach, the basic formula is simply
def solutions(n, k): if n == 0: return 1
then you need to add memoization (this will be equivalent to the DP approach) and some other optimizations, for example, for example. that if n > c[k] + c[k+1] + c[k+2] + ... , then you know that there are no solutions without the need for a search (and you can copy partial sums beforehand).
6502
source share