#1081. 格雷码
格雷码
说明
通常,人们习惯将所有n位二进制串按照字典序排列,例如所有2位二进制串按字典序从小到大排列为:00,01,10,11。
格雷码(Gray Code)是一种特殊的n位二进制串排列法,它要求相邻的两个二进制串间恰好有一位不同,特别地,第一个串与最后一个串也算作相邻。
所有2位二进制串按格雷码排列的一个例子为:00,01,11,10。
n位格雷码不止一种,下面给出其中一种格雷码的生成算法:
- 1 位格雷码由两个 1 位二进制串组成,顺序为:0,1。
- <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>+</mo><mn>1</mn></mrow></semantics></math>n+1 位格雷码的前 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个二进制串,可以由依此算法生成的 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位格雷码(总共 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位二进制串)按顺序排列,再在每个串前加一个前缀 0 构成。
- <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>+</mo><mn>1</mn></mrow></semantics></math>n+1 位格雷码的后 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个二进制串,可以由依此算法生成的 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位格雷码(总共 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位二进制串)按逆序排列,再在每个串前加一个前缀 1 构成。
综上,<math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>+</mo><mn>1</mn></mrow></semantics></math>n+1 位格雷码,由 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位格雷码的 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个二进制串按顺序排列再加前缀 0,和按逆序排列再加前缀 1 构成,共 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mi>�</mi><mo>+</mo><mn>1</mn></mrow></msup></mrow></semantics></math>2n+1 个二进制串。另外,对于 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi></mrow></semantics></math>n 位格雷码中的 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>2n 个 二进制串,我们按上述算法得到的排列顺序将它们从 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>∼</mo><msup><mn>2</mn><mi>�</mi></msup><mo>−</mo><mn>1</mn></mrow></semantics></math>0∼2n−1 编号。
按该算法,2位格雷码可以这样推出:
1.已知1位格雷码为0,1。
2.前两个格雷码为00,01。后两个格雷码为11,10。合并得到00,01,11,10,编号依次为0∼3。
同理,3位格雷码可以这样推出:
1.已知2位格雷码为:00,01,11,10。
2.前四个格雷码为:000,001,011,010。后四个格雷码为:110,111,101,100。合并得到:000,001,011,010,110,111,101,100,编号依次为0∼7。
现在给出n,k,请你求出按上述算法生成的n位格雷码中的k号二进制串。
输入格式
仅一行两个整数n,k,意义见题目描述。
输出格式
仅一行一个n位二进制串表示答案。
样例
2 3
10
提示
【样例1解释】
2位格雷码为:00,01,11,10,编号从0∼3,因此3号串是10。
【样例2解释】
3位格雷码为:000,001,011,010,110,111,101,100,编号从0∼7,因此5号串是111。
【数据范围】
对于 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>50</mn><mi mathvariant="normal">%</mi></mrow></semantics></math>50% 的数据:<math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>≤</mo><mn>10</mn></mrow></semantics></math>n≤10
对于 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>80</mn><mi mathvariant="normal">%</mi></mrow></semantics></math>80% 的数据:<math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>≤</mo><mn>5</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>6</mn></msup></mrow></semantics></math>k≤5×106
对于 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>95</mn><mi mathvariant="normal">%</mi></mrow></semantics></math>95% 的数据:<math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>�</mi><mo>≤</mo><msup><mn>2</mn><mn>63</mn></msup><mo>−</mo><mn>1</mn></mrow></semantics></math>k≤263−1
对于 <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>100</mn><mi mathvariant="normal">%</mi></mrow></semantics></math>100% 的数据:<math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>≤</mo><mi>�</mi><mo>≤</mo><mn>64</mn></mrow></semantics></math>1≤n≤64, <math xm<x>lns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>�</mi><mo><</mo><msup><mn>2</mn><mi>�</mi></msup></mrow></semantics></math>0≤k<2n