To solve this problem, we need to find a permutation of students such that each student's ID is not in their original position (derangement) and adjacent students have coprime IDs.
Approach
The key insight here is that a cyclic shift of the sequence [1, 2, ..., n] (i.e., [2, 3, ..., n, 1]) satisfies both conditions for all n ≥ 2:
- Derangement: Each element is shifted right by one, so no element remains in its original position.
- Coprime Adjacent IDs: Consecutive numbers are coprime, and the last element (1) and first element (2) are also coprime.
For n = 1, it's impossible to form such a permutation since the only permutation is [1], which violates the derangement condition.
Solution Code
n = int(input())
if n == 1:
print(-1)
else:
print(' '.join(map(str, range(2, n+1))) + ' 1')
Explanation
- Case n = 1: Output
-1as no valid permutation exists. - Case n ≥ 2: The permutation [2, 3, ..., n, 1] is a valid solution. This sequence is a cyclic shift of the original sequence, ensuring all elements are deranged and adjacent elements are coprime.
This approach efficiently constructs the required permutation in O(n) time, which is optimal for the problem constraints. The solution is straightforward and leverages the properties of consecutive numbers and cyclic shifts to meet the problem's requirements.


作者声明:本文包含人工智能生成内容。