提交时间:2021-12-07 19:16:37

运行 ID: 33731

#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct Node { int son[2]; bool vis; }t[2000010]; int pos = 1; void Add(long long x) { int p = 1,i; t[p].vis = true; for(i = 32;i >= 0;i--) { long long b = (x>>i)&1; if(!t[t[p].son[b]].vis) t[p].son[b] = ++pos; p = t[p].son[b]; t[p].vis = true; } return; } long long Query(long long x) { long long ans = 0,p = 1,i; for(i = 32;i >= 0;i--) { long long b = (x>>i)&1ll; if(t[t[p].son[!b]].vis) { ans = (ans<<1) + !b; p = t[p].son[!b]; } else { p = t[p].son[b]; ans = (ans<<1) + b; } } return ans; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); //freopen("xor.in","r",stdin); //freopen("xor.out","w",stdout); int T,lhn; cin>>T; for(lhn = 1;lhn <= T;lhn++) { int n,m,i; long long x; memset(t,0,sizeof(t)); pos = 1; cin>>n>>m; for(i = 1;i <= n;i++) { cin>>x; Add(x); } cout<<"Case #"<<lhn<<':'<<endl; while(m--) { cin>>x; cout<<Query(x)<<endl; } } return 0; }