F: Base-2 Palindromes
https://open.kattis.com/problems/base2palindrome
Last updated
https://open.kattis.com/problems/base2palindrome
Last updated
The key observation in this problem is that a palindrome is uniquely defined by the first half bits. Thus, the number of palindromes of length () with the first bit is .
We can figure out the most significant bit of the palindrome by counting the number of palindromes of each length until the total number of palindromes we have counted so far exceeds .
Suppose the most significant bit of the palindrome is the bit, and suppose there are palindromes of length at most . Then, we are interested in the palindrome of length .
Let denote the binary representation of . Let denote the string reversed. Now, we know that the answer is (expect if is odd, in which case we remove one bit from ).
For instance, suppose . If is even, the answer is . If b is odd, the answer is .
Finally, convert the binary string to an integer.