CSES Coin Combinations II

Author: Michael Cao

This problem is very similar Coin Combinations I, with the main difference being that we only count ordered combinations. This is also a well-known problem called the Ordered Coin Change problem, which can also be found in CPH Chapter 7 under "Counting the Number of Solutions".

This editorial assumes you've solved Coin Combinations I. If not, the editorial can be found here. For this problem, we'll use the same state and transitions as Coin Combinations I.

Dealing with Ordered Ways

To only count ordered ways, we switch the order of the loops. Since we loop over the coins before the weights in the dp array, it's impossible to create two combinations with the same coins ordered differently, because the order that we update the weights using the coins is the order we loop over them.

#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 II!