跳到正文

题解

题解:P12177 [蓝桥杯 2025 省 Python B] 异或和

zzy0618约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P12177

版本

  1. v1当前

和 P7627 是几乎一样的。

依然按位拆贡献,维护 std,0/1st_{d,0/1} 为前面的数二进制下第 dd 位为 0,10,1 的有多少个数。后面的 i−ji-j 相当于一个数重复了 i−ji-j 次,维护一个 tsd,0/1ts_{d,0/1} 为乘上 i−ji-j 这个系数后的按位答案,在每一轮计算后将 tsd,kts_{d,k} 加上一次 std,kst_{d,k} 即可。

值得注意的是最终答案大小高达 102110^{21},需要 int128。

C++
#include<bits/stdc++.h>
#define int long long
#define Int __int128
using namespace std;
int n;Int ans;
int st[21][2],ts[21][2];
void write(Int x){
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1,x;i<=n;++i){
        cin>>x;
        for(int j=20;j>=0;--j)
            ans+=ts[j][((x>>j)&1)^1]*(1ll<<j),
            ++st[j][(x>>j)&1],
            ts[j][0]+=st[j][0],ts[j][1]+=st[j][1];
    }write(ans);
    return 0;
}
原文