At a bake-sale cash box you accept coins in n distinct denominations, with an unlimited supply of each. A customer owes exactly A units. Count the number of distinct ways to make exactly A using the coins, where two ways are considered the same if they use the same number of each denomination (order of handing over the coins does not matter). Making A = 0 has exactly one way: hand over no coins.
Line 1: two integers n and A.
Line 2: n space-separated distinct integers, the coin denominations.
A single integer: the number of distinct coin multisets that sum to exactly A.
Example 1
Input
3 5 1 2 5
Expected
4
Explanation
The four multisets are {5}, {2,2,1}, {2,1,1,1} and {1,1,1,1,1}, so the count is 4.
Example 2
Input
2 3 2 4
Expected
0
Explanation
No multiset of 2s and 4s sums to the odd amount 3, so the count is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →