F. Minimize Fixed Points
Codeforces Round 1034 (Div. 3) F
Problem
Call a permutation of length good if for all . Find a good permutation with the minimum number of fixed points across all good permutations of length . If there are multiple such permutations, print any of them.
- A permutation of length is an array that contains every integer from to exactly once, in any order.
- denotes the greatest common divisor (GCD) of and .
- A fixed point of a permutation is an index () such that .
Input
The first line contains an integer () — the number of test cases.
The only line of each test case contains an integer () — the length of the permutation.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output on a single line an example of a good permutation of length with the minimum number of fixed points.
Solution
Idea
Start by initializing the permutation as the identity permutation, i.e., for all . Then, try to modify it to satisfy the condition.
To achieve this, for each , swap with , where is the smallest prime factor of . For example, if , the smallest prime factor is , so swap with . This ensures that and helps reduce the number of fixed points. The number of fixed points will reduce to the number of elements that are coprime to all other elements in the permutation.