[CSP-S 2019] Golay code

[CSP-S 2019] Golay code

Subject to the effect:

Gray code (Gray Code) is a special \ (n-\) bit binary sequence permutation method, which requires between two binary strings adjacent to just have a different , in particular, a first string and a string can be considered the last as neighbors.

\ (n-\) bit Gray code is more than one algorithm is given below wherein generating Gray code:

  1. \ (1 \) bit gray code by the two \ (1 \) bit binary string composed of, in order: \ (0 \) , \ (1 \) .
  2. \ (n + 1 \) before the bit Gray code \ (2 ^ n \) binary string, may be formed so algorithmically generated \ (n-\) (total bit Gray code \ (2 ^ n \) a \ (n- \) bit binary string) according to the order of arrangement, and then add a prefix in front of each string \ (0 \) configuration.
  3. \ (n + 1 \) after bit Gray code \ (2 ^ n \) binary string, may be formed so algorithmically generated \ (n-\) bit Gray code (total \ (2 ^ n \) a \ (n- \) bit binary string) by reverse order, and then add a prefix in front of each string \ (1 \) configuration.

In summary, \ (n-+. 1 \) bit Gray code by \ (n-\) bit Gray code \ (2 ^ n \) binary string in sequence plus prefix \ (0 \) , and in reverse order coupled with the prefix \ (1 \) configuration, co \ (2 ^ {n + 1 } \) binary string. Further, for the \ (n-\) bit Gray code in \ (2 ^ n \) binary sequence, we get the order them from the above-described algorithm \ (0 \ sim 2 ^ n - 1 \) number.

Now given \ (n-\) , \ (K \) , you obtain the above-described algorithm generated \ (n-\) bit Gray code in \ (K \) resolution binary string.

\ (1 \ n \ 64, 0 <k <2 ^ n \)

Ideas:

From \ (n-1 \) to \ (0 \) is determined for each one. If \ (K \) is the current bit is \ (1 \) , the corresponding bit of the Gray code is also \ (1 \) , and \ (K \) remaining untreated bit inversion; if \ ( K \) is the current bit is \ (1 \) , do the corresponding to various types is also \ (0 \) is not inverted.

Source:

#include<cstdio>
#include<cctype>
typedef unsigned long long uint64;
inline uint64 getint() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register uint64 x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
int main() {
    freopen("code.in","r",stdin);
    freopen("code.out","w",stdout);
    const int n=getint();
    uint64 k=getint();
    for(register int i=n-1;i>=0;i--) {
        const bool cur=(k>>i)&1;
        putchar(cur?'1':'0');
        if(cur) k=~k;
    }
    puts("");
    return 0;
}

Guess you like

Origin www.cnblogs.com/skylee03/p/11873178.html