QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 256 MB 总分: 100 可 Hack ✓

#15610. 忙碌河狸的故障机器

统计

在 Busy Beaver 的机器人实验室深处,放着一台实验性的 Busy Beaver 机器。给定一个用 $B$ 进制表示的正整数 $X$,它试图产生两个正整数 $Y$ 和 $Z$,使得 $X + Y = Z$,并且 $Y$ 和 $Z$ 的 $B$ 进制表示包含完全相同的数位多重集(multiset of digits)。

该机器从不产生前导零,也从不输出数位个数达到或超过 $2 \cdot 10^5$ 的数字。它最近停止了工作,因此你的任务是确定是否存在这样的 $Y$ 和 $Z$,如果存在,则输出它们。你将获得不含前导零的 $X$ 的 $B$ 进制数位。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 100$),表示测试用例的数量。

每个测试用例由两行组成。

每个测试用例的第一行包含两个整数 $N$ 和 $B$ ($1 \le N \le 10^5$;$2 \le B \le 10^5$)。

每个测试用例的第二行包含 $N$ 个整数 $a_1, a_2, \dots, a_N$ ($0 \le a_i \le B - 1$;$a_1 \ne 0$),表示 $X$ 在 $B$ 进制下的数位: $$X = a_1 B^{N-1} + a_2 B^{N-2} + \dots + a_N$$

所有测试用例中 $N$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,如果不存在合法解,输出单行 NO

否则,第一行输出 YES

第二行输出一个整数 $M$ ($1 \le M \le 2 \cdot 10^5$),表示 $Y$ 和 $Z$ 的数位个数。

下一行输出 $M$ 个整数 $p_1, p_2, \dots, p_M$ ($0 \le p_i \le B - 1$;$p_1 \ne 0$),表示: $$Y = p_1 B^{M-1} + p_2 B^{M-2} + \dots + p_M$$

再下一行输出 $M$ 个整数 $q_1, q_2, \dots, q_M$ ($0 \le q_i \le B - 1$;$q_1 \ne 0$),表示: $$Z = q_1 B^{M-1} + q_2 B^{M-2} + \dots + q_M$$

数位序列 $(p_1, \dots, p_M)$ 和 $(q_1, \dots, q_M)$ 必须使用完全相同的数位多重集,且它们表示的整数必须满足 $X + Y = Z$。你可以以任何大小写混合的形式输出 YESNO

样例

输入样例 1

3
2 10
3 6
4 5
1 4 3 4
5 12
4 8 8 3 1

输出样例 1

YES
2
1 5
5 1
YES
4
1 4 3 2
3 4 2 1
NO

说明

在第一个测试用例中,$B = 10$ 且 $X = 36$,一个合法的解是 $Y = 15$ 且 $Z = 51$,因为 $51 = 36 + 15$,且两个数都使用了数位集合 $\{1, 5\}$。

在第二个测试用例中,$B = 5$ 且 $X$ 的数位为 $(1, 4, 3, 4)_5$, $$X = 1 \cdot 5^3 + 4 \cdot 5^2 + 3 \cdot 5^1 + 4 = 244$$

一个合法的数对是: $$Y = (1, 4, 3, 2)_5 = 242, \quad Z = (3, 4, 2, 1)_5 = 486$$ 它们共享数位多重集 ${1, 2, 3, 4}$ 且满足 $X + Y = Z$。

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.