码迷,mamicode.com
首页 > 其他好文 > 详细

CF1174D Ehab and the Expected XOR Problem - 构造

时间:2019-10-15 20:50:19      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:ISE   inline   ***   printf   sequence   enc   子序列   its   枚举   

题面

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

·for any element ai in the array, \(1≤ai<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.

题意

给两个数 \(n\)\(x\),构造一个满足以下条件的序列:
·对任何序列中的元素 \(a_i\),1\leq a_i<2^n1≤a_i<2^n$
·序列中没有非空连续子序列异或和为 \(0\)\(x\)
·序列长度 \(l\) 应该最大

思路

思路比较巧妙,因为元素可重复不太好搞,就考虑构造一个答案序列 \(a\) 的异或前缀和 \(b\),且 \(b\) 满足任意 \(b_i \ xor \ b_j \ \not= \ x\)\(0\)
因为若 \(a \ xor \ b \ = \ c\),则 \(a \ xor \ c \ = \ b\),所以从 \(1\) 枚举到 \(2^n-1\) ,每次用可行的 \(i\) 数加入答案并排除 \(i \ xor \ x\) 这个数。

代码

/************************************************
*Author        :  lrj124
*Created Time  :  2019.10.15.19:28
*Mail          :  1584634848@qq.com
*Problem       :  cf1174d
************************************************/
#include <cstdio>
const int maxn = 1<<18;
int n,x,ans[maxn];
bool vis[maxn];
int main() {
    //freopen("cf1174d.in","r",stdin);
    //freopen("cf1174d.out","w",stdout);
    scanf("%d%d",&n,&x);
    vis[0] = vis[x] = true;
    for (int i = 1;i < 1<<n;i++)
        if (!vis[i]) {
            vis[i^x] = true;
            ans[++ans[0]] = i;
        }
    printf("%d\n",ans[0]);
    for (int i = 1;i <= ans[0];i++) printf("%d ",ans[i]^(i ^ 1 ? ans[i-1] : 0));
    return 0;
}

CF1174D Ehab and the Expected XOR Problem - 构造

标签:ISE   inline   ***   printf   sequence   enc   子序列   its   枚举   

原文地址:https://www.cnblogs.com/lrj124/p/11679998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!