This page is still under construction.

Parts of this page are still being built. What you see may change.

RealPhobia

Interview

Time limit1sMemory limit128 MB

Summary
For each fraction A/B, find C/D with D < B that minimizes the error |A/B - C/D|, breaking ties by the smallest denominator.
Level

Medium7 of 10

Topics
Number theory, Math, Binary search, Brute force
Solved
No attempts yet

Problem

Bert is a programmer who is genuinely afraid of floating-point arithmetic. He has had great success writing his programs with rational numbers instead, but he dislikes it when a denominator grows large.

Help Bert by writing a program that lowers the denominator of a rational number while introducing the smallest possible error. For a rational number A/BA/B with B>2B > 2 and 0<A<B0 < A < B, find a rational number C/DC/D such that:

  1. 0<C<D<B0 < C < D < B;
  2. the error ∣AB−CD∣\left|\dfrac{A}{B} - \dfrac{C}{D}\right| is the minimum over all valid CC and DD; and
  3. among all pairs achieving that minimum error, DD is the smallest possible positive integer.

Because condition 3 minimises DD, the answer fraction C/DC/D is always already in lowest terms.

Input

The first line contains an integer KK (1<K<10001 < K < 1000), the number of test cases. Each of the next KK lines contains one test case: a fraction written as two integers AA and BB separated by a slash (/), where

  1. BB is a 32-bit integer strictly greater than 22, and
  2. 0<A<B0 < A < B.

Output

For each test case, print one line containing the fraction C/DC/D, written as two integers separated by a slash (/).

Examples1

  1. Example 1

    Input
    3
    1/4
    2/3
    13/21
    
    Expected output
    1/3
    1/2
    8/13