Editorial for Baltic OI '13 P2 - Palindrome-Free Numbers
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 to . 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 .
For the full-score solution, the key observation is that if a number contains a palindrome of length (), 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, contains and contains . 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 and to use long long
s.
Comments