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 A ⊕ B, 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. A ⊕ B > max{AB}).

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 Tindicating 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 ithinteger denotes the skill level of ith student. Every integer will not exceed 109.

<h4< dd="">Output

For each case, print the answer in one line.

<h4< dd="">Sample Input

2
3
1 2 3
5
1 2 3 4 5

<h4< dd="">Sample Output

1
6
这题真的做的我失去梦想,成为了一条咸鱼- -;
题目很好理解,就是求有多少种 两个数的异或值大于这两个数 的情况。
赤裸裸的二进制,然鹅,我和我队友都不知道咋处理。
突然我灵光一闪,对他说,我们打表找规律吧!
结果我还真找到了规律,写了一大圈子,想了几个样例还全过了,信誓旦旦交上去 --WA。
(哇的一声哭出来)
然后一脸懵逼对着打的表想样例,咋输,咋对。
又进行 一系列 真·玄学debug 法 果不其然全部 -- WA。
(尴尬又不失礼貌的微笑)

...

好了,回归正题。
这题的正解自然是用二进制思想,思路是判断什么情况下,两个数的异或值会大于这两个数。
显然 1^1=0 1^0=1 0^0=0 那么设需要判断的两个数为a, b且 a<b(如果a=b,则a^b=0)
因为1^0=1,那么只要a的最高位对应在b中的值为0,则满足条件。这样讲比较抽象,举个例子:
b=10110,那么什么情况下 a^b>b 呢?肯定是当a的最高位在b的从左到右第2位,第5位时才会a^b>b,即:
b:   10110 10110
a:   1xxx 1
异或值:11xxx  10111

是吧,这样情况下的异或值都要大于a和b,也就是说只需要设一个book[Max],把每个数的首位位置记下来,再根据这个记录,判断是否满足情况。
具体看代码:
  
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int M = 111111;
const int Mx=33;
int nu[M];
int book[Mx]; //记录最高位的位置
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
memset(book,0,sizeof(book));
int n,ans=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>nu[i];
int l=31; //题目中说数据不超过1e9
while(!(nu[i]&(1<<l))) l--; //从第31位进行进行与运算,
book[l]++; //找到该数二进制首位后,记录
}
for(int i=0;i<n;i++){
int l=31;
while(!(nu[i]&(1<<l))) l--;//找nu[i]的二进制首位
while(l--){ //找到二进制首位后,继续遍历
if(!(nu[i]&(1<<l))) //找到该数二进制中的所有0
ans+=book[l]; //把该位是0的book[l]的所有记录相加,便是答案
} }
cout<<ans<<endl;
}
return 0;
}

  

这题的题解我看的是这个:http://blog.csdn.net/ZzebraA/article/details/51272652

zoj-3870 (二进制)的更多相关文章

  1. 位运算 ZOJ 3870 Team Formation

    题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...

  2. (二进制 异或)Team Formation --ZOJ --3870

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870 http://acm.hust.edu.cn/vjudge/ ...

  3. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  4. 【ZOJ 3870】 Team Formation

    题意 n个数,找出有几对a.b 符合 a ^ b > max(a,b) .^表示异或号 分析 对于数a,如果它的二进制是: 1 0 1  0 0 1,那么和它 ^ 后 能比他大的数就是: 0 1 ...

  5. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  6. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...

  7. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  8. zoj 3870

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5518 题意:n个数,从中选出两个数,问这两个数的异或值大于两个数较大 ...

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

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

  10. ZOJ - 3870-Team Formation二进制,位运算

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870 题意:找出一个数列中的两个数,所有通过异或和使得结果同时大于 ...

随机推荐

  1. Hadoop 专栏 - MapReduce 入门

    MapReduce的基本思想 先举一个简单的例子: 打个比方我们有三个人斗地主, 要数数牌够不够, 一种最简单的方法可以找一个人数数是不是有54张(传统单机计算); 还可以三个人各分一摞牌数各自的(M ...

  2. powershell中的cmdlet命令

    Add-Computer 向域或工作组中添加计算机. Add-Content 向指定的项中添加内容,如向文件中添加字词. Add-History 向会话历史记录追加条目. Add-Member 向 W ...

  3. Py-面向对象,组合,继承

    面向对象 只有特定对象能使用特定的几个方法对象=特征+动作 def dog(name,gender,type): #狗的动作 def jiao(dog): print('一条狗%s,汪汪汪' %dog ...

  4. ovs-ofctl命令

    用于监控和管理 OpenFlow 交换机. 1. 交换机管理命令 查看交换机信息: ovs-ofctl show s1  查看交换机流表: ovs-ofctl dump-tables s1 查看端口信 ...

  5. V2版的接口在V3版里面都能找到对应接口 数据结构

    开发文档 - 微信支付商户平台 https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/api.shtml 版本选择 关闭 V2版接口和V3版接口实际 ...

  6. java中List元素移除元素的那些坑

    https://blog.csdn.net/javageektech/article/details/96668890  List  的迭代器类 采用倒序移除 jdk1.8的写法 public sta ...

  7. mysql8.0.19忘记密码

    1.管理员打开cmd窗口 2.输入net stop mysql,停止mysql服务 3.开启跳过验证密码的mysql服务 用记事本打开 输入skip-grant-tables ,保存 4.管理员打开新 ...

  8. 20201104gryz模拟赛解题报告

    写在前面 \(Luckyblock\) 良心出题人, 题面好评 T1还是蛮简单的,用一个栈来维护就能过(某天听说 \(Luckyblock\) 出了套题,T1是个考栈的,看来就是这道了 注:栈的清空只 ...

  9. LOJ10145郁闷的出纳员

    传送门:https://loj.ac/problem/10145 简单的平衡树 ------------------------------------ 1 #include<bits/stdc ...

  10. Kubernetes (yaml 文件详解)

    # yaml格式的pod定义文件完整内容:apiVersion: v1       #必选,版本号,例如v1kind: Pod       #必选,Podmetadata:       #必选,元数据 ...