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 p1,p2,,pN of 1,2,,N wherein every adjacent sum in the permutation is non-prime. That is, for all 1i<N, we must have pi+pi+1 be non-prime. Can you find any such permutation, or determine that none exists?

Constraints

1N106

Subtask 1 [10%]

1N4

Subtask 2 [20%]

1N10

Subtask 3 [30%]

1N3×103

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 p1,p2,,pN of 1,2,,N wherein every adjacent sum in the permutation is non-prime.

Sample Input 1

Copy
7

Sample Output 1

Copy
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

Copy
2

Sample Output 2

Copy
-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.