If garbled topic, please click on the address to access the original title.
Title Description
Typically, people are accustomed to all the n- n-bit binary string as lexicographic order, for example, all the 2-bit binary strings are lexicographically in ascending order are: 00,01,10,11.
Gray code (Gray Code) is a special n- n-bit binary string permutation method, which requires between two binary strings exactly adjacent to a different, in particular, a first string and a string to be regarded as the last adjacent .
All 2-bit binary strings are arranged in a gray code example is: 00,01,11,10.
n- n-bit Gray code is more than one algorithm is given below wherein generating Gray code:
- A Gray code of a binary string composed of two, in order: 0,1.
- n-+. 1 n- + . 1-bit Gray code before 2 ^ n- 2 n-binary string or so the algorithm generated by the n- n-bit Gray code (a total of 2 ^ n- 2 n-a n- n-bit binary string) are arranged in order, and then before each string composed of 0 and a prefix.
- n-+. 1 n- + After a Gray code 2 ^ n- 2 n-binary string, it may be formed so algorithm generates n- n-bit Gray code (a total of 2 ^ n- 2 n-a n- n-bit binary string) in reverse order, and then before each string constituting a prefix 1.
In summary, n-1 + n- + 1-bit Gray code, the n- n-bit Gray code 2 ^ n- 2 are arranged in order prefix plus 0 n binary string, and in reverse order plus a prefix composed of a total of 2 ^ { } +. 1 n- 2 n- + . 1 binary string. Further, the n- n-bit Gray code in 2 ^ n- 2 n-binary strings, the order of the above-described algorithm we get them from 0 \ 2 ^ n-SIM -. 1 0 ~ 2 n- - . 1 ID.
According to the algorithm, two Gray code can launch this:
- Gray code is known as a 0.
- The first two Gray code to 00, 01. After two Gray code is 11,10. Were combined to give 00,01,11,10, numbered sequentially from 0 to 3.
Similarly, three Gray code can launch this:
- Gray code is known as 2: 00,01,11,10.
- The first four Gray code is: 000,001,011,010. After four Gray code is: 110,111,101,100. Combined to give: 000,001,011,010,110,111,101,100, sequentially numbered from 0 to 7.
Now given n- n-, K K, you obtain the above-described algorithm for generating the n- n-bit Gray code in K K number of binary string.
Input Format
Only one line two integers the n- the n-, k k, meaning see title description.
Output Format
Only a row of the n- the n-bit binary string represents the answer.
Sample input and output
2 3
10
3 5
111
44 1145141919810
00011000111111010000001001001000000001100011
Description / Tips
Sample 1 [explain]
Gray code 2: 00,01,11,10, numbered from 0~3, string 3 is therefore 10.
Sample 2 [explain]
3 Gray code: 000,001,011,010,110,111,101,100, numbered from 0 to 7, so the string 5 is 111.
【data range】
For 50 \% . 5 0 % Data: n-\ Leq 10 n- ≤ . 1 0
For 80 \% . 8 0 % Data: K \ Leq. 5 \ ^. 6 Times 10 K ≤ . 5 × . 1 0 . 6
For 95 \% . 9 . 5 % of the data: K \ Leq 63 is {2} ^ -. 1 K ≤ 2 . 6 . 3 - . 1
For 100 \% . 1 0 0 % data: . 1 \ n-Leq \ Leq 64 . 1 ≤ n- ≤ . 6 . 4, 0 \ Leq K \ ^ lt n-2 0 ≤ K < 2 n-
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; unsigned long long maxx=1; int n,where=1; bool ok; unsigned long long k; bool a[10000]; int main () { cin>>n>>k; for(int i=1;i<n;i++){ maxx*=2; } ok=1; while(maxx>0){ if(ok==1){ //cout<<maxx<<" "<<k<<endl; if(maxx>k){ a[where++]=0; } else{ k-=maxx; a[where++]=1; ok=0; } maxx/=2; continue; } if(ok==0){ //cout<<maxx<<" "<<k<<endl; if(maxx>k){ a[where++]=1; ok=1; } else{ k-=maxx; a[where++]=0; } maxx/=2; continue; } } for(int i=1;i<=n;i++){ printf("%d",a[i]); } return 0; }