#P13604. [NWRRC 2022] Greatest Common Divisor

[NWRRC 2022] Greatest Common Divisor

题目描述

Gennady is an aspiring programmer. He is currently learning the Euclidean algorithm for computing the greatest common divisor of two positive integers.

Unfortunately, Gennady sometimes confuses the integer division operator (denoted by div\tt{div}) with the remainder operator (denoted by mod\tt{mod}). As an example, 3737 div\tt{div} 10=310 = 3 and 3737 mod\tt{mod} 10=710 = 7.

Here's Gennady's latest implementation of the Euclidean algorithm:

  • Input: two positive integers xx and yy.
  • While y>0y > 0:
    • Set x=xx = x div\tt{div} yy, then swap xx and yy.
  • Output: xx.

As you can see, if Gennady used the mod\tt{mod} operator instead of the div\tt{div} operator, his implementation would be correct: the algorithm above would successfully find the greatest common divisor of xx and yy. However, it turns out that even with this nasty bug the algorithm sometimes works correctly!

You are given an integer nn. Gennady is interested in finding all input pairs (x,y)(x, y) such that 1x,yn1 \le x, y \le n, the algorithm finishes, and produces the correct output. Let (x1,y1),(x2,y2),,(xk,yk)(x_1, y_1), (x_2, y_2), \ldots, (x_k, y_k) be all such pairs in lexicographic order (for all 1i<k1 \le i < k, either xi<xi+1x_i < x_{i+1}, or xi=xi+1x_i = x_{i+1} and yi<yi+1y_i < y_{i+1}).

You are also given qq queries. Query ii is a positive integer pip_i, and you should print xpix_{p_i} and ypiy_{p_i}, or report that pi>kp_i > k.

输入格式

The first line contains two integers nn and qq --- the upper bound on the input values and the number of queries (1n,q21051 \le n, q \le 2 \cdot 10^5).

Each of the next qq lines contains a single integer pip_i (1pin21 \le p_i \le n^2).

输出格式

For each query, print two integers. These integers must either be xpix_{p_i} and ypiy_{p_i}, denoting the pip_i-th input pair in lexicographic order such that the algorithm finishes and produces a correct output, or \t{-1 -1} if there are less than pip_i such pairs.

10 13
1
2
3
4
5
6
7
8
9
10
11
12
13
2 2
3 3
4 2
4 4
5 5
6 6
7 7
8 8
9 3
9 9
10 4
10 10
-1 -1