Codeforces

来源:互联网 发布:windows ce模拟器 编辑:程序博客网 时间:2024/06/07 00:39
C. Mahmoud and Ehab and the xor

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 ofn distinct non-negative integers such the bitwise-xor sum of the integers in it is exactlyx. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than106.

Input

The only line contains two integers n andx (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 printn 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
YES1 2 4 5 7
Input
3 6
Output
YES1 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 .



题意:给你一个数X问你能不能分解成N个数,使得这N个数的异或和为X

解题思路:重点就是抓住异或的性质,两个相同的数异或等于0,任何数异或0结果都不变。意思就是例如  3^4^5^3^4^5^6=6  。然后这道题就很简单了。假设我们要使最后的结果为X,大小为4,那么我们可以   a^b^c^(a^b^c)^x=x,其中四个数分别为,a,b,c,(a^b^c)。但是a^b^c可能等于a,b,c中的一个数,所以我们要用一些足够特殊的数完成这个操作。题目告诉了每个数的最大值。具体看代码实现。



#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;int main(){    int x,n;    cin>>n>>x;    //n=1,2都是特殊情况,要特殊考虑    if(n==1)        cout<<"YES\n"<<x<<endl;    if(n==2){        if(x==0)            cout<<"NO\n";        else            cout<<"YES\n0 "<<x<<endl;    }    int temp=0;//存前i个数的异或和    int aaa=(1<<17);    int bbb=(1<<18);    if(n>2){        cout<<"YES\n";        //留3个数做最后的特殊处理,前i个数可以随便选,这里直接用0到n-3,但是你要记录前i个数的异或和。        for(int i=1;i<=n-3;i++){            cout<<i<<" ";            temp^=i;//记录异或和        }        //最后把剩下的三个数,特殊输出。        //aaa,bbb,aaa^bbb会抵消掉,然后前i个数的异或和会和temp抵消掉,最后剩下x        cout<<aaa<<" "<<bbb<<" "<<(aaa^bbb^temp^x)<<endl;    }    return 0;}


原创粉丝点击