Greatest Common Divisor
Time limit5sMemory limit1024 MB
Given n and queries p, find the p-th pair (x, y) in lexicographic order where Gennady's div-based Euclid finishes and outputs the true gcd.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Simulation
- Solved
- No attempts yet
Problem
Gennady is an aspiring programmer. He is learning the Euclidean algorithm for computing the greatest common divisor of two positive integers.
Gennady often confuses the integer division operator (div) with the remainder operator (mod). For example, div and mod .
Here is Gennady's implementation of the Euclidean algorithm:
- Input: two positive integers and .
- While : set
div, then swap and . - Output: .
If Gennady used mod instead of div, the algorithm would correctly find the greatest common divisor of and . However, even with this bug, the algorithm sometimes works correctly.
Gennady wants to find all input pairs such that , the algorithm finishes, and produces the correct output. Let be all such pairs in lexicographic order.
You are also given queries. Query is a positive integer . Print and , or report that .
Input
The first line contains two integers and , the upper bound on the input values and the number of queries ().
Each of the next lines contains a single integer ().
Output
For each query, print two integers: and , the -th pair in lexicographic order such that the algorithm finishes and produces a correct output. If there are fewer than such pairs, print -1 -1.