haechandev
Algorithms

[Baekjoon 2225] Sum Decomposition

A combinatorial DP recurrence based on adding a number or reducing the target sum.

September 2, 2022

Count ordered ways to choose k nonnegative integers whose sum is n. Splitting cases according to whether the last number is zero gives a simple recurrence.

dp[n][k] = dp[n][k - 1] + dp[n - 1][k]
  • If the last number is zero, the other k - 1 numbers must sum to n.
  • If it is positive, subtracting one from it corresponds to choosing k numbers that sum to n - 1.

I initialized the ways to make zero and the ways using a single number to one, then filled the table.

Problem: Baekjoon · Code: View on GitHub