Arpa’s obvious problem and Mehrdad’s terrible solution
There are some beautiful girls in Arpa’s land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where is bitwise xor operation (see notes for explanation).
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
2 3 1 2
1
6 1 5 1 2 3 4 1
2
Note
In the first sample there is only one pair of i = 1 and j = 2. so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since ) and i = 1, j = 5(since ).
A bitwise xor takes two bit integers of equal length and performs the logical xoroperation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: .
sol:询问有多少对i,j满足ai^aj = X,这就是Trie树可以轻松支持的,只要注意下X=0时的情况就可以了
Ps:答案会爆int
#includeusing namespace std;typedef long long ll;inline ll read(){ ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) { f|=(ch=='-'); ch=getchar(); } while(isdigit(ch)) { s=(s<<3)+(s<<1)+(ch^48); ch=getchar(); } return (f)?(-s):(s);}#define R(x) x=read()inline void write(ll x){ if(x<0) { putchar('-'); x=-x; } if(x<10) { putchar(x+'0'); return; } write(x/10); putchar((x%10)+'0'); return;}#define W(x) write(x),putchar(' ')#define Wl(x) write(x),putchar('\n')const int N=100005;int n,Num,a[N];struct Zidianshu{ int Trie[N*25][25],cnt; int End[N*25]; inline void Init() { cnt=0; } inline void Insert(int Num) { int i,Root=0; for(i=0;i<=23;i++) { int oo=(Num&(1< >i; if(!Trie[Root][oo]) Trie[Root][oo]=++cnt; Root=Trie[Root][oo]; } End[Root]++; } inline int Query(int Num,int Want) { int i,Root=0; for(i=0;i<=23;i++) { int oo=((Want&(1< >i)^((Num&(1< >i); if(!Trie[Root][oo]) return 0; Root=Trie[Root][oo]; } return End[Root]; }}Trie;int main(){ Trie.Init(); int i; long long ans=0; R(n); R(Num); for(i=1;i<=n;i++) { a[i]=read(); ans+=1ll*Trie.Query(a[i],Num); Trie.Insert(a[i]); } Wl(ans); return 0;}/*input2 31 2output1input6 15 1 2 3 4 1output2input10 01 2 1 2 1 2 1 2 1 2output20*/