1. 程式人生 > 其它 >CF862C Mahmoud and Ehab and the xor 題解

CF862C Mahmoud and Ehab and the xor 題解

Codeforces
Luogu

Description.

給你一個數 \(x\) 問你能不能分解成 \(n\) 個互不相同的數,使得這 \(n\) 個數的異或和為 \(x\)

Solution.

Corner Case 太多了
首先,一眼秒,直接前前 \(n-2\) 個是 \(i\),然後最後兩個是 \(x\oplus 2^{17}\oplus \bigoplus_{i=1}^{n-2}i\)\(2^{17}\) 就行了。
然後 WA,發現 \(n=1\) 的 corner case。
然後 WA,發現 \(x=\bigoplus_{i=1}^{n-2}i\) 的 corner case。
就考慮如果 \(x=\bigoplus_{i=1}^{n-2}i\)

,那肯定要把倒數第三個拉入夥來修改。
然後 WA,發現 \(x=\bigoplus_{i=1}^{n-2}i\)\(n=2\) 的無解情況。
然後 AC。

Coding.

點選檢視 /jk 程式碼
//是啊,你就是那隻鬼了,所以被你碰到以後,就輪到我變成鬼了{{{
#include<bits/stdc++.h>
using namespace std;typedef long long ll;
template<typename T>inline void read(T &x)
{
	x=0;char c=getchar(),f=0;
	for(;c<48||c>57;c=getchar()) if(!(c^45)) f=1;
	for(;c>=48&&c<=57;c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	f?x=-x:x;
}
template<typename T,typename...L>inline void read(T &x,L&...l) {read(x),read(l...);}//}}}
int a[100005];
int main()
{
	int n,x;read(n,x);if(n==2&&!x) return puts("NO"),0;
	puts("YES");if(n==1) return printf("%d\n",x),0;
	for(int i=1;i<n-1;i++) a[i]=i,x^=i;
	if(!x) a[n]=x,a[n-1]^=1<<18,a[n-2]^=1<<18|1<<19,a[n]^=1<<19;
	else a[n]=x,a[n]^=1<<18,a[n-1]^=1<<18;
	for(int i=1;i<=n;i++) printf("%d%c",a[i],i==n?'\n':' ');
	return 0;
}