Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.
Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.
You are given a positive integer 푛, and an array 푎 of positive integers. The task is to calculate the number of such pairs (푖,푗) that 푖<푗 and 푎푖 & 푎푗≥푎푖⊕푎푗, where & denotes the bitwise AND operation, and ⊕ denotes the bitwise XOR operation.
Danik has solved this task. But can you solve it?
Input
Each test contains multiple test cases.
The first line contains one positive integer 푡 (1≤푡≤10) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer 푛 (1≤푛≤105) — length of the array.
The second line contains 푛 positive integers 푎푖 (1≤푎푖≤109) — elements of the array.
It is guaranteed that the sum of 푛 over all test cases does not exceed 105.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
inputCopy
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
outputCopy
1
3
2
0
0
Note
In the first test case there is only one pair: (4,7): for it 4 & 7=4, and 4⊕7=3.
In the second test case all pairs are good.
In the third test case there are two pairs: (6,5) and (2,3).
In the fourth test case there are no good pairs.
题意:
多少(i,j),i<j,满足a[i]*a[j]>a[i]^a[j]
思路:
两个数的二进制最高位一定不一样。
如果a[i],a[j]二进制最高位一样,那么a[i]&a[j]>a[i]^a[j],否则小于。
所以只要找a[j]前面多少数二进制最高位和他一样就好了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;
int a[maxn];
int sum[40];
int main() {
int T;scanf("%d",&T);
while(T--) {
memset(sum,0,sizeof(sum));
int n;scanf("%d",&n);
ll ans = 0;
for(int i = 1;i <= n;i++) {
scanf("%d",&a[i]);
int pos = 0;
for(int j = 30;j >= 0;j--) {
if(a[i] & (1 << j)) {
pos = j;
break;
}
}
ans += sum[pos];
sum[pos]++;
}
printf("%lld\n",ans);
}
return 0;
}