【CF #435】C 【思维+异或的特点】
2017-10-13 18:05
357 查看
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 .
异或的特性
a^b=c 则 b=c^a or a=c^b
假如没有要求构造的n个数字必须不同,我们可以这样构造,
[1 2 3 … (n-1) ] ^ a ==x
最后一个数字a就为 x^ [1 2 3 … (n-1) ] 就可以满足要求 。
但是有可能a和前面的[1 2 3 … (n-1) ] 重复。
所以针对这个题,要改变一点。
既然可能重复,我们就从[1 2 3 … (n-1) ] 中抽出一个数字,改为1<<17( 1<<17就已经大于1e5啦). 这样就可以构造
[1 2 3 … (n-2) ] ^ (1<<17)^a ==x
(1<<17)^a=x^[1 2 3 … (n-2) ] .但是这个时候仍然可能a==1<<17,如果a==1<<17 ,那么x^[1 2 3 … (n-2) ]也就为0 ,
所以只要保证x^[1 2 3 … (n-2) ]!=0 ,那么a!=(1<<17),就可以满足条件。所以我们再从[1 2 3 … (n-2) ]中找到一个数字(1<<18).这样的话,x^[1 2 3 … (n-3) ]^(1<<18) 一定不会为0。。a!=(1
4000
<<17)!=(1<<18) 。
代码
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 .
异或的特性
a^b=c 则 b=c^a or a=c^b
假如没有要求构造的n个数字必须不同,我们可以这样构造,
[1 2 3 … (n-1) ] ^ a ==x
最后一个数字a就为 x^ [1 2 3 … (n-1) ] 就可以满足要求 。
但是有可能a和前面的[1 2 3 … (n-1) ] 重复。
所以针对这个题,要改变一点。
既然可能重复,我们就从[1 2 3 … (n-1) ] 中抽出一个数字,改为1<<17( 1<<17就已经大于1e5啦). 这样就可以构造
[1 2 3 … (n-2) ] ^ (1<<17)^a ==x
(1<<17)^a=x^[1 2 3 … (n-2) ] .但是这个时候仍然可能a==1<<17,如果a==1<<17 ,那么x^[1 2 3 … (n-2) ]也就为0 ,
所以只要保证x^[1 2 3 … (n-2) ]!=0 ,那么a!=(1<<17),就可以满足条件。所以我们再从[1 2 3 … (n-2) ]中找到一个数字(1<<18).这样的话,x^[1 2 3 … (n-3) ]^(1<<18) 一定不会为0。。a!=(1
4000
<<17)!=(1<<18) 。
代码
#include<bits/stdc++.h> using namespace std; #define LL long long #define fread() freopen("in.txt","r",stdin) #define fwrite() freopen("out.txt","w",stdout) #define CLOSE() ios_base::sync_with_stdio(false) const int MAXN = 1e5; const int MAXM = 1e6; const int mod = 1e9+7; const int inf = 0x3f3f3f3f; int main(){ CLOSE(); // fread(); // fwrite(); // int n;scanf("%d",&n); // int x;scanf("%d",&x); // for(int i=1;i<=n-1;i++) { // int val; // scanf("%d",&val); // x^=val; // } // printf("%d\n",x); int n,x;scanf("%d%d",&n,&x); if (n == 1){ printf("YES\n%d\n",x); return 0; } if(n == 2) { if (x== 0){ puts("NO"); return 0; }else{ puts("YES"); int a = 1<<17; int b=x^a; printf("%d\n%d\n",a,x^a); return 0; } } puts("YES"); for(int i=1;i<=n-3;i++) { x^=i; printf("%d\n",i); } int c=1<<18; printf("%d\n",c); x^=c; int a = 1<<17; int b=x^a; printf("%d\n%d\n",a,x^a); return 0; }
相关文章推荐
- cf 862C Mahmoud and Ehab and the xor 思维+构造
- Atcoder Tenka1 Programmer Beginner Contest IntegerotS 【异或+思维】
- Wannafly挑战赛1 Xorto(前缀异或+思维)
- CF- 798A. Mike and palindrome - 思维
- Riverside Curio(CF Round #472 D div2 思维水题)
- 【CF】--Buggy Robot (思维)
- cf(思维题,判断两个大数的大小的比较方法)
- CF --- 832D Misha, Grisha and Underground 【LCA + 思维】
- CF - 779A. Pupils Redistribution 贪心+思维
- CF 869 A. The Artful Expedient(异或)
- CF 219D Choosing Capital for Treeland 【树形dp + 思维】
- CF - 785B. Anton and Classes - 思维
- CF- 798A. Mike and palindrome - 思维
- 位异或运算符^的特点及面试题
- 思维的特点和缺陷
- CF--- PROBLEM 620 E 【思维 + DFS序 + 二进制状态】
- 2013年9月4日、内部类访问特点|思维方法
- Codeforces Round #435 (Div. 2)-异或规律&思维-Mahmoud and Ehab and the xor
- CF:3D City Model(小思维)
- CF - 754A.Lesha and array splitting - 贪心+思维