#C6618. Coin Change Combinations

    ID: 50398 Type: Default 1000ms 256MiB

Coin Change Combinations

Coin Change Combinations

You are given a target amount \(n\) and \(m\) coin denominations. Your task is to compute the number of distinct ways to make change for \(n\) using the provided coin denominations. Each coin can be used an unlimited number of times and the order in which coins are used does not matter.

Formally, let the coin denominations be \(c_1, c_2, \ldots, c_m\). We want to count the number of solutions for the equation:

[ c_1x_1 + c_2x_2 + \cdots + c_mx_m = n ]

where \(x_i \ge 0\) are integers. A dynamic programming approach can be used to solve this problem using the recurrence:

[ dp[j] = dp[j] + dp[j-c] \quad \text{for each coin } c \text{ and } j \ge c ]

Your program should read from standard input and write the result to standard output.

inputFormat

The input consists of two lines:

  • The first line contains two space-separated integers, \(n\) (the target amount) and \(m\) (the number of coin denominations).
  • The second line contains \(m\) space-separated integers representing the coin denominations.

You may assume \(1 \le n \le 10^4\) and \(1 \le m \le 100\).

outputFormat

Output a single integer: the number of distinct ways to make change for the target amount \(n\) using the given denominations.

## sample
10 2
1 2
6