Konstantin chooses and orders at least K activities from limited copies to minimize the chance Ilia wakes after falling asleep.
Hard8ProbabilityDynamic programmingGreedySortingNo attempts yetTime limit5sMemory limit512 MBKonstantin and Ilia live in the same house. Konstantin lives upstairs and likes activities that involve jumping, moving furniture around and making noise. Ilia lives downstairs and likes to sleep.
To have a good evening, Konstantin wants to perform at least K activities. Last night Ilia asked him not to wake him up, and Konstantin agreed. He took the request literally, so he picks his activities so that the probability of waking Ilia after Ilia has fallen asleep is as small as possible.
Activity i has a probability ai/bi attached to it. When Konstantin performs that activity, Ilia is awake at the moment it ends with probability ai/bi and asleep otherwise. Ilia's state before the activity does not change this probability. Konstantin can perform activity i at most ci times. Doing it more often bores him, and a bored Konstantin does not have a good evening.
Konstantin fixes the whole sequence of activities in advance, so that:
Ilia starts the evening awake. He is woken up when he is asleep at the moment one activity ends and awake at the moment the next activity ends. Konstantin cannot see whether Ilia is awake or asleep, so he cannot change the plan while the evening goes on.
Find the smallest Q Konstantin can reach.
The first line holds the number of test cases T. Each test case starts with a line holding two integers N and K. The next N lines describe one activity each in the format a/b c: the activity leaves Ilia awake with probability a/b at the moment it ends, and Konstantin can perform it at most c times.
For each test case print one line in the format Case #x: Q, where x is the test case number starting from 1 and Q is the smallest probability that Ilia is woken up. Print Q with exactly nine digits after the decimal point.
Every answer is a rational number, and no answer lies within 10−12 of a value halfway between two nine digit decimals, so rounding a double precision result gives the required digits.