Under construction

Still being built. Things may change

Counting Coin Combinations

Interview

Time limit1sMemory limit128 MB

Summary
Count the number of ways to sum a set of coin denominations (unlimited use, order ignored) to reach a target amount, for multiple test cases.
Level

Easy3 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

Given several coin denominations and a target amount, count how many distinct combinations of coins can make that amount.

Each denomination may be used any number of times. Two ways that differ only by the order of the coins are considered the same combination.

Input

The first line contains the number of test cases T.

Each test case consists of three lines.

  • The first line contains the number of coin denominations N (1 <= N <= 20).
  • The second line contains the N coin values in increasing order. Each value is an integer from 1 to 10000.
  • The third line contains the target amount M (1 <= M <= 10000).

You may assume that the number of combinations is less than $2^{31}-1$.

Output

For each test case, print one line containing the number of combinations that make amount M using the given denominations.

Examples1

  1. Example 1

    Input
    3
    2
    1 2
    1000
    3
    1 5 10
    100
    2
    5 7
    22
    
    Expected output
    501
    121
    1