2

I wanted to calculate the number of solutions of the equation, but I am unable to get any lead. The equation is:

enter image description here

All I could get is by doing something like,

enter image description here

But I don't know how to proceed on this.

Noob Coder
  • 244
  • 2
  • 6

1 Answers1

0

I'd try solving this by using dynamic programming.

Here's some pseudocode to get you started:

Procedure num_solutions(n, k, m):
  # Initialize memoization cache:
  if this function has been called for the first time:
    initialize memo_cache with (n+1)*(k+1)*(m+1) elements, all set to -1

  # Return cached solution if available
  if memo_cache[n][k][m] is not -1:
    return memo_cache[n][k][m]

  # Edge case:
  if m is equal to 1:
    # Solution only exists if 1 <= m <= k
    if n >= 1 and n <= k, set memo_cache[n][k][m] to 1 and return 1
    otherwise set memo_cache[n][k][m] to 0 and return 0

  # Degenerate case: No solution possible if n<m or n>k*m
  if n < m or n > k * m:
    set memo_cache[n][k][m] to 0 and return 0

  # Call recursively for a solution with m-1 elements
  set sum to 0
  for all i in range 1..k:
    sum = sum + num_solutions(n - i, k, m - 1)

  set memo_cache[n][k][m] to sum and return sum
r3mainer
  • 23,981
  • 3
  • 51
  • 88