[CF1174D] Ehab and the Expected XOR Problem
Description
给两个数 $ n $ 和 $ x $,构造一个满足以下条件的序列:
- 对任何序列中的元素 $ a_i (,) 1le a_i<2^n $
- 序列中没有非空连续子序列异或和为 $ 0 $ 或 $ x $
- 序列长度 $ l $ 应该最大
Solution
构造前缀和序列 (s_i = oplus_{j=1}^i a_i),每次暴力找一个 ([1,2^n)) 的数,使得 (s_i) 没有出现过即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n,x,b[N],a[N],top;
signed main() {
ios::sync_with_stdio(false);
cin>>n>>x;
top=1;
b[0]=b[x]=1;
while(true) {
for(int i=1;i<1<<n;i++) {
if(b[a[top-1]^i]==0) {
a[top]=a[top-1]^i;
b[a[top]]=1;
b[a[top]^x]=1;
break;
}
}
if(a[top]) ++top;
else break;
}
cout<<top-1<<endl;
for(int i=1;i<=top-1;i++) cout<<(a[i]^a[i-1])<<" ";
}