The Mailbox Manufacturers Problem
InterviewTime limit1sMemory limit128 MB
With k identical mailboxes usable up to m firecrackers, find the minimum worst-case cost in firecrackers to pin down exactly how many each can survive.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Binary search, Math, Greedy
- Solved
- No attempts yet
Problem
A mailbox manufacturer wants to know how many firecrackers his new mailbox prototype can withstand before it is destroyed. He gives you () identical prototypes, each able to hold up to () firecrackers, and asks you to determine the largest number of firecrackers a prototype can withstand.
You test a mailbox by placing some number of firecrackers inside and igniting them:
- If the number of firecrackers used is at most what the mailbox can withstand, the mailbox is unharmed and may be reused.
- Otherwise the mailbox is destroyed and can never be used again.
Every firecracker you ignite is consumed, whether or not the mailbox survives, so the cost of a single test equals the number of firecrackers used in it. You want a strategy that, in the worst case, spends as few firecrackers as possible while still determining the exact maximum a prototype can withstand.
You may assume:
- If a mailbox can withstand firecrackers, it can also withstand firecrackers.
- After an ignition a mailbox is either totally destroyed or completely unharmed (and reusable).
With only mailbox you must test firecracker, then , and so on, one at a time; in the worst case (the mailbox survives even a full load of ) this costs firecrackers. With more mailboxes you can do better.
The maximum number a prototype can withstand is an integer between and ; if it withstands a full load of , the answer for that prototype is . Determine the minimum number of firecrackers that, in the worst case, is required to find this maximum.
Input
The first line contains a single integer (), the number of test cases. Each of the following lines describes one test case with two integers and separated by a single space.
Output
For each test case, print a single line containing one integer: the minimum number of firecrackers needed, in the worst case, to determine the maximum number of firecrackers the mailbox prototype can withstand.