#P5770. [JSOI2016] 无界单词

[JSOI2016] 无界单词

Description

For a word SS, if there exists a length ll such that 0<l<S0 < l < |S|, and the prefix of SS with length ll is the same as the suffix of SS with length ll, then JYY calls SS bordered. For example, aabaa and ababab are both bordered strings. If a word does not have such an ll, then JYY calls it an unbordered word.

Now consider all strings of length NN that consist only of the letters a and b. JYY wants to know:

  1. How many unbordered words are there in total?
  2. Among these unbordered words, when sorted in lexicographical order, what is the KK-th smallest word?

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

The next TT lines each contain two positive integers NN and KK, describing one test case.

Output Format

For each test case, output two lines.

The first line contains an integer, the number of unbordered words of length NN.

The second line contains a string of length NN, representing the KK-th smallest unbordered word.

5
5 1
5 2
5 3
5 4
5 5

12
aaaab
12
aaabb
12
aabab
12
aabbb
12
ababb

Hint

For 20%20\% of the testdata, N20N \le 20.

For all testdata, 1T51 \le T \le 5, 1N641 \le N \le 64, and it is guaranteed that for every test case, the KK-th smallest unbordered word always exists.

Translated by ChatGPT 5