ARC098 D - Xor Sum 2

传送门:点击打开链接

题目大意:给你一个长度为n的整数序列,让你求出满足以下条件的(l, r)的对数:a[l] + a[l+1] + ... + a[r] == a[l] ^ a[l+1] ^ ... ^a[r]

解题思路:官方给出的题解就很给力了,a + b - a ^ b == 2(a & b),即当a & b == 0的时候a + b == a ^ b(其实不知道这个式子也能想出来),然后现在就找一个区间里面能否满足对于一个二进制位置这个区间中所有的数最多在这个位置上只有1个为1.有一些转换看代码,表达能力没那么好....

官方题解传送门:点击打开链接(让我翻译是不可能翻译的啦hh)

tip:ans开long long 否则会爆

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = (int)2e5 + 5;
const int mod = (int)1e9 + 7;
using namespace std;

int a[maxn];

int main()
{
	int n; scanf("%d", &n);
	int l = 1;
	ll ans = 0, sum = 0;
	for(int r = 1; r <= n; r++){
		scanf("%d", &a[r]);
		while((sum ^ a[r]) != sum + a[r]) sum ^= a[l], l++;
		sum ^= a[r];
		ans += (r - l + 1);
	}
	printf("%lld\n", ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值