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 , we can simply hard-code each test case.
:
Since is the only permutation for , the valid solution would just be .
:
The only permutations for would be and . Since the sum of and is , which is prime, there is no valid solution.
:
Since the adjacent sum of and is , which is prime, we must keep the and apart. To keep and away from each other, we must put in the middle, however this will not work as will always be adjacent to , with a prime sum of . Therefore there is no valid solution.
:
In any permutation for , a pair of adjacent even-odd numbers will always exist.
Since when , any even-odd pair will have a prime sum , there is no valid solution for .
Time Complexity:
Subtask 2
We can simply brute-force each permutation of and determine whether or not a valid solution exists.
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 and next to each other which results in the adjacent sum of , which is the first odd composite integer.
This approach works for .
We can reuse subtask 1 solutions for .
Time Complexity:
Comments