zoj--3870--Team Formation(位运算好题)
| Time Limit: 3000MS | Memory Limit: 131072KB | 64bit IO Format: %lld & %llu |
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
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{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
Hint
Source
#include<cstdio>
#include<cstring>
int a[100100];
int num[40];
void sum(int x)
{
int pos=31;
while(pos>=0)
{//1左移31位,&同为1时为1,其他为0
if(x&(1<<pos))//寻找最高的二进制位
{
num[pos]++;
return ;
}
pos--;
}
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum(a[i]);
}
long long ans=0;
for(int i=0;i<n;i++)
{
int pos=31;
while(pos)
{
if(a[i]&(1<<pos))
break;
pos--;
}
while(pos>=0)
{
if(!(a[i]&(1<<pos)))
ans+=num[pos];//num中存储的最高位为pos的数的个数
pos--;
}
}
printf("%lld\n",ans);
}
return 0;
}
zoj--3870--Team Formation(位运算好题)的更多相关文章
- ZOJ 3870 Team Formation 位运算 位异或用与运算做的
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...
- 位运算 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^ ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- ZOJ - 3870 Team Formation(异或)
题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...
- 蓝桥杯---汉字取首字母(位运算 & 水题)
确实题目虽然有点水,但是开始的时候好像还真的没有想到怎么提取出这个编号一不小心感觉可以可以用unsigned char 这种类型,直接转为16进制,但是之后发现虽然第一次在codeblock中还行,但 ...
- 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)
点此看题面 大致题意: 给你\(n\)个\(m\)位二进制数.每组询问给你一个\(m\)位二进制数,要求你从\(0\)开始,依次对于这\(n\)个数进行\(and\)或\(or\)操作,问有多少种方案 ...
- 【BZOJ4300】绝世好题(位运算水题)
点此看题面 大致题意: 给你一个序列\(a\),让你求出最长的一个子序列\(b\)满足\(b_i\&b_{i-1}!=0\). 位运算+\(DP\) 考虑设\(f_i\)表示以第\(i\)个数 ...
- zzulioj--1832--贪吃的松鼠(位运算好题)
1832: 贪吃的松鼠 Time Limit: 3 Sec Memory Limit: 2 MB Submit: 43 Solved: 7 SubmitStatusWeb Board Descri ...
随机推荐
- c#——值类型与引用类型
值类型传的是值 引用类型传的是地址
- asp.net MVC 下拉多级联动及编辑
多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- transform: scale(x,y)
作用: 1)缩放 2)反转 水平翻转:transform: scale(-1,1); 垂直翻转:transform: scale(1,-1); 水平垂直翻转: transform: scale(-1, ...
- img、a标签的使用
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- PHP控制反转(IOC)和依赖注入(DI
<?php class A { public $b; public $c; public function A() { //TODO } public function Method() { $ ...
- IDEA 创建一个普通的java项目
IntelliJ IDEA 如何创建一个普通的java项目,及创建java文件并运行 首先,确保idea软件正确安装完成,java开发工具包jdk安装完成. IntelliJ IDEA下载地址:htt ...
- centOS防火墙
默认防火墙firewall #停止firewall systemcl stop firewall.service #禁止firewall开机启动 systemctl disable firewall. ...
- 非递归全排列 python实现
python algorithm 全排列(Permutation) 排列(英语:Permutation)是将相异物件或符号根据确定的顺序重排.每个顺序都称作一个排列.例如,从一到六的数字有720种排列 ...
- 腾讯云,搭建LAMP服务
lamp (Web应用软件) 编辑 Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被 ...
- 6 DataFrame处理丢失数据--数据清洗
处理丢失数据 有两种丢失数据: · None · np.nan(NaN) 1 None None是Python自带的,其类 ...