QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 512 MB Puntuación total: 110

#13402. Euklid

Estadísticas

很少有人提及,欧几里得的祖母来自克罗地亚的 Vrsi。欧几里得那位不那么出名(但在年轻时同样有才华)的表弟 Edicul* 就是从那里来的。

有一天,他们正在玩一个叫做“发明算法”的游戏。Edicul 在沙滩上写下两个正整数。然后他进行以下操作:当沙滩上的两个数都不是 1 时,他将它们记为 $(a, b)$ 使得 $a \ge b$。然后擦掉这两个数,在沙滩上写下 $(\lfloor \frac{a}{b} \rfloor, b)$,并重复此过程。当这两个数中有一个变成 1 时,另一个数就是他算法的结果。

形式化地,如果 $a$ 和 $b$ 是正整数,Edicul 算法的结果 $R(a, b)$ 为:

$$R(a, b) = \begin{cases} R(b, a) & \text{if } a < b, \\ R(\lfloor \frac{a}{b} \rfloor, b) & \text{if } a \ge b > 1, \\ a & \text{if } a \ge b = 1. \end{cases}$$

欧几里得想了一会儿,说:“Edicul,我有一个更好的主意……”,剩下的就是历史了。不幸的是,Edicul 在数论领域的想法从未让他出名。这个悲伤的故事启发了以下问题:

给定正整数 $g$ 和 $h$,找到正整数 $a$ 和 $b$,使得它们的最大公约数是 $g$,且 Edicul 算法的结果 $R(a, b)$ 是 $h$。

*注:这在克罗地亚语中是一个双关语。翻译过来有点平淡,抱歉。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 40$) —— 独立测试用例的数量。

接下来的 $t$ 行,每行包含两个正整数 $g_i$ 和 $h_i$ ($h_i \ge 2$)。

输出格式

共输出 $t$ 行。对于第 $i$ 个测试用例,输出正整数 $a_i$ 和 $b_i$,满足 $\gcd(a_i, b_i) = g_i$ 且 $R(a_i, b_i) = h_i$。

输出的数字不能大于 $10^{18}$。可以证明,在给定的约束条件下,总是存在解。

如果某个测试用例存在多个解,输出其中任意一个即可。

数据范围

在所有子任务中,$1 \le g \le 200\,000$ 且 $2 \le h \le 200\,000$。

子任务 分值 约束条件
1 4 $g = h$
2 8 $h = 2$
3 8 $g = h^2$
4 15 $g, h \le 20$
5 40 $g, h \le 2000$
6 35 无附加限制。

样例

输入样例 1

1
1 4

输出样例 1

99 23

输入样例 2

2
3 2
5 5

输出样例 2

9 39
5 5

说明

第一个样例解释: 整数 $99$ 和 $23$ 互质,即它们的最大公约数是 $1$。我们有 $\lfloor \frac{99}{23} \rfloor = 4$,因此 $R(99, 23) = R(4, 23) = R(23, 4)$。接着 $\lfloor \frac{23}{4} \rfloor = 5$,所以 $R(23, 4) = R(5, 4) = R(1, 4) = R(4, 1) = 4$。

第二个样例解释: 对于第一个测试用例,$\gcd(9, 39) = 3$ 且 $R(9, 39) = 2$。 对于第二个测试用例,$\gcd(5, 5) = 5$ 且 $R(5, 5) = 5$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.