9月25日的打卡。
为什么又过了零点?因为和朋友去摸鱼了。
今天讲一下昨晚比赛的第二题。
题面如下:
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 n
, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and ai & aj≥ai⊕aj, 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 t
(1≤t≤10
) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n
(1≤n≤105
) — length of the array.
The second line contains n
positive integers ai (1≤ai≤109
) — elements of the array.
It is guaranteed that the sum of n
over all test cases does not exceed 105
.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
Output
1
3
2
0
0
此题的要求是两数的and值大于xor值。实际上大家比较即可发现,若要使该条件成立,则需要保证二者二进制的最高位位数相同。比如110和101.
那么如何知道这些数最高位是哪一位呢?因为是二进制的最高位,所以直接对原数log2处理就好。然后列个vis存储各个最高位的数字个数,然后计算即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 200005
using namespace std;
int T;
int n;
int num;
long long vis[35];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&num);
vis[(int)log2(num)]++;
}
long long step=0;
for(int i=0;i<34;i++){
step+=1LL*vis[i]*(vis[i]-1)/2;
}
memset(vis,0,sizeof(vis));
printf("%lld\n",step);
}
return 0;
}