codeforce 862C Mahmoud and Ehab and the xor(构造)

来源:互联网 发布:e订通软件 编辑:程序博客网 时间:2024/06/14 17:31

C. Mahmoud and Ehab and the xor
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 105, 0 ≤ 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个不相同数,使他们的异或值为x。
关于异或有两点性质:

a xor a=0
a xor 0=a

然后对于本题,就可以构造这个序列,对于<=3的情况特判
case 1 直接输出
case 2 输出0和x本身
default 先输出1~n-3,然后构建后三个数,使得整个数列的xor值满足答案
设ans=1^2^······^n-3,倒数第三个数为a,倒数第二个数为b,那么最后一个数应为
ans^a^b^x,对于a和b,需要找一个足够大的数,保证和数列中的其他数都不同

这里有个问题,为什么不输出1~n-2,然后只构造一个a,如果只构造一个a的话,那么最后一项是ans^a^x,对于ans^x我们不能保证他的值不为0,所以要构造两个数a和b,此时数列 最后三项 a b ans^a^b^x,不会出现最后一项等于a或b的情况

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;typedef long long ll;int main(){    int n,x;    scanf("%d%d",&n,&x);    if(n==1)    {        puts("YES");        printf("%d\n",x);    }    else if(n==2)    {        if(x==0)        {            puts("NO");        }        else        {            puts("YES");            printf("%d %d",0,x);        }    }    else    {        puts("YES");        int ans=0;        for(int i=1;i<n-2;i++)        {            printf("%d ",i);            ans^=i;        }        int a=1<<18,b=1<<17;        printf("%d %d %d\n",a,b,a^b^ans^x);    }
阅读全文
0 0