浙江省第十二届省赛 B - Team Formation
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
题意:
输入n个数:任意两个数进行异或,问异或值大于这两个数的最大值的情况共有几种
题解:
首先将每个数的最高位哈希,然后遍历每个数。每个数转化为二进制数,二进制中只要当前位为0,那么异或此位为最高位的数一定比这两个数大。
代码:
#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <vector> #include <map> #include <set> #include <bitset> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <cstdlib> #include <string> using namespace std; #define is_lower(c) (c>='a' && c<='z') #define is_upper(c) (c>='A' && c<='Z') #define is_alpha(c) (is_lower(c) || is_upper(c)) #define is_digit(c) (c>='0' && c<='9') #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define IO ios::sync_with_stdio(0);\ cin.tie(0);\ cout.tie(0); #define For(i,a,b) for(int i = a; i <= b; i++) typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; const ll inf=0x3f3f3f3f; const double EPS=1e-10; const ll inf_ll=(ll)1e18; const ll maxn=100005LL; const ll mod=1000000007LL; const int N = 100000+5; int arr[N]; int ans[32]; int main() { IO int T; cin>>T; while(T--){ memset(ans,0,sizeof(ans)); int n,res = 0; cin>>n; For(i,1,n) { cin>>arr[i]; ans[(int )log2(arr[i])]++; } For(i,1,n){ int bit[32]={0}; int x = arr[i]; while(x) { int x1 = x; x&=x-1; bit[(int)log2(x1-x)] = 1; } For(j,0,(int )log2(arr[i])-1) if(!bit[j]) res +=ans[j]; } cout<<res<<endl; } return 0; }