#P68. [CSP-S 2019 Day 1] Gray Code

[CSP-S 2019 Day 1] Gray Code

Person in Charge

Problem Description

Typically, people are accustomed to arranging all nn-bit binary strings in lexicographical order. For example, all 2-bit binary strings sorted in ascending lexicographical order are: 0000, 0101, 1010, 1111.

Gray Code is a special way to arrange nn-bit binary strings, which requires that exactly one bit differs between adjacent binary strings. In particular, the first and last strings are also considered adjacent.

An example of 2-bit binary strings arranged in Gray Code order is: 0000, 0101, 1111, 1010.

There is more than one type of nn-bit Gray Code. One algorithm for generating Gray Code is given below:

  1. A 1-bit Gray Code consists of two 1-bit binary strings in the order: 00, 11.
  2. The first 2n2^n binary strings of an (n+1)(n+1)-bit Gray Code can be formed by taking the nn-bit Gray Code (a total of 2n2^n nn-bit binary strings) generated by this algorithm, arranging them in order, and prepending a prefix 00 to each string.
  3. The last 2n2^n binary strings of an (n+1)(n+1)-bit Gray Code can be formed by taking the nn-bit Gray Code (a total of 2n2^n nn-bit binary strings) generated by this algorithm, arranging them in reverse order, and prepending a prefix 11 to each string.

In summary, an (n+1)(n+1)-bit Gray Code consists of the 2n2^n binary strings of the nn-bit Gray Code arranged in order with a prefix 00 added, followed by the same 2n2^n strings arranged in reverse order with a prefix 11 added, totaling 2n+12^{n+1} binary strings. Additionally, the 2n2^n binary strings in the nn-bit Gray Code are numbered from 02n10 \sim 2^n - 1 according to the order obtained by the above algorithm.

Using this algorithm, the 2-bit Gray Code can be derived as follows:

  1. The 1-bit Gray Code is known to be 00, 11.
  2. The first two Gray Code strings are 0000, 0101. The last two are 1111, 1010. Combining them gives 0000, 0101, 1111, 1010, numbered from 00 to 33 in sequence.

Similarly, the 3-bit Gray Code can be derived as follows:

  1. The 2-bit Gray Code is known to be: 0000, 0101, 1111, 1010.
  2. The first four Gray Code strings are: 000000, 001001, 011011, 010010. The last four are: 110110, 111111, 101101, 100100. Combining them gives: 000000, 001001, 011011, 010010, 110110, 111111, 101101, 100100, numbered from 070 \sim 7 in sequence.

Given nn and kk, please find the kk-th binary string in the nn-bit Gray Code generated by the above algorithm.

Input Format

A single line containing two integers nn and kk, with meanings as described in the problem statement.

Output Format

A single line containing an nn-bit binary string representing the answer.

Samples

2 3
10
3 5
111

Sample 1 Explanation

The 2-bit Gray Code is: 0000, 0101, 1111, 1010, numbered from 030 \sim 3. Therefore, the 3rd string is 1010.

Sample 2 Explanation

The 3-bit Gray Code is: 000000, 001001, 011011, 010010, 110110, 111111, 101101, 100100, numbered from 070 \sim 7. Therefore, the 5th string is 111111.

Sample 3

See code/code3.in and code/code3.ans in the contestant's directory.

Data Range

  • For 50%50\% of the data: n10n \le 10.
  • For 80%80\% of the data: k5×106k \le 5 \times 10^6.
  • For 95%95\% of the data: k2631k \le 2^{63} - 1.
  • For 100%100\% of the data: 1n641 \le n \le 64, 0k<2n0 \le k \lt 2^n.