Given two integers n and x, construct an array that satisfies the following conditions:
for any element ai in the array, 1≤ai<2n;
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 (1≤n≤18, 1≤x<218).
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 (1≤ai<2n) — the elements of the array a.
If there are multiple solutions, print any of them.
Examples
input
3 5
output
3
6 1 3
input
2 4
output
3
1 3 1
input
1 1
output
0
题意:给你两个数n,x,然后让你从[1,2^n) 这个区间里面选一串数字,使得这串数字的任意字串的异或不等于 0和x,这道题弄了好久才弄出来…
如果x>=2 ^n 的话,显然我们可以取1–2 ^n之间的所有数,为什么呢,你最后输出的数是你当前这个值和前面的异或,那么输出的前4 个数就是 0 ^1, 1 ^2, 2 ^3, 3 ^4 ,那么这4个数任意异或都不等于0 ,因为当前这个值和前面要和他异或的值有重复地方,但是如果x<2 ^n,比如x=3的话,显然 前三个异或的值为3不符合题意,所以你在选了一个数后要把这个数异或3的那个值去掉
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<set>
#define mod 10007
#define ll long long
using namespace std;
ll p[1<<18],q[1<<18];
int main()
{
ll n,x;
scanf("%lld%lld",&n,&x);
memset(p,0,sizeof(p));
memset(q,0,sizeof(q));
ll ans=1;
q[x]=1;
for(ll a=1;a<(1<<n);a++)
{
if(q[a])
{
continue;
}
q[a]=1;
p[ans++]=a;
q[a^x]=1;
}
printf("%lld\n",ans-1);
if(ans==1)
return 0;
for(int a=1;a<ans;a++)
printf("%d ",p[a]);
printf("\n");
for(int a=1;a<ans;a++)
{
printf("%lld ",p[a]^p[a-1]);
}
printf("\n");
return 0;
}