PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000085. Bậc hoán vị lớn nhất (Maximal Order of Permutations)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn, consider a permutation π\pi of 1,2,…,n1,2,\ldots,n, where π(i)\pi(i) is the image of element ii. The order of π\pi is the smallest positive integer kk for which applying π\pi exactly kk times returns every element to itself: πk(i)=i\pi^k(i)=i for all ii.

Among all permutations of nn 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 nn numbers π(1),…,π(n)\pi(1),\ldots,\pi(n), containing each integer from 11 to nn 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 nn in one input. This version processes a single nn, while preserving the optimization objective and lexicographic tie-break.

Input

One line contains the integer nn. There is no leading test-case count dd.

Output

Print one line with exactly nn distinct integers from 11 through nn, describing the lexicographically smallest permutation among those with maximum order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30.

  • Subtask 2 (30 points): 1≤n≤7001\le n\le700.

  • Subtask 3 (50 points): 1≤n≤1041\le n\le10^4.

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.