码迷,mamicode.com
首页 > 其他好文 > 详细

TZOJ--4986: Team Formation(二进制枚举)

时间:2018-11-27 22:05:02      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:href   his   kill   i++   not   arch   from   use   ted   

4986: Team Formation 

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

描述

 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.

输入

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

输出

For each case, print the answer in one line.

样例输入

2
3
1 2 3
5
1 2 3 4 5

样例输出

1

6

题目来源

ZJCPC 2015

题目上传者

crq

 

题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4986

 

题目大意:给你n个数字,问 满足(a^b)>max(a,b)的有多少对?

异或:相同为0不同为1,所以只需要枚举二进制中最高位的1所在位置,然后枚举这个数二进制中0所在的位置,计算出这个位置其他数字中是最高位的数字有几个即可。

 

#include<stdio.h>
#include<string.h>
int main()
{
	int t,n,a[100010],b[33];
	scanf("%d",&t);
	while(t--){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int x,i,j;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
			for(j=31;j>=0;j--){
				if(a[i]&(1<<j)){
					b[j]++;
					break;
				}
			}
		}
		int s=0;
		for(i=0;i<n;i++){
			for(j=31;j>=0;j--){
				if(a[i]&(1<<j))break;
			}
			for(;j>=0;j--){
				if(!(a[i]&(1<<j))){
					s+=b[j];
				}
			}
		}
		printf("%d\n",s);
	}
	return 0;
}

  

TZOJ--4986: Team Formation(二进制枚举)

标签:href   his   kill   i++   not   arch   from   use   ted   

原文地址:https://www.cnblogs.com/Anidlebrain/p/10029071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!