zoj 3870
Description
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 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
题目描述 : 求一段序列中任意两个数,假设是a和b,(a^b)>max(a,b),这样的数对有多少个;
我们通过分析 a^b,发现假设a=110101,b<a(b>a是同样的情形),那么b的二进制长度小于a的二进制的长度,
发现b的最高位的1,如果a的相对应的位置为1,抑或之后的值一定会比a小,如果a的相对应的位置为0,那么抑或之后的值就会比a大;
这样我们就可以进行操作了,
a=110101
有两种方式
1 : b=1 << (5,4,3,2,1,0) ,if((a & b)==0), 说明a相对应b二进制长度的那个位置上的数为0
2 : c=a >> (0,1,2,3,4,5),if((c & 1)==0),说明a的二进制某一对应位的那个位置上的数为0
#include <iostream> #include <cstdio> #include <string.h> #define M 100100 using namespace std; int a[M]; int input[M]; int t; long long n; void init() { memset(a,0,sizeof(a)); memset(input,0,sizeof(input)); } int getnum(int data) { int cnt=0; while(data>=1) { data >>=1; cnt++; } return cnt; } int solve() { int x=0,answer=0,s=0; for(int i=1;i<=n;i++) { x=getnum(input[i]); ++a[x]; } //for(int i=1;i<=3;i++) // printf("%d ",a[i]); for(int i=1;i<=n;i++) { x=getnum(input[i]); //printf("%d\n",x); for(int pos=0;pos<=x-1;pos++) { int p= input[i] >> pos ; if( (p & 1 ) ==0) answer+=a[pos+1]; // printf("%d %d %d\n",pos,answer,input[i]); } } return answer; } int main() { int T; // while(scanf("%d",&T)) // printf("%d\n",1 & T ); scanf("%d",&t); while(t--) { init(); scanf("%d",&n); for(int i = 1 ; i <= n ; i ++) { scanf("%d",&input[i]); } printf("%d\n",solve()); } return 0; }