Editorial for DMOPC '21 Contest 5 P1 - Permutations & Primes
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
,Subtask 1
Since
Since
The only permutations for
Since the adjacent sum of
In any permutation for
Since when
Time Complexity:
Subtask 2
We can simply brute-force each permutation of
Time Complexity:
Subtask 3
This subtask was left for inefficiencies in the implementation of the full solution or other solutions.
Full Solution
To guarantee that every adjacent sum in the permutation is non-prime, we can make all of them but the middle one even. To do this, we rearrange the numbers such that all even numbers are on one half of the permutation, and all odd numbers are on the other. This works since the sum of two even numbers or two odd numbers will always be even.
For the even-odd pair, we place
This approach works for
We can reuse subtask 1 solutions for
Time Complexity:
Comments