Cosmic Settlement
InterviewTime limit1sMemory limit512 MB
Given n rectangles of size a by b and a w by h field, find the largest integer d such that n rectangles of size (a+2d) by (b+2d) fit in a grid on the field, in either orientation.
- Level
Medium6 of 10
- Topics
- Binary search, Math, Implementation
- Solved
- No attempts yet
Problem
To colonize Mars, an research base must be built. The base consists of identical modules, each of which is a rectangle.
Each module is a habitation compartment shaped like an meter rectangle. To increase the reliability of the modules, engineers can add a layer of additional shielding around each module. The thickness of this layer must be a whole number of meters, and all modules must have the same shielding thickness. A module with shielding of thickness meters has the shape of an meter rectangle.
All modules must be placed on a pre-prepared rectangular field of size meters. They must be organized as a regular grid: their sides must be parallel to the sides of the field, and the modules must all be oriented the same way.
Write a program that, given the number and size of the modules and the size of the field where they are placed, determines the maximum thickness of the additional shielding layer that can be added to each module.
Input
The input file contains five space-separated integers: , , , , and (). It is guaranteed that without additional shielding, all modules can be placed in the settlement as described.
Output
The output file must contain a single integer: the maximum possible thickness of the additional shielding. If no additional shielding can be installed, output the number .
Notes
In the first example, shielding of thickness meters can be installed and the modules can be placed on the field as shown in the figure.
In the second example, the habitation compartment has size meters, and the field has size meters. Additional shielding cannot be added to the module.


