Editorial for COCI '14 Contest 4 #1 Cesta


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.

The prime factorization of number 30 is 2×3×5. Therefore, N is divisible by 30 if and only if it is divisible by 2, 3 and 5.

We know that a number is divisible by 2 if its last digit is even and it's divisible by 3 when the sum of its digits is divisible by 3 and, naturally, we know that it's divisible by 5 when its last digit is either 0 or 5. By combining these conditions, we conclude that N is divisible by 30 if and only if its last digit is 0 and the sum of its digits is divisible by 3. Therefore, if the number from the input data doesn't contain the digit 0 or the sum of its digits is not divisible by 3, we output -1.

What is obvious is that it is sufficient to sort the digits of number N in descending order to get the required number.

Implementations with time complexity of O(n) or O(nlogn), where n is the number of digits of N, were sufficient to score all points on this task.


Comments

There are no comments at the moment.