#P16655. [GKS 2018 #F] Palindromic Sequence

[GKS 2018 #F] Palindromic Sequence

Problem Description

Hannah is working on a new language which consists only of first LL lowercase letters of the English alphabet. She is obsessed with palindromes, which are words that read the same forward and backward, e.g hannah and civic. She has written down all of the words in her language of length at most NN, that are also palindromes.

Now, she is interested in finding the length of the word that is lexicographically KthK^{th} smallest among all the words she has written. A word composed of ordered letters a1a_1, a2a_2, ..., apa_p is lexicographically smaller than word b1b_1, b2b_2, ..., bqb_q if ai<bia_i < b_i, where i is the first index where characters differ in the two words. Also, a prefix of a word is considered lexicographically smaller than the word itself. For example, the following words are arranged in lexicographically increasing order: a, aa, aba, cabac, d.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of one line containing three integers L, N, and K, as described above.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the length of the lexicographically KthK^{th} smallest palindromic word among all palindromic words of length at most NN in Hannah's language. If no such word exists, output 00.

2
2 3 4
2 3 9
Case #1: 3
Case #2: 0

Hint

In Sample Cases #1 and #2, Hannah's language consists only of the letters a and b. All the palindromic words of length at most 33 in her language, in lexicographic order, are: aa, aaaa, aaaaaa, abaaba, bb, babbab, bbbb and bbbbbb.

In Sample Case #1, the fourth-smallest word is abaaba, which is 33 characters long, so we output 33.

In Sample Case #2, KK exceeds the total number of possible words, and hence we output 00.

Limits

1T1001 \le T \le 100.

1L261 \le L \le 26.

1K10121 \le K \le 10^{12}.

Small dataset (Test set 1 - Visible)

1N1001 \le N \le 100.

Large dataset (Test set 2 - Hidden)

1N10121 \le N \le 10^{12}.