HDU6411 带劲的and和
并查集 + 位运输
先用并查集处理每个联通块,并把权值排序。
然后对于 a * (c & d) 可以把c和d才成二进制,同样位置上存在1才对答案有贡献,所以我们对每个联通块,枚举每个位置是1的数量,算出贡献就好了。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int ret = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
return w ? -ret : ret;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template <typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template <typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 100005;
const int MOD = 1e9 + 7;
int _, n, m, v[N], parent[N], t[30];
vector<int> vi[N];
int find(int p){
while(p != parent[p]){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
bool isConnect(int p, int q){
return find(p) == find(q);
}
void merge(int p, int q){
int pRoot = find(p), qRoot = find(q);
if(pRoot == qRoot) return;
parent[qRoot] = pRoot;
}
void build(){
for(int i = 0; i <= n; i ++){
parent[i] = i;
vi[i].clear();
}
}
int main(){
for(_ = read(); _; _ --){
n = read(), m = read(), build();
for(int i = 1; i <= n; i ++) v[i] = read();
for(int i = 1; i <= m; i ++){
int x = read(), y = read();
if(isConnect(x, y)) continue;
merge(x, y);
}
for(int i = 1; i <= n; i ++) vi[find(i)].push_back(v[i]);
for(int i = 1; i <= n; i ++) sort(vi[i].begin(), vi[i].end());
ll ans = 0;
for(int i = 1; i <= n; i ++){
if(vi[i].size() <= 1) continue;
full(t, 0);
for(auto val: vi[i]){
for(int j = 0; j < 32; j ++){
if(val & (1 << j)){
ans = (ans % MOD + 1LL * t[j] * val % MOD * (1 << j) % MOD) % MOD;
t[j] ++;
}
}
}
}
printf("%lld\n", ans);
}
return 0;
}