QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 512 MB Total points: 100

#11722. 哈密顿

Statistics

Lin-Manuel 正在一条由 $n$ 个连续编号为 $1$ 到 $n$ 的格子组成的带状区域上移动。 他从格子 $a$ 出发,想要在格子 $b$ 结束。在此过程中,他希望恰好访问每个格子一次。 从任何格子 $x$ 出发,Lin-Manuel 可以走到左侧相邻的格子 $x - 1$(如果存在),或者右侧相邻的格子 $x + 1$(如果存在)。 他还可以呼唤他的朋友,女巫 Miranda,她会赋予他一种魔法。有了这种魔法,他将能够从当前格子 $x$ 飞行一次到任何满足 $\gcd(x, y) = 1$ 的格子 $y$。 Lin-Manuel 不想给 Miranda 增加太多负担。因此,他希望通过尽可能少的飞行次数来实现他的目标。 请帮助他找到所需的最少飞行次数,以及访问格子的最优序列。

输入格式

输入的第一行包含一个整数 $t$ ($1 \le t \le 10^3$),表示测试用例的数量。 接下来的 $t$ 行,每行包含三个整数 $n, a, b$ ($2 \le n \le 2 \cdot 10^5; 1 \le a, b \le n; a \neq b$),分别表示带状区域的格子数量、起始格子和结束格子。 所有 $n$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,如果无论飞行多少次都无法实现目标,输出一个整数 $-1$。 否则,输出从格子 $a$ 到格子 $b$ 且恰好访问每个格子一次所需的最少飞行次数,随后输出 $n$ 个不同的整数 $c_1, c_2, \dots, c_n$ ($1 \le c_i \le n$),表示访问格子的顺序,描述任何需要最少飞行次数的有效路径。特别地,必须满足 $c_1 = a$ 且 $c_n = b$。

样例

输入 1

4
5 1 5
6 4 5
7 5 3
4 1 3

输出 1

0
1 2 3 4 5
1
4 3 2 1 6 5
2
5 4 7 6 1 2 3
-1

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.