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 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
题目的意思是从给出的所有数中选两个,如果这两个的位异或之后的值大于两个的最大值,那么说明这两个数符合要求
然后求的是所有符合要求的可能数
我这里是用与运算写的
补充一下 9&8=8,10&9=8,17&8=0,7&8=0 大家看懂了把 够直观了
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<cmath> #define ls (u<<1) #define rs (u<<1|1) #define maxn 100010 #define ll long long #define INF 1e9 using namespace std; #define max(a,b) (a)>(b)?(a):(b) #define min(a,b) (a)<(b)?(a):(b) int a[maxn]; int b[35]; int main() { int T; scanf("%d",&T); while(T--){ memset(b,0,sizeof b); int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); for(int j=31;j>=0;j--){ if((1<<j)&a[i]){//把每个数和2的倍数与运算之后的值存起来,因为10&9=8,10&8=8,意思是所有比8大比16小的数两两与运算之后都等于8
//之所以这样可以是因为在(2^3)-(2^4)之间所有的数异或之后都小于两个数,因为异或之后最高位肯定为0
b[j]++; break; } } } int ans=0; for(int i=0;i<n;i++){ int j; for(j=31;j>=0;j--){ if(a[i]&(1<<j)){ break; } } for(;j>=0;j--){ if(!(a[i]&(1<<j))){//所有比2的j次方小的2的次方数与a[i]与运算都为0,这样可以算出在1-2^j次方之间有多少数,即多少个可能性 ans+=b[j]; } } } printf("%d\n",ans); } return 0; }