Description

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 AB, 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. AB > 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

这道题思路一次进入,直接1A。关键是抓住位运算的特点。对于亦或运算1和0运算的结果还是1,0和0运算的结果还是0,而1和1运算结果会变成0。所以对于一个二进制数A,比如1001001.那么对于B,不妨设A>B,要想让A^B > A,必然B中的第一位1要和A中的0进行亦或运算。所以对于一个二进制位固定长度的B,只要第一位1和A中的0进行亦或,那么答案一定是变大的。

所以只需要枚举A中0的位置,然后找其二进制位对应长度的B的个数即可。而且预处理计算出二进制各长度的数的个数,并保存在cnt数组里,加速了计算。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; bool cmp(int a, int b)
{
return a > b;
} int n, a[100005], l[100005];
int cnt[40]; int GetLen(int n)
{
int len = 0;
while (n)
{
len++;
n >>= 1;
}
return len;
} void Init()
{
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; ++i)
{
l[i] = GetLen(a[i]);
cnt[l[i]]++;
}
} LL Work()
{
int len;
LL ans = 0;
for (int i = 0; i < n; ++i)
{
len = l[i];
int j;
for (j = len-1; j >= 0; j--)
{
if ((a[i] & (1<<j)) == 0)
{
ans += cnt[j+1];
}
}
}
return ans;
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a+n, cmp);
Init();
printf("%lld\n", Work());
}
return 0;
}

AndyQsmart ACM学习历程——ZOJ3870 Team Formation(位运算)的更多相关文章

  1. ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...

  2. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  3. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  4. ZOJ 3870 Team Formation 位运算 位异或用与运算做的

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...

  5. ZOJ3870 Team Formation

    /** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...

  6. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  7. ACM学习历程—HDU1719 Friend(数论)

    Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...

  8. ios开发学习笔记004-进制与位运算

    进制 二进制   0 1组成,封2进1 八进制 0-7组成,封8进1 十进制 0-9组成,封10进1 十六进制 0-15组成,封16进1 printf以不同进制形式进行输出 变量的内存地址形式 变量在 ...

  9. ACM学习历程—计蒜客15 单独的数字(位运算)

    http://nanti.jisuanke.com/t/15 题目要求是求出只出现一次的数字,其余数字均出现三次. 之前有过一个题是其余数字出现两次,那么就是全部亦或起来就得到答案. 这题有些不太一样 ...

随机推荐

  1. Source-php-request-2

    php比較坑的地方就是实现相同的目的,能够使用超级多种手段.比方(file_get_contents和fopen以及如今提到的curl以及fsockopen当然还有socket)这对于一个经验少的程序 ...

  2. mysql忘记root密码且忘了安装目录如何修改root密码

    问题背景 很久之前在本机上安装mysql,也没用过(主要是用Oracle),导致root密码忘记.更严重的是,连自己的安装目录都忘记了. 遇到的问题 1.在任务管理器可以找到mysql的服务已经起来, ...

  3. Getting Started with the G1 Garbage Collector(译)

    原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...

  4. maven新建项目报错

    创建了一个maven项目,报错 Errors occurred during the build. Errors running builder 'Maven Project Builder' on ...

  5. eclipse集成tomcat改动字符集參数

    问题: 在eclipse 4.4(Luna)中集成tomcat时,直接改动原tomcat文件夹中的配置文件,不起作用. 有时.我们会修改字符集參数为utf-8,以解决中文乱码问题,修改之后依旧乱码-- ...

  6. iOS对象(数组)转化为JSon字符串

    - (void)seabc { NSArray *arry=[NSArray arrayWithObjects:@"0081",@"0082",@"0 ...

  7. JBoss AS 7之域名直接訪问配置(The Return Of The King)

    1.6域名直接訪问配置 部署JBoss的时候,默认情况下訪问是须要加项目名字才可以訪问的. 可是怎样才可以"IP+port"訪问呢?以下将解说怎样配置. 配置域名訪问须要在项目以下 ...

  8. uva--10714+找规律

    题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...

  9. activemq 安装-单点

    一,准备工作:首先安装jdk1.7及其以上版本,此环境安装的是jdk-1.8   二.搭建activemq 环境:  192.168.9.25         centos6.5            ...

  10. Angular入门(二) 服务

    目的:为了不再把相同的代码复制一遍又一遍,我们要创建一个单一的可复用的数据服务,并且把它注入到需要它的那些组件中. ※  文件命名约定:服务名称的小写形式(基本名),加上.service后缀,如果服务 ...