跳到正文

个人记录

✨[zhoi edu. round 6] Xor Sums of Arrays

zhlzt约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

官方题解

加法之逆运算为减法,异或之逆运算为异或。

于是就是一个显然的预处理前缀和,异或和,再加上 O(n2)O(n^2) 的 dp。

代码实现

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+10;
int prex[maxn],pres[maxn],dp[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<=n;i++){
			int a;cin>>a; dp[i]=0;
			pres[i]=pres[i-1]+a;
			prex[i]=prex[i-1]^a;
		}
		for(int i=1;i<=n;i++){
			for(int j=0;j<i;j++){
				int val1=pres[i]-pres[j];
				int val2=(i-j)*(prex[i]^prex[j]);
				dp[i]=max(dp[i],dp[j]+max(val1,val2));
			}
		}
		cout<<dp[n]<<'\n';
	}
	return 0;
}
原文