DMOPC '21 Contest 5 P1 - Permutations & Primes

View as PDF

Submit solution


Points: 5 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

At the Phoenix Wonderland carnival, you found a mysterious booth that proposes an interesting challenge. The operator (some strange floating nuigurumi) will give you an integer N, and you are to find any permutation p_1, p_2, \dots, p_N of 1, 2, \dots, N wherein every adjacent sum in the permutation is non-prime. That is, for all 1 \le i < N, we must have p_i + p_{i + 1} be non-prime. Can you find any such permutation, or determine that none exists?

Constraints

1 \le N \le 10^6

Subtask 1 [10%]

1 \le N \le 4

Subtask 2 [20%]

1 \le N \le 10

Subtask 3 [30%]

1 \le N \le 3 \times 10^3

Subtask 4 [40%]

No additional constraints.

Input Specification

The first and only line of input contains a single integer N.

Output Specification

If there exists no valid permutation, output -1 on a line by itself.

Otherwise, output N space-separated integers on a single line, representing a permutation p_1, p_2, \dots, p_N of 1, 2, \dots, N wherein every adjacent sum in the permutation is non-prime.

Sample Input 1

7

Sample Output 1

7 2 4 6 3 5 1

Explanation for Sample 1

Note that the adjacent pairs have sums of 9, 6, 10, 9, 8, 6, none of which are prime.

Sample Input 2

2

Sample Output 2

-1

Explanation for Sample 2

Note that 1 + 2 = 3 which is prime, so regardless of order the sum of the one pair is prime.


Comments

There are no comments at the moment.