E. Keroro侵略地球
Keroro來侵略地球之前,曾跟Giroro伍長打賭:“我一個人滅掉整個地球給你看!”.
於是Keroro同學真的自己一個人來到地球開始他的侵略行動了。從K隆星出發之前,Keroro從Kururu曹長那兒拿了若干台左手武器{Li}和若干台右手武器{Ri},Keroro需要從{Li}裡選一台左手武器,從{Ri}裡選一台右手武器,用來組合成可用的恐怖武器。
左右手武器組合的規則很簡單,假設從{Li}選出來攻擊力為p的武器,從{Ri}選出來攻擊力為q的武器,組合起來的攻擊力就是p XOR q.
Keroro想知道,他能組合成的最強武器攻擊力為多少?
Hint:必須左右手武器都選出來一個,才能組合成可用武器
XOR表二進位裡的“異或”操作,pascal語言裡是"xor", C/C++/Java裡是"^".
Input :
第一行兩個整數n, m (1 <= n,m <= 100000), 表有n件左手武器,m件右手武器。
第二行n個正整數{L},li表第i件左手武器的攻擊力,0 <= li <= 10^12
第三行m個正整數{R},ri表第i件右手武器的攻擊力,0 <= ri <= 10^12
Output :
最強組合武器的最大值。
思路:參看CF round_173 div E題
#include <stdio.h>#include <algorithm>using namespace std;#define LIMIT 45class Trie { public : Trie * children[2]; Trie () { children[0] = children[1] = NULL; } void Insert(long long x, int deep) { if(deep == -1) return ; int t = ((x >> deep) & 1LL); if(!children[t]) children[t] = new Trie(); children[t]->Insert(x, deep-1); } long long Query(long long x, int deep) { if(deep == -1) return 0; int t = (( x >> deep ) & 1LL); if(children[!t]) return (1LL << deep) + children[!t]->Query(x, deep-1); else return children[t]->Query(x, deep-1); return 0; }};Trie * root = new Trie();int n, m;int main(){ long long x; scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) { scanf("%lld", &x); root->Insert(x, LIMIT); } long long ans = 0; for(int i = 0; i < m; i++) { scanf("%lld", &x); ans = max(ans, root->Query(x, LIMIT)); } printf("%lld\n", ans); return 0;}