Codeforces Round #435 (Div. 2)-Mahmoud and Ehab and the xor(异或)

C. Mahmoud and Ehab and the xor
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.

Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.

Input

The only line contains two integers n and x (1 ≤ n ≤ 1050 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.

Output

If there is no such set, print "NO" (without quotes).

Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.

Examples
input
5 5
output
YES
1 2 4 5 7
input
3 6
output
YES
1 2 5
Note

You can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR

For the first sample .

For the second sample .


题意:给你两个数n和x,让你构造n个非负整数,使得这n个数的异或和为x,且这n个数相互不重复。

题解:首先想到异或的一个性质,若x^y=z,则x=y^z,呢我们可以这样构造,因为让你构造长为n的序列。

所以我们令其前n-1个数为1到n-1,则令最后一个为x^1^2.....^n-1即可。但是会出现一个问题,假如最后构造这个数和之前的某个数冲突(重复)了怎么办?

我们知道一个数异或同一个数两次等于没异或这个数,因此既然最后构造这个数和之前的某个数冲突了,呢我们就把这两个数都丢了,此时数组中只剩下n-2个数,无脑往后找大的数的话很麻烦,容易出错,我们可以考虑把之前的已构造的某个数去掉,然后加上两个数,这两个数满足异或和为这个数,我们可以往大了找,这样可以保证不重复,但是我们应该去掉那个数呢,此时就要分情况讨论了:

1.冲突的数为1时,我们肯定把1去掉,然后再把另一个数拆成连个数,具体拆那个?我们可以随便拆,但是因为我们要拆的数肯定是小于n的数,假如你要拆的数很大时,n很小时就要特判。。。很麻烦,所以拆2就行了。

2.冲突的数不为2时,无脑拆1就行了。

#include<set>  
#include<map>     
#include<stack>            
#include<queue>            
#include<vector>    
#include<string> 
#include<time.h>
#include<math.h>            
#include<stdio.h>            
#include<iostream>            
#include<string.h>            
#include<stdlib.h>    
#include<algorithm>   
#include<functional>    
using namespace std;            
#define ll __int64        
#define inf  1000000000       
#define mod 1000000007             
#define maxn  2000100
#define lowbit(x) (x&-x)            
#define eps 1e-9 
int main(void)
{
	int n,x,i,tmp;
	scanf("%d%d",&n,&x);
	/*tmp=x;
	for(i=1;i<=100;i++)
	{
		tmp^=i;
		printf("%d\n",tmp);
	}*/
	if(n==1)
		printf("YES\n%d\n",x);
	else if(n==2 && x==0)
		printf("NO\n");
	else
	{
		tmp=x;int t=0;
		for(i=1;i<n;i++)
			tmp^=i;
		printf("YES\n");
		if(tmp>=1 && tmp<=n-1)
		{
			if(tmp==1)
				printf("999999 999997 0 "),t=2;
			else
				printf("999999 999998 0 "),t=1;
		}
		else
			printf("%d ",tmp);
		for(i=1;i<n;i++)
			if(i!=tmp && i!=t)
				printf("%d ",i);
		printf("\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值