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. 安装JDK与卸载JDK教程

    卸载JDK 删除JDK的安装目录,也就是删除了主程序(通过环境变量可以找到) 删除java_home的环境变量 删除环境变量path中与java_home相关的 通过DOS命令cmd来检验是否卸载成功 ...

  2. 面试必问:如何实现Redis分布式锁

    摘要:今天我们来聊聊分布式锁这块知识,具体的来看看Redis分布式锁的实现原理. 一.写在前面 现在面试,一般都会聊聊分布式系统这块的东西.通常面试官都会从服务框架(Spring Cloud.Dubb ...

  3. muduo 网络库的整体架构图和一个简化版本的架构设计

    https://blog.csdn.net/adkada1/article/details/54342275 简析 https://blog.csdn.net/amoscykl/article/det ...

  4. WPF和MVVM的结合使用方法,不可错过

    Model:存储数据模型(类) 也在此业务逻辑,主要负责类文件的存储. ViewModel:连接View和Model,借助Command来负责界面的跳转和调用Model中方法来操作Model的数据. ...

  5. odoo-nginx 配置之80端口

    1 upstream odoo { 2 server 127.0.0.1:8069 weight=1 fail_timeout=0; 3 } 4 5 upstream odoo-im { 6 serv ...

  6. vmware安装linux系统,自动建立没选项

    虚拟机安装CentOS自己跳过分区,直接就到最后的软件包安装了 建完系统后不用power on,建完后在edit一下系统参数,应该会看见两个cd, 有一个是vmware自己加的,把那个删除后在开机就可 ...

  7. CentOS 安装TFTP

    1.当然是使用yum安装最直接,一共会安装3个东东tftp.i386tftp-server.i386xinetd.i386[root@localhost CentOS]# yum -y install ...

  8. ACM-古老的密码(排序qsort)

    古老的密码 题目描述:给定两个长度一样且不超过100的字符串,判断是否能把其中一个字符串的各个字母重排,之后对26个字母做一个一一映射,使得两个字符串相同例如,JWPUDJSTVP重排后可以得到WJD ...

  9. 动态规划TG.lv(1) (洛谷提高历练地)

    动态规划TG.lv(1) P1005 矩阵取数游戏 分析:每行不超过80个数字,直接区间DP即可,\(dp[i][j]\)表示区间\([i,j]\)之间取数可以得到的答案,每次向右或者向左扩展即可.但 ...

  10. [CF套题] CF-1163

    CF-1163 传送门 # Penalty A B1 B2 C1 C2 D E F 3 (483) 464 +0 0:06 +1 01:13 +3 01:12 + 01:57 + 01:56 A 第一 ...