The Number Trick
Time limit5sMemory limit1536 MB
Given K observed subset products, find the most likely hidden multiset of N numbers from 2 to M under the given posterior score.
- Level
Medium7 of 10
- Topics
- Brute force, Combinatorics, Probability
- Solved
- No attempts yet
Problem
Maryam and Peiling are practicing a number trick.
Maryam first picks integers independently and uniformly at random from 2 to , and writes one of them on each of cards. The same number may appear several times. She then repeats the following times: she takes each of the cards independently with probability to form a subset, and writes down the product of the numbers on the cards she took. If she takes no card, the product is 1.
Maryam tells Peiling the products together with and . Peiling has to name the hidden numbers.
Peiling cannot always be right. When every product is 1, the products say nothing about the hidden numbers. So Peiling names the collection of numbers that is most likely given the products she saw. Your program computes exactly that answer for each of independent groups of products.
Here is the same rule as a formula. Let be a multiset of integers from 2 to .
- is the number of ordered -tuples whose multiset is . If occurs times in , then .
- is the number of subsets whose product is when the cards carry . There are subsets in total, and two cards are distinct even when they carry the same number.
If one group of products is , the score of is
is proportional to the posterior probability that the hidden multiset is once those products are observed. The answer for that group is therefore an that maximizes . Several multisets may reach the maximum, so take the one whose elements, listed in non-decreasing order, form the lexicographically smallest string.
Input
The first line contains the number of test cases . is always 1.
The second line contains the integers , , , separated by spaces.
Each of the next lines contains the products of one group, separated by spaces.
Limits
- Every given product is at least 1 and at most .
- For every group there is at least one multiset with for all , so a candidate with a positive score always exists.
Output
Print "Case #1:" on the first line.
Then print lines. Line holds the answer for group , that is the lexicographically smallest multiset among those maximizing , printed as digits. Write the digits in non-decreasing order with no spaces between them. Each digit is between 2 and . Since , every hidden number has one digit.