QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 64 MB 總分: 100

#11887. 置换的极大阶

统计

$n$ 个元素的排列是一个一一映射(单射) $\pi:\{1,2, \ldots ,n\} \mapsto \{1,2, \ldots ,n\}$。排列 $\pi$ 的阶是满足对于所有 $i=1,2, \ldots ,n$ 都有:

$$\pi(\pi(\ldots(\pi(i))\ldots))=i$$

的最小正整数 $k \ge 1$,即 $\pi$ 自身复合 $k$ 次后成为恒等函数。例如,对于 $3$ 个元素的排列 $\pi(1)=3, \pi(2)=2, \pi(3)=1$,其阶为 $2$,因为 $\pi(\pi(1))=1, \pi(\pi(2))=2, \pi(\pi(3))=3$。

对于给定的 $n$,我们考虑具有最大阶的 $n$ 元素排列。例如,$5$ 个元素排列的最大阶为 $6$。一个阶为 $6$ 的 $5$ 元素排列示例为 $\pi(1)=4, \pi(2)=5, \pi(3)=2, \pi(4)=1, \pi(5)=3$。

在所有具有最大阶的 $n$ 元素排列中,我们希望找到字典序最小的一个。更准确地说,如果存在某个 $i$,使得对于所有 $j < i$ 都有 $\pi(j)= \sigma(j)$ 且 $\pi(i) < \sigma(i)$,则称排列 $\pi$ 比排列 $\sigma$ 更靠前(字典序更小)。$5$ 个元素中阶为 $6$ 的字典序最小的排列是 $\pi(1)=2, \pi(2)=1, \pi(3)=4, \pi(4)=5, \pi(5)=3$。

实现细节

编写一个程序:

  • 从标准输入读取一组整数 $n_1,n_2,\ldots,n_d$,
  • 对于每个数字 $n_i$(其中 $i=1,2,\ldots ,d$),确定具有最大阶的 $n_i$ 元素字典序最小排列,
  • 将确定的排列写入标准输出。

输入格式

标准输入的第一行包含一个正整数 $d$,$1 \le d \le 10$。接下来的 $d$ 行每行包含一个正整数 $n_1,n_2,\ldots ,n_d$,$1 \le n_i \le 10\,000$。

输出格式

你的程序应向标准输出写入 $d$ 行。第 $i$ 行应包含由空格分隔的整数序列,构成 $n_i$ 元素中具有最大阶的字典序最小排列,即序列 $\pi(1),\pi(2),\ldots ,\pi(n_i)$,其中 $\pi$ 表示该排列。

样例

输入 1

2
5
14

输出 1

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

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.