AT4294 题解
EuphoricStar · · 题解
思路
能算得上是贪心(?)
碰到异或一类的位运算的题,通常都考虑拆位算贡献。所以我们从高位往低位算
发现我们如果要让
注意如果个数相等,那么
算出
代码
/*
p_b_p_b txdy
AThousandMoon txdy
AThousandSuns txdy
hxy txdy
*/
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define scd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const int maxn = 100100;
ll n, m, a[maxn];
void solve() {
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
ll x = 0;
for (int j = 60; ~j; --j) {
int cnt0 = 0, cnt1 = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] & (1LL << j)) {
++cnt1;
} else {
++cnt0;
}
}
if (cnt0 > cnt1 && (x | (1LL << j)) <= m) {
x |= (1LL << j);
}
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
ans += (x ^ a[i]);
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}