#P5770. Unbounded Words

    ID: 18998 Type: Default 1000ms 256MiB

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:

  1. How many unbounded words are there among these \(2^N\) strings?
  2. 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\):

  1. \(N\) — the length of the string (\(N \ge 1\)).
  2. \(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:

  1. The first line contains the total number of unbounded words of length \(N\).
  2. The second line contains the \(K\)th smallest unbounded word in lexicographical order.

sample

3 1
4

aab

</p>