ZOJ 3870

来源:互联网 发布:女人不孕不育网络咨询 编辑:程序博客网 时间:2024/06/06 20:13
Team Formation

Time Limit: 2 Seconds      Memory Limit: 131072 KB

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 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

231 2 351 2 3 4 5

Sample Output

16
分析: 设两个数字 a b 如果a^b>max(a,b)的话,那么随便取a,b中的一个数字,比如选的是a吧- -,那就要求得a的1最高位置,如果a的最高位置对应b的位置是0的话,就说明这两个数字是一个队伍的。所以我们对于每一个输入的数字,都得出他的最高位置k并且记录最高位置为k的元素总个数,然后遍历输入的数字,对于每个遍历的数字,求得他的第一个0,求和最高位置1的元素总数。下面给出代码:
#include<stdio.h>#include<string.h>using namespace std;int arr[100008];int jl[50];void cal(int x){    int n=32;    while(n--)    {        if((1<<n)&x)        {            jl[n]++;            return;        }    }}int main(){int n;scanf("%d",&n);while(n--){    int x;    int sum=0;    scanf("%d",&x);    memset(jl,0,sizeof(jl));    for(int i=0;i<x;i++)    {        int temp;        scanf("%d",&temp);        cal(temp);        arr[i]=temp;    }    for(int i=0;i<x;i++)    {        int n=32;        while(n--)        {            if((1<<n)&arr[i])                break;        }        while(n--)        {            if(((1<<n)&arr[i])==0)            {                sum+=jl[n];            }        }    }    printf("%d\n",sum);}}


1 0