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, 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.

Input

The only line contains two integers n and x (1≤n≤18  1≤x<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 (1≤ai<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 }

原文地址:https://www.cnblogs.com/beafreeman/p/11001328.html

时间: 2024-08-30 15:54:40

Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem的相关文章

Codeforces Round #250 (Div. 2) C、The Child and Toy

注意此题,每一个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],其中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部分的编号. 注意题目移除的都是与第i部分直接相连的部分的能量值, 将本题目简化得,只考虑两个点1和2,1和2相连,1的能量值是10,2的能量值是20, 移除绳子时,要保持能量最小,可以移除部分2,这样移除的能量就是与2相连的部分1的能量即是10: 故每次相连两部分都移除能量值大的即可 #includ

Codeforces Round #251 (Div. 2) C、D

Codeforces Round #251 (Div. 2) C题: 题意:给定一些数字,要把这些数字方程k行,其中p行和为奇数,剩下和为偶数. 思路:根据奇数偶数的性质,先把p行放1个奇数,然后看看剩下的奇数是不是偶数个,如果不是肯定不满足,然后判断一下剩下的奇数个数/2加上偶数个数是否多余p个,如果不是肯定不满足,然后把这些放入p行,还有剩下的数字就全丢到最后一行去. D题: 题意:给定两个序列,每次操作可以对序列中的数字进行+1或者-1,要使得a序列的最小大于b序列的最大,问最少需要几次操

cf 1174 D Ehab and the Expected XOR Problem

cf 1174 D Ehab and the Expected XOR Problem 题意 在1~\(2^n\)范围内找到一个最长的序列,使得该序列的每一个子串异或后不等于0和x 题解 假设该序列为a,那么前缀异或和b[i] = a[i]^a[i-1]^...^a[0],如果b之间异或都不会等于0和x,那么a之间也不会. #include <cstdio> #include <cstring> int main() { int n, x; while(~scanf("%

Codeforces Round #298 (Div. 2) A、B、C题

题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side

Codeforces Round #563 (Div. 2)C

C. Ehab and a Special Coloring Problem 题目链接:http://codeforces.com/contest/1174/problem/C 题目 You're given an integer n. For every integer i from 2 to n, assign a positive integer ai such that the following conditions hold: For any pair of integers (i,

Codeforces Round #563 (Div. 2)/Codeforces1174

CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i\)与\(\sum\limits_{n+1}^{2n}a_i\)差值最大,排一下序就好了 CF1174B Ehab Is an Odd Person 一个显然的性质就是如果至少有一个奇数和一个偶数,那么是可以随意调整的,也就是升序排序 否则不可以进行任何操作 CF1174C Ehab and a Special Coloring Problem 把每个质数预处理出来,\(x

Codeforces Round #563 (Div. 2)C. Ehab and a Special Coloring Problem

原文链接:传送门思路:素数筛代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstring" 4 using namespace std; 5 long long a[2000006],n; 6 int main(){ 7 cin>>n; 8 long long flag = 1; 9 memset(a,0,sizeof(a)); 10 for(int i=2;i&l

Codeforces Round #563 (Div. 2)B;Ehab Is an Odd Person

原文链接:任意门 题目大意:给你一组数,让你交换两个数的位置,让它们的和为奇数,且使其交换后,顺序满足最小字典序列.思路:这就是一道狗题,看代码,你就会******了,只需要sort排序.代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstdio" 4 #include"cstring" 5 long long a[10000006],n,js=0,os=0

Codeforces Round #563 (Div. 2)

CF 题目链接:https://codeforces.com/contest/1174 总结 ABC都比较水,27分钟写完了,但是D题想偏了,一直WA,AC代码确实太巧妙了. A题 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 100050 int n,sum1,sum2,a[N]; template<typename T>void read(T&x) { ll k=0