Problem 755
Count Subsets with Sum K
Medium · 20 pts · Dynamic Programming, Knapsack
Problem
Given N non-negative integers (zeros are allowed) and a target K, count the subsets whose elements add up to exactly K, modulo 1000000007. Subsets are chosen by position, so equal values at different positions make different subsets; the empty subset counts when K is 0. Zeros double the count, since each zero can be taken or left. Keep ways[s] for every sum s up to K, starting with ways[0] = 1, and update the sums from high to low for every element.
Input
The first line contains N and K. The second line contains the N integers.
Output
Print the number of subsets with sum K, modulo 1000000007.
Constraints
1 <= N <= 1000; 0 <= A[i] <= 1000; 0 <= K <= 1000
Example input
4 3 1 2 2 3
Example output
3
Explanation
For 1 2 2 3 with K = 3, the subsets {1, 2} (with either 2) and {3} add up to 3. That is 3 subsets.