Palindromic Sequence
Time limit20sMemory limit1024 MB
Given L letters, a length limit N, and K, find the length of the Kth smallest palindrome in lexicographic order, or 0 if none exists.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math, Binary search
- Solved
- No attempts yet
Problem
Hannah is working on a new language made only of the first lowercase letters of the English alphabet. She is obsessed with palindromes, which are words that read the same forward and backward, such as hannah and civic. She has written down every word in her language whose length is at most and that is also a palindrome.
Find the length of the th smallest palindromic word among all the words she wrote, in lexicographic order. A word is lexicographically smaller than if at the first index where the two words differ. A prefix of a word is smaller than the word itself. For example, these words are in lexicographically increasing order: a, aa, aba, cabac, d.
Input
The first line contains the number of test cases . Each of the next lines contains three integers , , and , as described above.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the length of the lexicographically th smallest palindromic word among all palindromic words of length at most in Hannah's language. If no such word exists, output 0.