#P5657. K-th Gray Code

    ID: 18885 Type: Default 1000ms 256MiB

K-th Gray Code

K-th Gray Code

Given two integers \(n\) and \(k\), where \(n\) is the number of bits and \(k\) is the index (starting from 0) in an \(n\)-bit Gray code sequence generated by the following recursive algorithm:

  1. The 1-bit Gray code is: 0, 1.
  2. For \(n+1\) bits, the first \(2^n\) code words are obtained by prefixing the \(n\)-bit Gray code (in order) with a 0.
  3. The last \(2^n\) code words are obtained by prefixing the \(n\)-bit Gray code (in reverse order) with a 1.

This results in a circular Gray code in which every two consecutive code words (including the first and the last) differ in exactly one bit. In mathematical terms, the Gray code of a number \(k\) can be computed by:

[ G(k) = k \oplus \left(\frac{k}{2}\right) \quad \text{or more precisely} \quad G(k) = k \oplus (k \gg 1), ]

where \(\oplus\) denotes the bitwise XOR operator. The task is to output the \(k\)-th Gray code in binary form, using exactly \(n\) bits (with possible leading zeros).

inputFormat

The input consists of a single line containing two integers \(n\) and \(k\) separated by a space.

\(1 \leq n \leq 32\) and \(0 \leq k < 2^n\).

outputFormat

Output the \(n\)-bit Gray code corresponding to index \(k\). The output should be a binary string of length exactly \(n\) (with leading zeros if necessary).

sample

2 3
10