Codeforces Round #563 (Div. 2) D

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值