Yeetzhee
Time limit20sMemory limit1024 MB
For N dice with M faces, find the minimum expected number of rolls needed to finish with group sizes A, when each die can be rerolled before moving on.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Probability
- Solved
- No attempts yet
Problem
Pommel is very bored at home, so she has invented a new game involving dice. Each die has the numbers from 1 to written on it. Whenever she throws a die, each of the possible values is equally likely.
Pommel places all the dice in a row. She goes through the dice one at a time from left to right. For each die she rolls, Pommel can either keep the value she rolled and move on to the next die, or she can re-roll the die. Pommel can re-roll a die as many times as she wants before moving on to the next die.
Once Pommel has gone through all the dice, the game is finished. To determine whether she has won, she puts the dice into groups. All dice with the same value are put into the same group. So if she finishes the game with distinct values, there will be groups. These groups of dice are then sorted by the number of dice in non-decreasing order.
For example:
- If the final dice results are [2, 2, 3, 2, 2, 3], the dice are put into two groups and ordered as [3, 3] and [2, 2, 2, 2].
- If the final dice results are [1, 6, 7, 7], the dice are put into three groups and ordered as [6], [1], and [7, 7] (or equivalently, [1], [6], and [7, 7]).
Pommel wins if she finishes the game with exactly groups, and the -th group contains exactly dice for all .
What is the expected value of the total number of dice rolls it will take Pommel to win the game, assuming she plays optimally to minimize this expected value?
It is guaranteed that for any valid input, it is possible for Pommel to win the game.
Input
The first line of the input gives the number of test cases, . test cases follow. The first line of each test case contains the integers , , and . Then lines follow describing the groups she must finish with. The -th line contains .
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the expected total number of dice rolls for Pommel to win the game.
will be considered correct if it is within an absolute or relative error of of the correct answer.
Constraints
- .
- .
- for all .
- .
- for all .
Hint
In Sample case #1, Pommel has dice, each with a number from 1 to written on it. To win, she must finish the game with groups. One group must have one die (), while the other group must have two dice (). One optimal strategy for Pommel is as follows:
- Pommel throws the first die once.
- Pommel throws the second die once.
- If the first and second dice are the same, Pommel keeps throwing the third die until it ends in a different value from the first two. It takes 1.2 dice rolls on average.
- If the first and second dice are different, Pommel keeps throwing the third die until it matches the first or the second die. It takes 3 dice rolls on average.
This strategy takes Pommel 4.7 () dice rolls on average.
In Sample case #2, Pommel has dice, each with a number from 1 to written on it. To win, she must finish the game with group, with all dice in it (). For the first die, Pommel rolls it once. Then, for each remaining die, she keeps rolling until it has the same value as the first one. It takes 2 dice rolls on average.
This strategy takes Pommel 9 () dice rolls on average.