#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 -bit binary strings in lexicographical order. For example, all 2-bit binary strings sorted in ascending lexicographical order are: , , , .
Gray Code is a special way to arrange -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: , , , .
There is more than one type of -bit Gray Code. One algorithm for generating Gray Code is given below:
- A 1-bit Gray Code consists of two 1-bit binary strings in the order: , .
- The first binary strings of an -bit Gray Code can be formed by taking the -bit Gray Code (a total of -bit binary strings) generated by this algorithm, arranging them in order, and prepending a prefix to each string.
- The last binary strings of an -bit Gray Code can be formed by taking the -bit Gray Code (a total of -bit binary strings) generated by this algorithm, arranging them in reverse order, and prepending a prefix to each string.
In summary, an -bit Gray Code consists of the binary strings of the -bit Gray Code arranged in order with a prefix added, followed by the same strings arranged in reverse order with a prefix added, totaling binary strings. Additionally, the binary strings in the -bit Gray Code are numbered from according to the order obtained by the above algorithm.
Using this algorithm, the 2-bit Gray Code can be derived as follows:
- The 1-bit Gray Code is known to be , .
- The first two Gray Code strings are , . The last two are , . Combining them gives , , , , numbered from to in sequence.
Similarly, the 3-bit Gray Code can be derived as follows:
- The 2-bit Gray Code is known to be: , , , .
- The first four Gray Code strings are: , , , . The last four are: , , , . Combining them gives: , , , , , , , , numbered from in sequence.
Given and , please find the -th binary string in the -bit Gray Code generated by the above algorithm.
Input Format
A single line containing two integers and , with meanings as described in the problem statement.
Output Format
A single line containing an -bit binary string representing the answer.
Samples
2 3
10
3 5
111
Sample 1 Explanation
The 2-bit Gray Code is: , , , , numbered from . Therefore, the 3rd string is .
Sample 2 Explanation
The 3-bit Gray Code is: , , , , , , , , numbered from . Therefore, the 5th string is .
Sample 3
See code/code3.in and code/code3.ans in the contestant's directory.
Data Range
- For of the data: .
- For of the data: .
- For of the data: .
- For of the data: , .