QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100 Difficulty: [show]

#2007. Gray Code

Statistics

Usually, people are accustomed to arranging all $n$-bit binary strings in lexicographical order. For example, all 2-bit binary strings arranged in ascending lexicographical order are: 00, 01, 10, 11.

Gray Code is a special method of arranging $n$-bit binary strings, which requires that any two adjacent binary strings differ by exactly one bit. Specifically, the first string and the last string are also considered adjacent.

An example of all 2-bit binary strings arranged in Gray Code order is: 00, 01, 11, 10.

There is more than one type of $n$-bit Gray Code. Below is a generation algorithm for one such Gray Code:

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

In summary, an $(n+1)$-bit Gray Code consists of the $2^n$ binary strings of an $n$-bit Gray Code arranged in order with a prefix of 0, followed by the same strings arranged in reverse order with a prefix of 1, totaling $2^{n+1}$ binary strings. Additionally, for the $2^n$ binary strings in an $n$-bit Gray Code, we number them from $0 \sim 2^n - 1$ based on the order obtained by the above algorithm.

Following this algorithm, a 2-bit Gray Code can be derived as follows: 1. Known 1-bit Gray Code: 0, 1. 2. The first two Gray codes are 00, 01. The last two Gray codes are 11, 10. Combining them gives 00, 01, 11, 10, numbered $0 \sim 3$ respectively.

Similarly, a 3-bit Gray Code can be derived as follows: 1. Known 2-bit Gray Code: 00, 01, 11, 10. 2. The first four Gray codes are: 000, 001, 011, 010. The last four Gray codes are: 110, 111, 101, 100. Combining them gives: 000, 001, 011, 010, 110, 111, 101, 100, numbered $0 \sim 7$ respectively.

Given $n$ and $k$, please find the $k$-th binary string in the $n$-bit Gray Code generated by the above algorithm.

Input

A single line containing two integers $n$ and $k$, with meanings as described in the problem statement.

Output

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

Examples

Input 1

2 3

Output 1

10

Note 1

The 2-bit Gray Code is: 00, 01, 11, 10, numbered $0 \sim 3$, so the string at index 3 is 10.

Input 2

3 5

Output 2

111

Note 2

The 3-bit Gray Code is: 000, 001, 011, 010, 110, 111, 101, 100, numbered $0 \sim 7$, so the string at index 5 is 111.

Input 3

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

Constraints

For 50% of the data: $n \le 10$ For 80% of the data: $k \le 5 \times 10^6$ For 95% of the data: $k \le 2^{63} - 1$ For 100% of the data: $1 \le n \le 64$, $0 \le k < 2^n$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.