J异或之和(好题)
题意
求在n个数中任选3个数的异或和 的总和
思路
计算所有的64位整数的 选3个异或和满足1的个数
该位是1的两种情况:
- 1 1 1
- 0 0 1
- 第一种情况的选法个数有 C X O R [ i ] 3 C_{XOR[i]}^{3} CXOR[i]3
- 第二种情况的选法个数有
C
n
−
X
O
R
[
i
]
2
∗
X
O
R
[
i
]
C_{n-XOR[i]}^{2} * XOR[i]
Cn−XOR[i]2∗XOR[i]
以上两种异或都是1,所以只要求出所有的 选法中 在该位异或和是1的种类即可,然后根据该位上的权值相加即可得出答案
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a) cout<<#a<<" : "<<a<<endl;
#define IOS std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define PAUSE system("pause")
#define sd(a) scanf("%d",&a)
#define sll(a) scanf("%intd",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a) scanf("%lf",&a)
#define sff(a,b) scanf("%lf%lf",&a,&b)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
using namespace std;
typedef long long LL;
const int mod = 1000000007;
// 计算所有的64位整数的 选3个异或和满足1的个数
LL XOR[64];
LL qmi(LL a, LL b, int p) {
LL res = 1;
while(b) {
if(b & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
b >>= 1;
}
return res % p;
}
LL C(LL n, LL m) {
if(n < m) return 0;
LL ans = 1;
for(LL i = 1; i <= m; i ++) {
ans = (ans % mod * (n - i + 1) % mod * qmi(i, mod - 2, mod) % mod) % mod;
}
return ans % mod;
}
int main() {
LL n;
scanf("%lld", &n);
rep(i, 1, n) {
LL x;
scanf("%lld", &x);
LL k = 0;
while(x) {
if(x & 1)
XOR[k] ++;
k ++;
x >>= 1;
}
}
LL sum = 0;
LL k = 1;
rep(i, 0, 64) {
sum = (sum % mod + k * (C(XOR[i], 3) % mod + XOR[i] % mod * C(n - XOR[i], 2) % mod) % mod) % mod;
sum %= mod;
k = (k << 1) % mod;
}
cout << sum % mod;
return 0;
}