Editorial for COCI '19 Contest 1 #2 Lutrija


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For 20\%, it was enough to first check whether |A-B| is prime. If it is, then the array could simply be \{A, B\}. If it isn't prime, we can check for every 2 \le x \le 1000 if x is prime and |A-x| is prime and |x-B| is prime. If all three hold, then the sought array is \{A, x, B\}. 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 2 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 A-2, A, A+2, B-2, B, B+2 and 2. 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 A and B. This is a standard problem which we can solve using BFS.


Comments

There are no comments at the moment.