跳到正文

题解

AT_arc135_c 题解

Melo.VVちゃん约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年7月30日
收录
2025年12月12日
更新
2026年11月5日
篇幅
约 3 分钟

版本

  1. v1当前

哇啊哦,原来是神秘诈骗题。

我们考虑当前经过一系列异或操作之后的序列的形式 a1⊕k,a2⊕k⋯an⊕ka_1\oplus k,a_2\oplus k\cdots a_n\oplus k,此时如果我们再异或上某一个位置的数,也就是 ai⊕ka_i\oplus k,可以得到 a1⊕ai,a2⊕ai⋯0⋯an⊕aia_1\oplus a_i,a_2\oplus a_i\cdots 0\cdots a_n\oplus a_i,也就是说,无论操作多少次,达到的效果一定可以只通过一次异或操作达到。

问题转化为在序列里找一个数,让整个序列都异或上这个数,求操作后序列最大的和(可以不干)。

考虑异或的性质,对于两个数,显然我们只需要知道当前两个数每一个二进制位上的数是否不同就知道最后的异或和,那么多个数的时候也同理。

只需要开一个数组,记录对于每一个二进制位,原序列中有多少数这一位为 11,然后暴力枚举 ii,遍历 aia_i 的每一个二进制位,看看原序列中有多少数在这一位上和他不一样,算一下答案,就做完了。

Code

C++
#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 ;
}
原文