解法
, 只用考虑
前面有多少个
满足条件。
考虑aj最高位1在h,如果ai的最高位在h之前,或者在h之后,那么异或的值都大于并的值,所以要让并且大于异或,就必须是ai的最高位1也在h
。
代码
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug_in freopen("in.txt","r",stdin)
#define debug_out freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll maxM = 1e6+10;
const ll inf = 1e8;
const ll inf2 = 1e17;
template<class T>void read(T &x){
T s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
x = s*w;
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template <typename ... T>
void DummyWrapper(T... t){}
template <class T>
T unpacker(const T& t){
cout<<' '<<t;
return t;
}
template <typename T, typename... Args>
void pt(const T& t, const Args& ... data){
cout << t;
DummyWrapper(unpacker(data)...);
cout << '\n';
}
//--------------------------------------------
int T,N;
int cnt[50];
int a[maxn];
void solve(){
ll ans = 0;
for(int i = 1;i<=N;i++){
int high = -1;
for(int j = 31;j>=0;j--){
if((a[i]>>j)& 1){
high = j;
break;
}
}
ans += cnt[high];
cnt[high]++;
}
printf("%lld\n",ans);
}
int main(){
// debug_in;
read(T);
while(T--){
read(N);
for(int i = 0;i<40;i++) cnt[i] = 0;
for(int i = 1;i<=N;i++) read(a[i]);
solve();
}
return 0;
} 
京公网安备 11010502036488号