#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 100000 + 5, INF = 0x7fffffff; LL trie[32 * maxn][2], value[32 * maxn]; int rt, tot; void build(LL s) { rt = 0; for(int i=32; i>=0; i--) { int x = (s >> i) & 1; // 取每一位 if(trie[rt][x] == 0) { trie[rt][x] = ++tot; } rt = trie[rt][x]; } value[rt] = s; //最后的结点记录s } LL qp(LL s) { rt = 0; for(int i=32; i>=0; i--) { int x = (s >> i) & 1; if(trie[rt][x^1]) rt = trie[rt][x^1]; else rt = trie[rt][x]; } return value[rt]; } void init() { tot = 0; mem(trie, 0); mem(value, 0); } int main() { int T, kase = 0; scanf("%d", &T); while(T--) { init(); int n, m; scanf("%d%d", &n, &m); rap(i, 1, n) { LL temp; scanf("%lld", &temp); build(temp); } printf("Case #%d:\n", ++kase); rap(i, 1, m) { LL temp; scanf("%lld", &temp); printf("%lld\n", qp(temp)); } } return 0; }
原文地址:https://www.cnblogs.com/WTSRUVF/p/9457564.html
时间: 2024-10-10 19:18:43