内容中心

2026年比较好的扎带口碑好的厂家推荐-浙江力泰塑料有限公司

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:

  1. Derangement: Each element is shifted right by one, so no element remains in its original position.
  2. 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 -1 as 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.

浙江力泰塑料有限公司

浙江力泰塑料有限公司



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

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?