Editorial for Baltic OI '13 P2 - Palindrome-Free Numbers


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 the first subtask, one only needs to brute force the solutions from a to b. For each number, we check whether it is palindrome free and if it is, we just add one to a counter variable. The checking can be done quite easily in quadratic time depending on the number of digits by trying all the mid positions of possible palindromes. This gives us a solution in \mathcal O(18^2 \times (b-a)).

For the full-score solution, the key observation is that if a number contains a palindrome of length k (k \ge 2), it contains a palindrome of length two or three (just take the middle of the found palindrome and depending on whether it is odd or not, you take the middle and the left and right of the middle). For example, 12\,323 contains 232 and 1\,221 contains 22. With this information, it is quite easy to do dynamic programming by saving the last two positions, and the length of the number, in order to get the number of palindrome-free numbers up to a certain number. A common mistake was to forget to consider 0 and to use long longs.


Comments

There are no comments at the moment.