QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#1854. 哈密顿路径

Statistics

给定一个包含 $n$ 个顶点的有向图,顶点编号从 $0$ 到 $n - 1$。同时给定两个整数 $p$ 和 $q$,满足 $1 \le p, q \le n$。

图的边构造如下:对于每个顶点 $i$, 如果 $i + p < n$,则存在一条从 $i$ 到 $i + p$ 的边; 如果 $i - q \ge 0$,则存在一条从 $i$ 到 $i - q$ 的边。

显然,该图恰好有 $(n - p) + (n - q)$ 条边。

请在该图中寻找任意一条哈密顿路径,或者确定其不存在。 回想一下,哈密顿路径是指访问每个顶点恰好一次的路径。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^4$),表示测试用例的数量。 每个测试用例由一行组成,包含三个整数:$n, p, q$ ($1 \le p, q \le n \le 10^6$)。 保证所有测试用例中 $n$ 的总和不超过 $10^6$。

输出格式

对于每个测试用例,输出一行,包含 $n$ 个整数,表示哈密顿路径中顶点的顺序;如果不存在,则输出 $-1$。 如果存在多个解,输出其中任意一个即可。

样例

输入 1

3
5 3 2
8 2 4
13 5 7

输出 1

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

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#571Editorial Open集训队作业 解题报告 by 陈翰文Qingyu2026-01-02 22:28:40 Download

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.