Upstairs and Downstairs
Time limit100sMemory limit512 MB
Konstantin must order at least K activities with capped repeats to minimize the chance Ilia falls asleep and later wakes.
- Level
Hard8 of 10
- Topics
- Probability, Greedy, Sorting
- Solved
- No attempts yet
Problem
Konstantin and Ilia live in the same house. Konstantin lives upstairs and likes activities that involve jumping, moving furniture around and generally making noise. Ilia lives downstairs and likes to sleep.
To have a good evening, Konstantin wants to perform at least activities. Last night Ilia asked him to try not to wake him up, and Konstantin, who is a good neighbor, agreed. He took the request very literally, so he picks his activities to make the probability that Ilia wakes up after falling asleep as small as possible.
Activity has a probability attached to it. When Konstantin performs it, Ilia is awake at the end of that activity with probability and asleep otherwise. This outcome does not depend on whether Ilia was asleep when the activity started, and it is independent of the outcome of every other activity. Konstantin can perform activity at most times, because doing it more often is boring, and a bored Konstantin does not have a good evening.
Konstantin fixes an ordered sequence of activities so that:
- the total number of activities is at least ;
- activity is performed no more than times;
- the probability that Ilia is woken up one or more times is as small as possible.
Ilia starts awake, so waking him up means that he is asleep at the end of some activity and awake at the end of the next one.
Konstantin cannot tell whether Ilia is awake or asleep, so he decides the whole sequence in advance and never adapts it while the evening goes on.
Find the smallest Konstantin can achieve while still having a good evening.
Input
The first line contains the number of test cases . Each test case begins with a line holding two integers and . The next lines describe the activities Konstantin can choose from, one per line, in the format a/b c: the activity leaves Ilia awake with probability , and Konstantin can perform it at most times. For example, 3/4 2 is an activity that leaves Ilia awake with probability and can be performed at most twice.
Limits
- , ,
- Let be the sum of all in one test case. Then and .
Output
For each test case, print one line in the format Case #x: Q, where is the case number starting from 1 and is the smallest probability that Ilia is woken up during the activities Konstantin performs.
Print rounded to exactly nine digits after the decimal point. Every answer in the test data is far from a rounding boundary, so a double precision result printed with nine digits gives the expected line.