Mint

Time limit1sMemory limit128 MB

Summary
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 dd using kk coins has length k⋅dk \cdot d, where k≥1k \ge 1 is a whole number of coins.

Call a length LL constructible when at least four different coin types have a thickness that divides LL: 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 LL.

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 nn and tt (4≤n≤504 \le n \le 50, 1≤t≤101 \le t \le 10): the number of available coin types and the number of tables to design.

The next nn 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 tt 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.

Examples2

  1. Example 1

    Input
    4 2
    50
    100
    200
    400
    1000
    2000
    0 0
    
    Expected output
    800 1200
    2000 2000
    
  2. Example 2

    Input
    4 3
    2
    3
    5
    7
    210
    400
    630
    0 0
    
    Expected output
    210 210
    210 420
    630 630