Under construction

Still being built. Things may change

King Thrór's Gold

Time limit1sMemory limit128 MB

Summary
Count the ways to choose exactly k distinct bar values summing to T, and list all solutions in lexicographic order when there are at most 20.
Level

Medium5 of 10

Topics
Dynamic programming, Backtracking, Sorting, Combinatorics
Solved
No attempts yet

Problem

Dwarf king Thrór wants to reward his kk best warriors with some of his gold bars. The dwarves' unit of currency is the Mirian. Thrór owns nn bars of distinct values v1,…,vnv_1, \dots, v_n Mirians. He wants to hand out exactly kk bars whose values add up to a target total of TT Mirians. Because his warriors are not equally deserving, they will in general receive bars of different values.

Thrór asks his brother Frór to work out in how many different ways the target TT can be written as a sum of the values of exactly kk distinct bars.

For example, with the n=10n = 10 bars V={1,2,4,5,10,11,13,15,17,19}V = \{1, 2, 4, 5, 10, 11, 13, 15, 17, 19\}, handing out k=3k = 3 bars for a target of T=20T = 20 has four solutions:

1+2+171 + 2 + 17, 1+4+151 + 4 + 15, 2+5+132 + 5 + 13, 4+5+114 + 5 + 11.

For the same target with k=4k = 4 warriors there are only two:

1+2+4+131 + 2 + 4 + 13, 1+4+5+101 + 4 + 5 + 10.

Write a program that, given the target TT, the number of warriors kk, and the bar values V={v1,…,vn}V = \{v_1, \dots, v_n\}, reports how many different ways TT can be formed as the sum of exactly kk distinct values from VV. Whenever the number of solutions is 20 or fewer, the program must also list every solution in lexicographically increasing order: solutions using the smallest bar come first; among those, solutions using the next smallest bar come first; and so on. Within each solution the values are printed in increasing order.

Input

The first line contains the number of test cases CC (C≤100C \le 100). Each test case has the following format:

  • A line with the target value TT and the number of warriors kk, separated by a space (T≤500T \le 500, k≤50k \le 50).
  • A line with the number of gold bars nn (n≤100n \le 100).
  • The values v1,…,vnv_1, \dots, v_n of the nn bars. They are distinct positive integers listed in strictly increasing order. (They may span more than one line.)

The number of solutions is guaranteed to fit in a signed 64-bit integer (it is less than 263−12^{63} - 1).

Output

For each test case, in the order given, print the following block:

Test case X
 Target sum = T
 Number of warriors = k
 V = {v1, v2, ..., vn}
Number of solutions = C

Here X is the 1-based test-case number; T, k, and the values are echoed from the input (the values inside the braces are separated by a comma and a space); and C is the number of ways to form the target. The Target sum, Number of warriors, and V = {...} lines each begin with a single leading space; the Number of solutions line does not.

If C is 20 or fewer, print one additional line for each solution, in lexicographically increasing order. Each such line begins with a single leading space followed by the kk chosen values in increasing order, separated by single spaces.

Print one blank line between consecutive test cases.

Examples3

  1. Example 1

    Input
    2
    20 3
    10
    1 2 4 5 10 11 13 15 17 19
    100 9
    19
    1 2 4 5 6 8 9 10 12 13 15 16 17 18 19 20 21 23 24
    
    Expected output
    Test case 1
     Target sum = 20
     Number of warriors = 3
     V = {1, 2, 4, 5, 10, 11, 13, 15, 17, 19}
    Number of solutions = 4
     1 2 17
     1 4 15
     2 5 13
     4 5 11
    
    Test case 2
     Target sum = 100
     Number of warriors = 9
     V = {1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 23, 24}
    Number of solutions = 1491
    
  2. Example 2

    Input
    1
    20 3
    10
    1 2 4 5 10 11 13 15 17 19
    
    Expected output
    Test case 1
     Target sum = 20
     Number of warriors = 3
     V = {1, 2, 4, 5, 10, 11, 13, 15, 17, 19}
    Number of solutions = 4
     1 2 17
     1 4 15
     2 5 13
     4 5 11
    
  3. Example 3

    Input
    1
    20 4
    10
    1 2 4 5 10 11 13 15 17 19
    
    Expected output
    Test case 1
     Target sum = 20
     Number of warriors = 4
     V = {1, 2, 4, 5, 10, 11, 13, 15, 17, 19}
    Number of solutions = 2
     1 2 4 13
     1 4 5 10