AT_arc135_c 题解
Melo.VVちゃん · · 题解
哇啊哦,原来是神秘诈骗题。
我们考虑当前经过一系列异或操作之后的序列的形式
问题转化为在序列里找一个数,让整个序列都异或上这个数,求操作后序列最大的和(可以不干)。
考虑异或的性质,对于两个数,显然我们只需要知道当前两个数每一个二进制位上的数是否不同就知道最后的异或和,那么多个数的时候也同理。
只需要开一个数组,记录对于每一个二进制位,原序列中有多少数这一位为
Code
#include <bits/stdc++.h>
#define int long long
#define f(i ,m ,n ,x) for (int i = (m) ; i <= (n) ; i += (x))
template < typename T > inline void read ( T &x ) {
x = 0 ;
bool flag (0) ; char ch = getchar () ;
while (! isdigit (ch)) {
flag = ch == '-' ;
ch = getchar () ;
} while (isdigit (ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48) ;
ch = getchar () ;
} flag ? x = - x : 0 ;
}
const int N = 3e5 + 7 ;
int n ,a[N] ,cnt[77] ,res ;
signed main () {
read (n) ;
f (i ,1 ,n ,1) {
read (a[i]) ;
res += a[i] ;
static int b ; b = a[i] ;
int cur = 0 ;
while (b) {
cnt[cur ++] += (b & 1) ;
b >>= 1 ;
}
}
f (i ,1 ,n ,1) {
int ans = 0 ;
f (j ,0 ,32 ,1) {
int cur = (a[i] & (1ll << j)) ;
if (cur) ans += (n - cnt[j]) * (1ll << j) ;
else ans += cnt[j] * (1ll << j) ;
} res = std :: max (res ,ans) ;
} std :: cout << res << '\n' ;
return 0 ;
}