CSES-Solutions/Dynamic Programming/Coin Combinations I.cpp
Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to
calculate the number of distinct ways you can produce a money sum x using the available coins.
For example, if the coins are {2,3,5} and the desired sum is 9, there are 8 ways:
Input
The first input line has two integers n and x: the number of coins and the desired sum of money.
The second line has n distinct integers c1,c2,…,cn: the value of each coin.
Output
Print one integer: the number of ways modulo 109+7.
Solution Code:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int maxN = 100;
const int maxX = 1e6;
const ll MOD = 1e9+7;
int N, X, c[maxN];
ll dp[maxX+1];
int main(){
scanf("%d %d", &N, &X);
for(int i = 0; i < N; i++)
scanf("%d", &c[i]);
dp[0] = 1;
for(int i = 0; i < X; i++)
if(dp[i] != 0)
for(int j = 0; j < N; j++)
if(i+c[j] <= X)
dp[i+c[j]] = (dp[i+c[j]] + dp[i]) % MOD;
printf("%lld\n", dp[X]);
}