Subset sum

There are traditionally two problems associated with Subset Sum. One is counting the number of ways a list of $$n$$ numbers make up a given target integer. This is also referred to as the Coin Counting problem. (We will call this Problem A for this article.) The other is figuring out if a subset of a given list of integers can sum to a given target integer (0 in common literature). This is, in a way, a special case of the knapsack problem. (We will call this Problem B for this article.)

Problem A (Coin Counting)
The problem can be defined as: Given a set (or list) of $$n$$ numbers $$a_1, a_2, \ldots, a_n$$, how many ways solutions is there to $$k_1a_1 + k_2a_2 + \ldots + k_na_n = T$$, where $$k_i >= 0$$ for all $$i$$.

Let $$c( i, j )$$ be the number of ways to sum to $$j$$ using the subsequence $$a_1, a_2, \ldots, a_i$$, then the recurrence is simply:

$$c( i, j ) = c( i - 1, j ) + c( i - 1, j - a_i )$$

where

$$c( 0, 0 ) = 1$$

This problem is often asked as a variation of such: Given a 1c, 2c, 5c, and a 10c piece, how many ways can you make 1 dollar?

Problem B (Simplified Knapsack)
This problem can be thought of as a more specific version of Problem A. It can be defined as: Given a set (or list) of $$n$$ numbers $$a_1, a_2, \ldots, a_n$$, is there a solution such that $$k_1a_1 + k_2a_2 + \ldots + k_na_n = T$$, where $$k_i >= 0$$. This is basically a decision variation on Problem A, and can be solved similarly.

The recurrence is basically:

$$c( i, j ) = c( i - 1, j ) \or c( i - 1, j - a_i )$$

where

$$c( 0, 0 ) = 1$$

Other variations
There are still other variations and constraints that can be solved similarly. A constraint where $$k_i \in (0,1) \forall i$$ is one such example.