Ruins
Time limit8sMemory limit512 MB
For each pair a, b up to 10000, choose factorizations a=a1*a2 and b=b1*b2 to order four numbers so the sum of squared consecutive gaps is minimal.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Brute force, Sorting
- Solved
- No attempts yet
Problem
An explorer finds the door of an underground treasure house in desert ruins. Two positive integers and are carved on the door, and the lock opens only after the problem posed by those two numbers is solved.
Choose positive integers with and . The four numbers may be equal to each other. Sort the four numbers in non-decreasing order as and compute . Find the smallest value this expression takes over all choices.
For example, with and , the factorizations and give the sequence . Its value is , and no choice gives a smaller value.
Input
The input consists of several data sets. Each line holds two positive integers not greater than 10,000. A line with two zeros marks the end of the input and is not processed.
Output
For each data set, print the minimum value as a single integer on its own line. Print no extra spaces or characters.