問題
Use any number of each supplied denomination to make amount exactly. Count combinations, not orderings: two arrangements using the same counts of each coin are the same way. Denominations are distinct and positive.
Implement coin_sums(amount, coins).
Input
amount: The total amount to make.
coins: The available distinct denominations.
Output
Return the number of unordered combinations.
制約
0 ≤ amount ≤ 200. There are 0 through 10 distinct denominations, each from 1 through 200. All counts fit signed 64-bit. Amount 0 has one way: choose no coins.
例
入力:
coins = [1, 2]
amount = 4
期待される出力:
expected = 3
入力:
std::vector<long long> coins = {1, 2};
long long amount = 4;
期待される出力:
long long expected = 3;
入力:
long[] coins = new long[] {1, 2};
long amount = 4;
期待される出力:
long expected = 3;
解説
The combinations are four 1s, two 1s with one 2, and two 2s.
入力:
coins = [2]
amount = 3
期待される出力:
expected = 0
入力:
std::vector<long long> coins = {2};
long long amount = 3;
期待される出力:
long long expected = 0;
入力:
long[] coins = new long[] {2};
long amount = 3;
期待される出力:
long expected = 0;
解説
Any sum of 2-valued coins is even, so 3 is impossible.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
The order of coins does not matter, so combinations should be counted by denomination, not by permutation.
ヒント 2
Build the number of ways for each amount incrementally, letting each coin contribute to all reachable totals.
ヒント 3
Be careful with amount zero, which always has one valid way: choose no coins.
目安の計算量
O(amount × number_of_coins) time, O(amount) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。