#P5770. Unbounded Words
Unbounded Words
Unbounded Words
Consider a word \(S\) consisting of letters. We say \(S\) is bounded if there exists an integer \(l\) such that \(0 < l < |S|\) and the prefix of \(S\) of length \(l\) is equal to the suffix of \(S\) of length \(l\). Otherwise, \(S\) is called unbounded.
For example, the strings aabaa
and ababab
are bounded. Now, consider all strings of length \(N\) consisting only of the letters a
and b
. Your task is to answer the following questions:
- How many unbounded words are there among these \(2^N\) strings?
- What is the \(K\)th smallest unbounded word when the unbounded words are arranged in lexicographical order?
Note: All formulas are given in LaTeX format.
inputFormat
The input consists of a single line containing two integers \(N\) and \(K\):
- \(N\) — the length of the string (\(N \ge 1\)).
- \(K\) — the 1-indexed order of the desired unbounded word.
You can assume that \(K\) is valid (i.e. \(K\) does not exceed the total number of unbounded words).
outputFormat
Output two lines:
- The first line contains the total number of unbounded words of length \(N\).
- The second line contains the \(K\)th smallest unbounded word in lexicographical order.
sample
3 1
4
aab
</p>