Codeforces Round #672 (Div. 2) ProblemB

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值