Thursday, July 8, 2021

CSES Coin Combinations 1

 Problem:

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:

  • 2+2+5
  • 2+5+2
  • 5+2+2
  • 3+3+3
  • 2+2+2+3
  • 2+2+3+2
  • 2+3+2+2
  • 3+2+2+2

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.

Constraints

  • 1n100
  • 1x106
  • 1ci106

Example

Input:
3 9
2 3 5


Output:
8


Full Explanation:



CODE:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n=0,x=0;
    cin>>x>>n;

    int dice[x];

    for(int i=0 ; i<x ; i++){
        cin>>dice[i];
    }

    long long value[n+1];

    value[0] = 1;
    for(int i=1;i<=n;i++){

        value[i] = 0;

        for (auto c:dice){
            if(i-c >=0){
                value[i]+=(value[i-c]%1000000007);
            }
        }
    }

    cout<<value[n]%(1000000007);

    return 0;
}

No comments:

Post a Comment

CSES Coin Combinations 1

 Problem: Consider a money system consisting of  n n  coins. Each coin has a positive integer value. Your task is to calculate the number of...