For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.
Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A ⊕ B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A ⊕ B > max{A, B}).
Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. The ith integer denotes the skill level of ith student. Every integer will not exceed 109.
Output
For each case, print the answer in one line.
Sample Input
2
3
1 2 3
5
1 2 3 4 5
Sample Output
1
6
题意:在一组数中找出满足A ⊕ B > max{A, B}的对数。
题解:当两个数满足A ⊕ B > max{A, B}时,若A>B,则存在A的二进制最高位大于B的最高位(一个为1,另一个为0),另外B的二进制最高位大于A的这一位(一个为1,另一个为0),我们考虑用cnt[i]表示最高位为i+1的数的个数,在考虑每个数时,从这个数低一位开始遍历,直接判断这个数是否满足这一位为0即可累加计数。
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define Mst(a,b) memset(a,b,sizeof(a))
#define fir first
#define se second
#define PII pair<int,int>
#define lowbit(x) x&-x
const int N = 1e5+5;
int cnt[N],a[N];
int cal(int x) {
per(i,31,0) {
if(x>>i&1) return i;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
Mst(cnt,0);
int n;
cin >> n;
rep(i,0,n-1) {
cin >> a[i];
cnt[cal(a[i])]++;
}
int res = 0;
rep(i,0,n-1) {
per(j,cal(a[i])-1,0) {
if(!(a[i]>>j&1)) res += cnt[j];
}
}
cout << res << endl;
}
return 0;
}