DMOPC '21 Contest 5 P5 - Permutations & Primes (Hard Version)

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 5.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

1N105

Subtask 1 [30%]

1N103

Subtask 2 [30%]

1N104

Subtask 3 [40%]

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


  • 8
    TechnobladeNeverDies  commented on Jan. 20, 2022, 12:32 a.m.

    petition to ban changes in problem constraints after a problem has at least one AC submission