首页 > 其他分享 >Codeforces Round #672 (Div. 2) ProblemB

Codeforces Round #672 (Div. 2) ProblemB

时间:2022-11-18 11:03:21浏览次数:49  
标签:int positive Codeforces vis test integer Div include 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;
}


标签:int,positive,Codeforces,vis,test,integer,Div,include,ProblemB
From: https://blog.51cto.com/u_9368800/5867930

相关文章

  • Codeforces Round #672 (Div. 2) Problem A
    今日份的题目。(指9月24日,因为比赛从晚上10点半持续到12点半)问题A是水题,题面如下(大半夜了,就不翻译了,赶着睡觉)(其他题目明天再发)Wheatleydecidedtotrytomakeatestcha......
  • codeforces 2000左右dp题目练习
    栾巨的dp题单,刷完他要v我500可以提升dp水平。1.YaroslavandTwoStrings白给题,令\(f_{i,0/1,0/1}\)表示前\(i\)位,有无小于,有无大于,根据每位的字符转移即可。2.To......
  • SVG Line Between Divs (multi-point)
     <!doctypehtml><html><head><metacharset="utf-8"><title>SVGLineBetweenDivs(multi-point)</title><style>html,body{margin:0;padding:0;}......
  • Codeforces Round #828 (Div. 3) A~F
    A签到点击查看代码#include<bits/stdc++.h>#definelllonglongusingnamespacestd;constintN=2e5+10;intn;map<int,char>m;inta[N];chars[N];i......
  • Codeforces 1646 D. Weight the Tree
    题意给你n个节点的树,让你给每个节点进行赋值,并且赋的值需要为正整数;同时当一个节点的值等于所有邻居节点的值的和时,这个点为好点;求出一组赋值情况,满足树的好点个数最大......
  • WEB安全DIV+CSS基础
    0x001DIV+CSS介绍层叠样式表(英文全称:CascadingStyleSheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机......
  • CodeForces - 372C Watching Fireworks is Fun
    题意:有n个点,其中m个要放烟花。每个放烟花的点有属性b[i],放的时间t[i]和位置a[i]。假设放烟花的时候你在位置x,那么可以获得快乐b[i]-|x-a[i]|。你走来走去地看烟花,起始位置......
  • Codeforces Round #833 (Div. 2)-B
    B题目链接:https://codeforces.com/contest/1748/problem/B Whatisthemaximumpossiblelengthadiversestring?100!10个数字每个出现10次暴力n*102下见代码#......
  • B. Diverse Substrings
    题目链接:Problem-B-Codeforces输入71727741010501100639999652345618789987887987998798输出1210121015106题目大意就是给出T个用例给出一个长度为n,只包含'0'......
  • Codeforces Gym 100958 E Cellular Automaton (Makoto rng_58 Soejima contest) 题解
    题目链接其实"序列中1的数量有限"是一个非常重要的条件。这意味着我们可以找到序列中的第一个1和最后一个1。考虑这样一件事情:初始时我们把一个长度为\(2^{2w+1}\)的"滑......