This page is still under construction.

Parts of this page are still being built. What you see may change.

Buddy, Can You Spare a Tronk?

Time limit5sMemory limit128 MB

Summary
Count and list all multisets of n distinct unit fractions summing to exactly 1, with a repetition limit and forbidden denominators.
Level

Medium7 of 10

Topics
Backtracking, Number theory, Math, Brute force
Solved
No attempts yet

Problem

The principal unit of currency in the world of Tron is the Tronk. Making change for a Tronk is surprisingly tricky. In the USA we have half-dollars, quarters, dimes, nickels, and pennies, worth respectively 12\frac{1}{2}, 14\frac{1}{4}, 110\frac{1}{10}, 120\frac{1}{20}, and 1100\frac{1}{100} of a dollar. In the world of Tron there are infinitely many coins, one for the reciprocal of every positive integer:

1, 12, 13, 14, 15, …1,\ \frac{1}{2},\ \frac{1}{3},\ \frac{1}{4},\ \frac{1}{5},\ \dots

In particular, there is no smallest coin.

Using a combination of exactly nn coins, how many different ways are there to make change for one Tronk — that is, to choose nn unit fractions whose sum is exactly 11? For example, with n=3n = 3 coins there are exactly three ways:

13+13+13,12+13+16,12+14+14.\frac{1}{3}+\frac{1}{3}+\frac{1}{3},\qquad \frac{1}{2}+\frac{1}{3}+\frac{1}{6},\qquad \frac{1}{2}+\frac{1}{4}+\frac{1}{4}.

Two extra constraints apply:

  • You are given an integer rr. No coin may be used more than rr times. For example, if r=2r = 2 the first combination above is no longer valid, but the other two still are. If r=0r = 0, each coin may be used any number of times.
  • You are given a list of integers F={f1,f2,…,fk}F = \{f_1, f_2, \dots, f_k\}. None of the reciprocals 1fi\frac{1}{f_i} may be used. For example, if F={4,6}F = \{4, 6\} then 14\frac{1}{4} and 16\frac{1}{6} are forbidden, so only the first combination above remains valid.

Every combination you report must be exact. Because of floating-point error and extremely small coins (such as 110000000\frac{1}{10000000}), a set of coins can sum to a value arbitrarily close to 11; if it does not sum to exactly 11, it is wrong.

Input

The first line contains the number of test cases TT (T≤20T \le 20). Each of the next TT lines describes one test case and lists, in order:

  • nn — the exact number of coins that must be used (n≤10n \le 10);
  • rr — the repetition limit (r=0r = 0 means each coin may be used any number of times);
  • kk — the number of forbidden coins (k≤20k \le 20);
  • f1,f2,…,fkf_1, f_2, \dots, f_k — the forbidden coins.

You may assume that no valid combination ever needs a coin smaller than 110000000\frac{1}{10000000}; equivalently, every denominator that can appear is at most 10,000,00010{,}000{,}000.

Output

For each test case, print the results in the following format.

First print a header line:

Case i : Number of coins = n; Repetitions = r; Forbidden = [f_1 f_2 ... f_k]

Here ii is the test-case number starting from 11, and the forbidden coins are listed in the order they were given (use empty brackets [] when there are none).

Then print every valid combination on its own line. Within a line, list the coin denominators separated by single spaces and in non-decreasing order; for instance the set {16,13,12}\{\frac{1}{6}, \frac{1}{3}, \frac{1}{2}\} is written as 2 3 6. The combinations must appear in lexicographically increasing order (compare the denominator sequences element by element).

Finally print the line C solutions found, where CC is the number of valid combinations. If there are none, print No solutions found instead, with no combination lines.

Examples4

  1. Example 1

    Input
    3
    3 0 0
    5 1 2 3 4
    4 1 6 2 3 4 5 6 7
    
    Expected output
    Case 1 : Number of coins = 3; Repetitions = 0; Forbidden = []
    2 3 6
    2 4 4
    3 3 3
    3 solutions found
    Case 2 : Number of coins = 5; Repetitions = 1; Forbidden = [3 4]
    2 5 6 8 120
    2 5 6 9 45
    2 5 6 10 30
    2 5 6 12 20
    4 solutions found
    Case 3 : Number of coins = 4; Repetitions = 1; Forbidden = [2 3 4 5 6 7]
    No solutions found
    
  2. Example 2

    Input
    1
    1 0 0
    
    Expected output
    Case 1 : Number of coins = 1; Repetitions = 0; Forbidden = []
    1
    1 solutions found
    
  3. Example 3

    Input
    1
    3 2 0
    
    Expected output
    Case 1 : Number of coins = 3; Repetitions = 2; Forbidden = []
    2 3 6
    2 4 4
    2 solutions found
    
  4. Example 4

    Input
    1
    4 0 2 2 3
    
    Expected output
    Case 1 : Number of coins = 4; Repetitions = 0; Forbidden = [2 3]
    4 4 4 4
    1 solutions found