题意:给出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;
}