eolymp
bolt
Try our new interface for solving problems
Problems

Checkpoint

Checkpoint

You are racing on a 2D lattice grid starting from the origin (\textbf{0}, \textbf{0}) towards a goal (\textbf{M}, \textbf{N}) where \textbf{M} and \textbf{N} are positive integers such that \textbf{0} < \textbf{M} ≤ \textbf{N}. There is a checkpoint that's neither on the origin nor on the goal with coordinates (\textbf{m}, \textbf{n}) such that \textbf{0} ≤ \textbf{m} ≤ \textbf{M} and \textbf{0} ≤ \textbf{n} ≤ \textbf{N}. You must clear the checkpoint before you reach the goal. The shortest path takes \textbf{T = M + N} steps. At each point, you can move to the four immediate neighbors at a fixed speed, but since you don't want to lose the race, you are only going to take either a step to the right or to the top. Even though there are many ways to reach the goal while clearing the checkpoint, the race is completely pointless since it is relatively easy to figure out the shortest route. To make the race more interesting, we change the rules. Instead of racing to the same goal (\textbf{M}, \textbf{N}), the racers get to pick a goal (\textbf{x}, \textbf{y}) and place the checkpoint to their liking so that there are exactly \textbf{S} distinct shortest paths. For example, given \textbf{S = 4}, consider the following two goal and checkpoint configurations Placing the checkpoint at (\textbf{1}, \textbf{3}) and the goal at (\textbf{2}, \textbf{3}). There are \textbf{4} ways to get from the origin to the checkpoint depending on when you move to the right. Once you are at the checkpoint, there is only one way to reach the goal with minimal number of steps. This gives a total of \textbf{4} distinct shortest paths, and takes \textbf{T = 2 + 3 = 5} steps. However, you can do better. Placing the checkpoint at (\textbf{1}, \textbf{1}) and the goal at (\textbf{2}, \textbf{2}). There are two ways to get from the origin to the checkpoint depending on whether you move to the right first or later. Similarly, there are two ways to get to the goal, which gives a total of \textbf{4} distinct shortest paths. This time, you only need \textbf{T = 2 + 2 = 4} steps. As a Hacker Cup racer, you want to figure out how to place the checkpoint and the goal so that you cannot possibly lose. Given \textbf{S}, find the smallest possible \textbf{T}, over all possible checkpoint and goal configurations, such that there are exactly \textbf{S} distinct shortest paths clearing the checkpoint. \InputFile As input for the race you will receive a text file containing an integer \textbf{R (R ≤ 20)}, the number of races you will participate in. This will be followed by \textbf{R} lines, each describing a race by a single number \textbf{S} (\textbf{1} ≤ \textbf{S} ≤ \textbf{10000000}). Lines are separated using Unix-style ("\textbf{\textbackslash n}") line endings. \OutputFile Your submission should contain the smallest possible length of the shortest path, \textbf{T} for each corresponding race, one per line and in order.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5
4
5
12
14
1
Output example #1
Case #1: 4
Case #2: 6
Case #3: 6
Case #4: 9
Case #5: 2
Source Facebook Hacker Cup 2012 Round 1