CF 1174 D. Ehab and the Expected XOR Problem 异或技巧

题意就是给我们两个数n,x。让我们构造个数组,数组有三个条件

1.要有尽可能多的元素

2.要其中任何一段数字的异或和不等于0和x

3.元素的范围是[ 1, 2^n)


#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
const int maxn =1<<19;
int cnt,S[maxn];
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    // 不能存在两相等 也不能存在和x异或为0的数
    set<int>s;
    s.insert(0);
    for(int i=1;i<(1<<n);i++){
        if(!s.count(i^x)){//已存在一个0 如果与x相等会得到0 保证不会有相同的元素 尽可能变大三个条件
            s.insert(i);
            S[++cnt]=i;
        }
    }

    printf("%d\n",cnt);
    for(int i=1;i<=cnt;i++){
        printf("%d",S[i]^S[i-1]);
        if(i==cnt)puts("");
        else putchar(' ');
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33859479/article/details/91865888