2015 浙江省赛B Team Formation (技巧,动归)

Team Formation

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

N个球员,每个球员有一个水平值,现在要组队,每个队两个人,这个队的水平就是这两个人水平的异或,这个队能打好代表这个异或值大于人一个队员;
问多少种组队方法;两个队不同即为任意一个队员不同;
燕帅给提供的思路,就是记录每个人最高位出现的位置,对于每个人,只需找比这个人小的水平与其异或大于自己的个数和就好了;


位的问题,一半考虑排序从高位到低位

#include <iostream>
#include<cstring>
#include <string>
#include <algorithm>
using namespace std;
long long a[100005];
long long b[25];
int main()
{
    int t;
    cin>>t;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    while(t--)
    {
        int n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);//从小到大排,就只要考虑第一位
        long long sum=0;
        for(int i=1;i<=n;i++)
        {
            int x=a[i];
            int ans=0;
            while(x)
            {
                ans++;
                if(x%2==0)  sum+=b[ans];//如果该位上是0,看前面有多少个1
                x=x/2;//向前进一位
            }
            b[ans]++;//这一位+1,比如1010,在b【4】++;
        }
        printf("%lld
",sum);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/caiyishuai/p/8566673.html