Buddy, Can You Spare a Tronk?
Time limit5sMemory limit128 MB
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 , , , , and of a dollar. In the world of Tron there are infinitely many coins, one for the reciprocal of every positive integer:
In particular, there is no smallest coin.
Using a combination of exactly coins, how many different ways are there to make change for one Tronk — that is, to choose unit fractions whose sum is exactly ? For example, with coins there are exactly three ways:
Two extra constraints apply:
- You are given an integer . No coin may be used more than times. For example, if the first combination above is no longer valid, but the other two still are. If , each coin may be used any number of times.
- You are given a list of integers . None of the reciprocals may be used. For example, if then and 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 ), a set of coins can sum to a value arbitrarily close to ; if it does not sum to exactly , it is wrong.
Input
The first line contains the number of test cases (). Each of the next lines describes one test case and lists, in order:
- — the exact number of coins that must be used ();
- — the repetition limit ( means each coin may be used any number of times);
- — the number of forbidden coins ();
- — the forbidden coins.
You may assume that no valid combination ever needs a coin smaller than ; equivalently, every denominator that can appear is at most .
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 is the test-case number starting from , 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 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 is the number of valid combinations. If there are none, print No solutions found instead, with no combination lines.