版本比较
10.30 狂刷水题
正文
增加 55 行删除 0 行
452 行未改动全部展开453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511452 行未改动全部展开 } return 0;}```### [P10853 【MX-X2-T2】「Cfz Round 4」Xor Counting](https://www.luogu.com.cn/problem/P10853)注意到想要满足 $a_i \le (a_i \oplus a_j) $ 就要满足 $a_i$ 的最高位和 $a_j$ 不同,然而 $a_i\leq a_j$,所以 $(a_i)_2$ 的最高位一定是 $1$,对应到 $(a_j)_2$ 上该为 $(a_j)_2$ 一定是 $0$。
我们先写一个函数计算 $a$ 在二进制下最高位的位数,对于每个数,我们开一个桶统计一下最高位的出现次数,再开一个桶表示对于当前数的最高位,有多少个数的这一位是 $1$。 需要特判 $0$ 的情况,容易证明对于 $0$ 任意的数都是合法的。
代码如下:```cpp#include<bits/stdc++.h>using namespace std;const int MAXN=5e5+10;int a[MAXN];long long tot1[30+2],tot2[30+2];int get_high(int a) { if(!a) return -1 ; for(int i=30;i>=0;i--) { if((1<<i)&a) return i ; }}int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); int T; cin>>T; while(T--) { memset(tot1,0,sizeof(tot1)); memset(tot2,0,sizeof(tot2)); int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; tot1[get_high(a[i])]++; for(int j=30;j>=0;j--) { if((1<<j)&a[i]) tot2[j]++; } } long long ans=0; for(int i=1;i<=n;i++) { if(!a[i]) ans+=n; else ans+=tot2[get_high(a[i])]-tot1[get_high(a[i])]; } cout<<ans<<endl; } return 0;}```