0

It's a homework question and has to be solved using Dyanmic Programming approach.

What I've managed to do so far is that:

Let f(x) denote the number of times x can be written:

Then f(x) = f(x - 1) + 1 ; f(5) = f(4) + 1 (5 = 4 + 1)

But I don't think this is the right approach. Anybody would like to help?

An example of what the problem really is:

Number of ways 4 can be written:

4: 3 + 1
4: (2 + 1) + 1
4: 2 + 2
4: (1 + 1) + (1 + 1)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Cartoon
  • 101
  • 4
  • 11

1 Answers1

2

this representation is call partition. it could be solved in different ways.

for example, let's say

f(x, m) - number of partitions of x 
such that the largest number in that partition is m

then

f(x, m) = sum of all f(x - m, k) where (1 <= k <= m),
also (k<=x-m), because f(x, y) = 0 where (y > x)

for your example ( let's count the number itself a partition also (f(x, x) = 1))

f(1, 1) = 1
f(2, 1) = f(1, 1) = 1
f(2, 2) = 1
f(3, 1) = f(2, 1) = 1
f(3, 2) = f(1, 1) = 1 //+ f(1, 2) zero
f(4, 1) = f(3, 1) = 1 
f(4, 2) = f(2, 1) + f(2, 2) = 2
f(4, 3) = f(1, 1) = 1 // + f(1, 2) + f(1, 3) zeroes
f(4, 4) = 1

so the sum of f(4, 1), f(4, 2), f(4, 3), f(4, 4) = 5 ( 4 if not count 4 itself a partition)

Herokiller
  • 2,891
  • 5
  • 32
  • 50