This page is still under construction.

Parts of this page are still being built. What you see may change.

The Mailbox Manufacturers Problem

Interview

Time limit1sMemory limit128 MB

Summary
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 kk (1≤k≤101 \le k \le 10) identical prototypes, each able to hold up to mm (1≤m≤1001 \le m \le 100) 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:

  1. If a mailbox can withstand xx firecrackers, it can also withstand x−1x - 1 firecrackers.
  2. After an ignition a mailbox is either totally destroyed or completely unharmed (and reusable).

With only k=1k = 1 mailbox you must test 11 firecracker, then 22, and so on, one at a time; in the worst case (the mailbox survives even a full load of mm) this costs 1+2+⋯+m=m(m+1)21 + 2 + \cdots + m = \frac{m(m + 1)}{2} firecrackers. With more mailboxes you can do better.

The maximum number a prototype can withstand is an integer between 00 and mm; if it withstands a full load of mm, the answer for that prototype is mm. 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 NN (1≤N≤101 \le N \le 10), the number of test cases. Each of the following NN lines describes one test case with two integers kk and mm 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.

Examples1

  1. Example 1

    Input
    4
    1 10
    1 100
    3 73
    5 100
    
    Expected output
    55
    5050
    382
    495