AtCoder Regular Contest 098 D - Xor Sum 2 区间异或=相加 DP思想

博客介绍了AtCoder Regular Contest 098的一道题目,题号为D - Xor Sum 2。内容涉及寻找满足条件的整数序列连续子序列对(l, r),使得Al xor Al+1 xor ... xor Ar等于Al + Al+1 + ... + Ar。博主通过DP思想解释了如何解决这个问题,强调了异或操作在本题中的等价于加减法的关系,并指出比赛中的解题策略涉及到区间DP的应用。" 102150388,8841117,SQL Server CTE 递归查询详解,"['数据库理论', 'SQL查询']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

There is an integer sequence A of length N.

Find the number of the pairs of integers l and r (1lrN) that satisfy the following condition:

  • Al xor Al+1 xor … xor Ar=Al Al+1 + … Ar

Here, xor denotes the bitwise exclusive OR.

Definition of XOR

Constraints

  • 1N2×105
  • 0Ai<220
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1 A2  AN

Output

Print the number of the pairs of integers l and r (1lrN) that satisfy the condition.


Sample Input 1

Copy
4
2 5 4 6

Sample Output 1

Copy
5

(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition. (l,r)=(1,2) also satisfies the condition, since A1 xor A2=A1 A2=7. There are no other pairs that satisfy the condition, so the answer is 5.


Sample Input 2

Copy
9
0 0 0 0 0 0 0 0 0

Sample Output 2

Copy
45

Sample Input 3

Copy
19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

Sample Output 3

Copy
37

题意:给出n个数,求它的连续子序列中,满足下列公式,(l,r)的对数有多少对

Al xor Al+1 xor … xor Ar=Al Al+1 + … Ar

思路:由题意可以得到,连续子序列,如果在ai这个数不符合公式的话,即之后的符合条件的对数中将不在需要这个元素,所有枚举元素来计算符合公式的对数 。

难以理解的就是异或等效于加法与减法(!!!)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
long long f[maxn], s[maxn];
long long ans;

int main(){
    int n, x; cin >> n;
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        f[i] = f[i-1] + x;    //计算异或前缀和
        s[i] = s[i-1] ^ x;    //计算加法前缀和
    }
    int l = 1;
    ans=0;
    for(int r = 1; r <= n; r++){         //枚举元素
        for(; f[r] - f[l-1] != (s[r] ^ s[l-1]); l++);//如果符合条件的话 则立即退出进行计算
        ans += r - l + 1;
    }
    printf("%lld\n", ans);
    return 0;
}

今天这场Atcoder感觉就是区间+DP的思想

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值