题解 F

传送门

首先发现合法的 \(x\) 不超过 \(n\) 个,所以试着枚举 \(x\)
从a中随便选一个数和所有的b异或一遍就可以得到一个可能的集合了
然后就要能 \(O(n)\) check一个 \(x\) 是否合法

\[a\oplus b=x \iff a \oplus x=b \]

所以可以开个map记录数量

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 2010
#define ll long long
#define fir first
#define sec second
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n;
int a[N], b[N], sta[N], top;

namespace force{
	int p[N];
	void solve() {
		for (int i=1; i<=n; ++i) p[i]=i;
		do {
			int x=a[1]^b[p[1]];
			// cout<<"x: "<<x<<endl;
			for (int i=2; i<=n; ++i) if ((a[i]^b[p[i]])!=x) goto jump;
			sta[++top]=x;
			jump: ;
		} while (next_permutation(p+1, p+n+1));
		// cout<<"top: "<<top<<endl;
		sort(sta+1, sta+top+1);
		top=unique(sta+1, sta+top+1)-sta-1;
		printf("%d\n", top);
		for (int i=1; i<=top; ++i) printf("%d\n", sta[i]);
		exit(0);
	}
}

namespace task1{
	int c[N], tot;
	unordered_map<int, int> mp;
	void solve() {
		for (int i=1; i<=n; ++i) c[i]=a[1]^b[i];
		sort(c+1, c+n+1);
		tot=unique(c+1, c+n+1)-c-1;
		for (int i=1; i<=tot; ++i) {
			for (int j=1; j<=n; ++j) mp[b[j]]=0;
			for (int j=1; j<=n; ++j) ++mp[b[j]];
			for (int j=1,t; j<=n; ++j) {
				t=a[j]^c[i];
				if (mp.find(t)==mp.end()) goto jump;
				else if (--mp[t]<0) goto jump;
			}
			for (auto it:mp) if (it.sec!=0) goto jump;
			sta[++top]=c[i];
			jump: ;
		}
		printf("%d\n", top);
		for (int i=1; i<=top; ++i) printf("%d\n", sta[i]);
		exit(0);
	}
}

signed main()
{
	freopen("f.in", "r", stdin);
	freopen("f.out", "w", stdout);

	n=read();
	for (int i=1; i<=n; ++i) a[i]=read();
	for (int i=1; i<=n; ++i) b[i]=read();
	// force::solve();
	task1::solve();

	return 0;
}
posted @ 2021-10-17 09:15  Administrator-09  阅读(0)  评论(0编辑  收藏  举报