Editorial for COCI '19 Contest 1 #2 Lutrija
Submitting an official solution before solving the problem yourself is a bannable offence.
For , it was enough to first check whether is prime. If it is, then the array could simply be . If it isn't prime, we can check for every if is prime and is prime and is prime. If all three hold, then the sought array is . Otherwise, there is no solution.
To obtain the full score, it was enough to note that the absolute difference between two prime numbers greater than must be an even number. Since the absolute difference between each successive pair must be prime, we can conclude that the only numbers that could appear in the array are , , , , , and . Out of those seven numbers, we will leave only the prime ones. Now, if we represent each number as a node in a graph and connect the two nodes if their absolute difference is prime, the task boils down to finding a path between nodes and . This is a standard problem which we can solve using BFS.
Comments