题解 P5657 【格雷码【民间数据】】

· · 题解

考虑答案的每一位

0位为011001100110...

1位为0011110000111100...

发现第i位即k\oplus \lfloor\frac k2\rfloor的第i

#include<iostream>
int n;
unsigned long long k;
int main(){
    std::cin>>n>>k;
    k^=k>>1;
    while(~--n)std::cout<<(k>>n&1);
}