CSES Coin Combinations I

Author: Michael Cao

In this problem, we are asked the number of ways to achieve some value, xx, using nn coins of distinct values where the order of coins does not matter. This is known as the "Unordered Coin Change" problem, which you can read about in CPH Chapter 7 under "Counting the Number of Solutions".

Main Idea

To solve this problem, let dp[w]\texttt{dp[w]} equal the number of ways to achieve the sum of values, ww. Then, for some weight ww, let's try to use each coin. For dp[w]\texttt{dp[w]}, we'll transition from dp[w - coin[i]]\texttt{dp[w - coin[i]]} for all ii, where coin[x]\texttt{coin[x]} defines the value of the xx-th coin.

So, the transitions are:

dp[w]=i=1n(dp[wcoins[i]])dp[w] = \sum_{i=1}^n{(dp[w - coins[i]])}

Warning!

Remember to take your answer mod 109+710^9 + 7, as instructed in the problem statement.

Example Code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first

Give Us Feedback on CSES Coin Combinations I!