Mint
Time limit1sMemory limit128 MB
Given coin thicknesses, a length is constructible when at least four distinct thicknesses divide it; for each query height, report the nearest constructible length at or below it and the nearest at or above it.
- Level
Medium6 of 10
- Topics
- Number theory, Brute force, Math, Implementation
- Solved
- No attempts yet
Problem
The Royal Canadian Mint has commissioned a new line of designer coffee tables whose legs are built from stacks of coins. Every table has four legs. Each leg is a stack of coins of a single coin type, and the four legs must each use a different coin type. All four legs must have exactly the same length.
Many coin types are available, including foreign and commemorative coins, and each coin type has its own thickness. A leg built from a coin of thickness using coins has length , where is a whole number of coins.
Call a length constructible when at least four different coin types have a thickness that divides : then each of those four legs can be a whole number of coins of its own type, and all four legs reach the same length .
Given the available coin types and a desired table height, report the two constructible leg lengths nearest to the desired height — the greatest constructible length that does not exceed it, and the smallest constructible length that is not below it.
Input
The input contains several test cases. Each test case begins with a line holding two integers and (, ): the number of available coin types and the number of tables to design.
The next lines each contain one integer — the thickness of a coin type, given in hundredths of a millimetre. Two different coin types may share the same thickness.
The following lines each contain one integer — the desired height of a table, also in hundredths of a millimetre. Each desired height is at least the smallest constructible length, so both requested values always exist.
A line containing 0 0 follows the last test case and is not processed.
Output
For each desired table height, in the order given, print one line with two integers separated by a single space: the greatest constructible leg length that does not exceed the desired height, followed by the smallest constructible leg length that is not less than the desired height.