CSES-Solutions/Dynamic Programming/Coin Combinations I.cpp

CSES-Solutions/Dynamic Programming/Coin Combinations I.cpp
1 min read
Consider a money system consisting of coins. Each coin has a positive integer value. Your task is to 
calculate the number of distinct ways you can produce a money sum  using the available coins.
For example, if the coins are and the desired sum is , there are ways:
Input
The first input line has two integers and : the number of coins and the desired sum of money.
The second line has distinct integers : the value of each coin.
Output
Print one integer: the number of ways modulo .
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]);
}
Men' fashion , Dating Tips , Relationship Advice

You may like these posts

Post a Comment