DMOPC '21 Contest 5 P5 - Permutations & Primes

View as PDF

Submit solution


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

Author:
Problem type

The primeness of any permutation p1,p2,,pN of 1,2,,N is defined as the sum of all prime absolute differences of consecutive elements. Given an integer N, find any permutation p1,p2,,pN of 1,2,,N with the maximum primeness over all length N permutations.

Constraints

1N100

Subtask 1 [20%]

1N20

Subtask 2 [60%]

1N50

Subtask 3 [20%]

No additional constraints.

Input Specification

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

Output Specification

Output N space-separated integers on a single line, representing a permutation p1,p2,,pN of 1,2,,N with the maximum primeness over all length N permutations.

Sample Input 1

Copy
3

Sample Output 1

Copy
2 3 1

Explanation for Sample 1

There are 2 absolute differences of consecutive elements, namely |23|=1 and |31|=2. Out of these, only 2 is prime, so the primeness of this permutation is 2.

Sample Input 2

Copy
6

Sample Output 2

Copy
3 6 1 4 2 5

Explanation for Sample 2

The absolute differences of consecutive elements are 3,5,3,2,3. All of these are prime, so the primeness of this permutation is 3+5+3+2+3=16.


Comments

There are no comments at the moment.