Problem 184
Coin Change Ways
Medium · 20 pts · Array, Dynamic Programming
Problem
You are given N distinct coin denominations, with an unlimited supply of each, and an amount S. Count the different combinations of coins that add up to exactly S. The order of the coins does not matter, so 2 + 1 and 1 + 2 are the same combination. Print the count modulo 1000000007. There is exactly one way to make 0: use no coins. Process one denomination at a time: ways[x] += ways[x - coin] for x from coin up to S. Looping over coins in the outer loop is what makes each combination count only once. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
First line: integers N and S. Second line: N distinct coin values.
Output
Print the number of combinations modulo 1000000007.
Constraints
1 <= N <= 300; 1 <= coin <= 5000; 0 <= S <= 5000
Example input
3 5 1 2 5
Example output
4
Explanation
With coins 1, 2 and 5, the amount 5 can be made as 5, as 2 + 2 + 1, as 2 + 1 + 1 + 1, and as 1 + 1 + 1 + 1 + 1. That is 4 combinations.