题面
自出题
挂个 pdf
题面下载
算法
暴力
可能的答案只有 \(O(n^2)\) 个, 考虑每个答案 \(\rm{check}\) 是 \(O(n \log n)\) 的
总时间复杂度 \(O(n ^ 3 \log n)\)
/*O(answer * n * logn), 即 O(n^3logn) 的算法, 预期 60pts*/
/*对于每一种可能的答案, 首先对于每一个点, 计算其配对点是否存在, 若存在, 则继续, 否则退出*/
#include <bits/stdc++.h>
#define int long long
const int MAXN = 2e3 + 24;
const int MAXCNT = 4520641;
int n;
int a[MAXN], b[MAXN];
int b_copy[MAXN];
int Appear_Time[MAXN];
int num = 0;
int Possible_X[MAXCNT];
int cnt = 0;
int Ans[MAXCNT];
int lsy = 0;
class LSH
{
private:
public:
void solve()
{
num = std::unique(b_copy + 1, b_copy + n + 1) - (b_copy + 1);
for (int i = 1; i <= n; i++)
{
b[i] = std::lower_bound(b_copy + 1, b_copy + n + 1, b[i]) - b_copy;
Appear_Time[b[i]]++;
}
}
} lsh;
class Solution
{
private:
int Appear_Time_Copy[MAXN];
void init()
{
for (int i = 1; i <= n; i++)
{
Appear_Time_Copy[i] = Appear_Time[i];
}
}
bool check(int x)
{
init();
for (int i = 1; i <= n; i++)
{
int Need = x ^ a[i];
int pos = std::lower_bound(b_copy + 1, b_copy + n + 1, Need) - b_copy;
if (b_copy[pos] != Need || Appear_Time_Copy[pos] == 0)
return false;
Appear_Time_Copy[pos]--;
}
return true;
}
public:
void solve()
{
for (int i = 1; i <= cnt; i++)
{
int NowX = Possible_X[i];
if(check(NowX))
{
Ans[++lsy] = NowX;
}
}
std::sort(Ans + 1, Ans + lsy + 1);
lsy = std::unique(Ans + 1, Ans + lsy + 1) - (Ans + 1);
printf("%lld\n", lsy); // 520
for (int i = 1; i <= lsy; i++)
{
printf("%lld\n", Ans[i]);
}
}
} Sol;
signed main()
{
//freopen("f.in", "r", stdin);
//freopen("f.out", "w", stdout);
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &b[i]), b_copy[i] = b[i];
std::sort(b_copy + 1, b_copy + n + 1);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
Possible_X[++cnt] = a[i] ^ b[j];
}
}
lsh.solve();
Sol.solve();
return 0;
}
/*
3
1 2 3
6 4 7
1
5
*/
/*
2
0 1
0 2
0
*/
优化
观察到符合条件的 \(x\) , 当且仅当 \(x\) 出现了 \(\geq n\) 次, 判断后复杂度变为 \(O(n ^ 2)\)
具体的, 使用 hash 存储