zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494

The 12th Zhejiang Provincial Collegiate Programming Contest - B
Team Formation

Time Limit: 3 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

2

3

1 2 3

5

1 2 3 4 5

Sample Output

1

6

 

 

分析:

 

任选两个数,异或大于他们两个数

 

思路:对于一个数字的二进制形式,我们与比它小的比较,如果对应位互异,那么肯定是可行的

 

 

 

AC代码:

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <string.h>

 4 #include <stack>

 5 #include <queue>

 6 #include <map>

 7 #include <set>

 8 #include <vector>

 9 #include <math.h>

10 #include <algorithm>

11 using namespace std;

12 #define ls 2*i

13 #define rs 2*i+1

14 #define up(i,x,y) for(i=x;i<=y;i++)

15 #define down(i,x,y) for(i=x;i>=y;i--)

16 #define mem(a,x) memset(a,x,sizeof(a))

17 #define w(a) while(a)

18 #define LL long long

19 const double pi = acos(-1.0);

20 #define Len 20005

21 #define mod 19999997

22 const int INF = 0x3f3f3f3f;

23 

24 int t,n,a[100005],bit[100];

25 

26 int main()

27 {

28     int i,j,k;

29     scanf("%d",&t);

30     w(t--)

31     {

32         mem(bit,0);

33         scanf("%d",&n);

34         up(i,0,n-1)

35         {

36             scanf("%d",&a[i]);

37             for(j = 31;j>=0;j--)

38             {

39                 if(a[i]&(1<<j))

40                 {

41                     bit[j]++;

42                     break;

43                 }

44             }

45         }

46         LL ans = 0;

47         up(i,0,n-1)

48         {

49             if(a[i])

50             {

51                 int l = 31;

52                 w(1)

53                 {

54                     if(a[i]&(1<<l)) break;

55                     l--;

56                 }

57                 w(l>=0)

58                 {

59                     if(!(a[i]&(1<<l))) ans+=bit[l];

60                     l--;

61                 }

62             }

63         }

64         printf("%lld\n",ans);

65     }

66 

67     return 0;

68 }
View Code

 

你可能感兴趣的:(programming)