Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem

D. Ehab and the Expected XOR Problem

Given two integers n and x, construct an array that satisfies the following conditions:

  • for any element ai in the array, 1ai<2^n
  • there is no non-empty subsegment with bitwise XOR equal to 0 or x,
  • its length l should be maximized.

A sequence b is a subsegment of a sequence a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input

The only line contains two integers n and x (1n18  1x<2^18).

Output

The first line should contain the length of the array l.

If l is positive, the second line should contain l space-separated integers a1, a2, …, al (1ai<2^n) — the elements of the array a.

If there are multiple solutions, print any of them.

solution

这道题当时候在写的时候并不知道怎么写,题目大意就是要求子区间的异或和不能够0或者x,求最长的区间数字。
首先对于这道题我们可以得到一个显而易见的结论那就是,对于所有的数字进行前缀异或和,如果前缀数组中不存在相同的两个数字,那么这个序列的连续子区间的异或和肯定不会为零,那么对于x>=2^n的情况显然最长的长度就是2^n-1。而当x<2^n时,我们得到x的情况当且仅当sum[l]^sum[r]=x时,我们才会出现异或和为0的情况,那么显然我们可以要a^b=x的情况只能存在a,b中间的一个即可,所以我们这样就可以确定我们的前缀异或和数组了。那么求原数组a[i]=sum[i]^sum[i-1]即可,代码如下:
 1 #include<bits/stdc++.h>
 2 #include<vector>
 3 using namespace std;
 4 long long n,x; 
 5 bool pd[2000010];
 6 int main(){
 7     cin>>n>>x;
 8     memset(pd,0,sizeof pd);
 9     long long cnm=(1<<n);
10     for (int i=0;i<cnm;i++)
11         if (!pd[i])
12             pd[i^x]=1;
13     int ans=0;
14     for (int i=1;i<cnm;i++)
15         if (!pd[i])
16             ans++;
17     cout<<ans<<endl;
18     int l=0;
19     for (int i=1;i<cnm;i++)
20         if (!pd[i]) {
21             cout<<(i^l)<<' ';
22             l=i;
23         }
24 }

猜你喜欢

转载自www.cnblogs.com/beafreeman/p/11001328.html