QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#14557. Permutation Cycles

Statistics

A permutation of length $n$ is a sequence containing every integer $x$ such that $1 \le x \le n$ exactly once.

For a permutation $A = \{a_i\}$, we define its number of cycle components $R(A)$ as the number of connected components in the undirected graph $G = \{V, E\}$, where $V = \{i \mid 1 \le i \le n\}$ and $E = \{(i, a_i) \mid 1 \le i \le n\}$.

The weight $V(A)$ of a permutation $A = \{a_i\}$ is defined as the sum of the number of cycle components of the $n$ permutations that are cyclically equivalent to $A$. Specifically, two permutations $P$ and $Q$ are cyclically equivalent if and only if $P$ can be transformed into $Q$ through a series of right-shift operations (i.e., moving the last element of $P$ to the front of $P$).

For example, the weight of $\{1, 2, 3, 4\}$ is the sum of the number of cycle components of $\{1, 2, 3, 4\}, \{4, 1, 2, 3\}, \{3, 4, 1, 2\}, \{2, 3, 4, 1\}$, which is $4 + 1 + 2 + 1 = 8$.

Given an integer $n$, you need to find a permutation $A$ of length $n$ that maximizes its weight $V(A)$. You must output the value of $V(A)$ and the permutation $A$.

Input

A single integer $n$ ($1 \le n \le 10^3$).

Output

Output the maximum weight on the first line. On the second line, output the $n$ integers of the permutation separated by spaces. If there are multiple solutions, you may output any one of them.

Examples

Input 1

3

Output 1

6
1 3 2

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.