Editorial for COCI '07 Contest 5 #2 Pascal
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Directly implementing the given algorithm is too slow.
A more efficient approach is to realize what the given program does: it calculates the difference between and its largest divisor. We can find the largest divisor of by dividing by its smallest divisor greater than .
If is prime, then the solution is . Otherwise, its smallest divisor is at most so we can use trial division to find it.
Comments