异或

24 阅读1分钟

题目

Problem - B - Codeforces

在讨论子句段的 XOR 时,通常可以考虑前缀 XOR。那么,如果 bi=a1a2aib_i = a_1 \oplus a_2 \oplus \ldots \oplus a_i 是数组或前缀 XOR,那么子段 XOR ff 就是 f(x,y)=bybx1f(x, y) = b_y \oplus b_{x-1} 。也就是说,要使 f(l,r)f(l, r) 为零, bl1b_{l - 1} 必须等于 brb_r ,而 bb 的所有其他值都应该是不同的。最简单的构造就是 bi=ib_i = i ,但 br=l1b_r = l - 1 除外。这样,数组 aa 就可以重构为 ai=bibi1a_i = b_i \oplus b_{i - 1}

AC代码如下

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
void solve(){
	int n,l,r;
	cin>>n>>l>>r;
	vector<int>b(n+1);
	for(int i=0;i<=n;i++){
		b[i]=i;
	}
	b[r]=b[l-1];
	for(int i=1;i<=n;i++){
		cout<<(b[i]^b[i-1])<<(i==n?"":" ");
	}
	cout<<endl;
}
signed main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t=1;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}