#P5919. [POI 2004] MAK

[POI 2004] MAK

Problem Description

A permutation is a one-to-one function mapping of nn elements p:{1,2,,n}{1,2,,n}p:\{1,2,\ldots,n\}\to\{1,2,\ldots,n\}. The order of a permutation pp is the smallest k1k\ge 1 such that for all i=1,2,,ni=1,2,\ldots,n, the following holds:

p(p((p(i))))=ip(p(\ldots(p(i))\ldots))=i

(kk times in total).

For example, for 33 elements, the permutation with order 22 is p(1)=3,p(2)=2,p(3)=1p(1)=3,p(2)=2,p(3)=1, because p(p(1))=1,p(p(2))=2,p(p(3))=3p(p(1))=1,p(p(2))=2,p(p(3))=3.

For a given nn, we want a permutation of length nn whose order is as large as possible. For example, among permutations of length 55, the maximum possible order is 66.

One example is p(1)=4,p(2)=5,p(3)=2,p(4)=1,p(5)=3p(1)=4,p(2)=5,p(3)=2,p(4)=1,p(5)=3.

Among all permutations that achieve the maximum order, we need to find the lexicographically smallest one.

More precisely, we say permutation pp is smaller than permutation rr if there exists an ii such that for all j<ij<i we have p(j)=r(j)p(j)=r(j) and p(i)<r(i)p(i)<r(i). Then, among permutations of length 55, the smallest one is p(1)=2,p(2)=1,p(3)=4,p(4)=5,p(5)=3p(1)=2,p(2)=1,p(3)=4,p(4)=5,p(5)=3.

Input Format

The first line contains an integer dd, meaning there are dd test cases.

The next dd lines give the lengths n1,n2,,ndn_1,n_2,\ldots,n_d.

Output Format

Output dd lines, each containing one optimal permutation.

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

Hint

For 100%100\% of the testdata, 1d101\le d\le 10 and 1ni1041\le n_i\le 10^4.

Translated by ChatGPT 5