#QHD0000085. Bậc hoán vị lớn nhất (Maximal Order of Permutations)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bậc hoán vị lớn nhất (Maximal Order of Permutations)
Maximal Order of Permutations
Source: POI
Version: Phuoc Hung OJ Extended
Problem Statement
Given a positive integer , consider a permutation of , where is the image of element . The order of is the smallest positive integer for which applying exactly times returns every element to itself: for all .
Among all permutations of elements, consider those with the largest possible order. If multiple permutations achieve the largest order, choose the lexicographically smallest one: at the first position where two permutations differ, the permutation with the smaller value is preferred. You must not output an arbitrary maximum-order permutation.
Output exactly numbers , containing each integer from to exactly once. The order of a permutation equals the least common multiple of its disjoint cycle lengths.
PHOJ adaptation: The original problem allows several independent values of in one input. This version processes a single , while preserving the optimization objective and lexicographic tie-break.
Input
One line contains the integer . There is no leading test-case count .
Output
Print one line with exactly distinct integers from through , describing the lexicographically smallest permutation among those with maximum order.
Subtasks
-
Subtask 1 (20 points): .
-
Subtask 2 (30 points): .
-
Subtask 3 (50 points): .
Examples
Example 1
Input
5
Output
2 1 4 5 3
Explanation: The two cycles have lengths 2 and 3, yielding LCM 6. The displayed permutation is lexicographically smallest among those reaching maximum order.
Example 2
Input
14
Output
2 3 1 5 6 7 4 9 10 11 12 13 14 8
Explanation: The consecutive cycles have lengths 3, 4 and 7, yielding LCM 84. This is the smallest lexicographic maximum-order permutation.