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. vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】

    vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...

  2. 机器学习7-模型保存&无监督学习

    模型保存和加载 sklearn模型的保存和加载API from sklearn.externals import joblib 保存:joblib.dump(rf, 'test.pkl') 加载:es ...

  3. 在.NET Core 中使用Quartz.NET

    Quartz.NET是功能齐全的开源作业调度系统,可用于最小的应用程序到大型企业系统. Quartz.NET具有三个主要概念: job:运行的后台任务 trigger:控制后台任务运行的触发器. sc ...

  4. ThreadLocal 原理分析

    用法 ThreadLocal<String> threadLocal = new ThreadLocal<>(); // 无初始值 ThreadLocal<String& ...

  5. Py数据类型—列表,字典,元组

    列表:数据类型list. 写法li=[1,12,9,"sdsad",["ad","dd"] ].用中括号括起来,用逗号分割每个元素列表中元素 ...

  6. jQuery 实现复制功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 就是通过事件方法,在window.loaction.href里追加了参数字符串

    参考博文:https://www.kancloud.cn/digest/yvettelau/137669

  8. 【Source Insight】查找功能 Lookup References 详解

    1.Options Case Sensitive //区分大小写 Whole Words Only //全字匹配查找 Skip Inactive Code //跳过无效代码查找 Skip Commen ...

  9. 这几个小技巧,让你书写不一样的Vue!

    前言 最近一直在阅读Vue的源码,发现了几个实战中用得上的小技巧,下面跟大家分享一下. 同时也可以阅读我之前写的Vue文章 vue开发中的"骚操作" 挖掘隐藏在源码中的Vue技巧! ...

  10. 慕课网金职位 Java工程师2020 百度网盘下载

    百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...