牛客小白月赛25-J异或和之和

传送门

题意:给出n个数,求所有3元组的异或的和。

思路:考虑一个单个bit上三个数异或后的结果,只有当:
三个数全为1:1 1 1 和
一个为1,两个为0 : 1 0 0 ;才会在此位上给结果作贡献。
最大的数为1^18,所以我们直接存上63个位置上1的个数和0的个数,然后用组合数直接求这位上的贡献就可以了。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;

const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const LL mod = 1000000007;

int num[63][2];

void solve() {
    int n; cin >> n;
    for(int i = 1; i <= n; i++) {
        LL t; cin >> t;
        for(int j = 0; j < 63; j++) {
            if((1LL << j) & t) num[j][1]++;
            else num[j][0]++;
        }
    }
    LL res = 0;
    for(int i = 0; i < 63; i++) {
        LL bits = (1LL << i);
        LL n1 = num[i][1], n0 = num[i][0];
        res += (bits % mod * (n1 * (n1 - 1) * (n1 - 2) / 6 % mod) % mod);
        res %= mod;
        res += (bits % mod * (n1 * n0 * (n0 - 1) / 2 % mod) % mod);
        res %= mod;
    }
    cout << res << endl;
}

int main() {
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值