codeforce539C. Sasha and a Bit of Relax(异或说明)

本文介绍了一个算法问题,即在一个整数数组中寻找满足特定条件的区间对数量。这些区间需满足长度为偶数且左右半区间的XOR值相等。文章通过分析XOR运算特性,提出了一种有效解决方案,并提供了完整的代码实现。

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

Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn’t an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.

Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l,r) (l≤r). To check if a pair (l,r) is a funny pair, take mid=l+r−12, then if r−l+1 is an even number and al⊕al+1⊕ …⊕amid=amid+1⊕amid+2⊕…⊕ar, then the pair is funny. In other words, ⊕ of elements of the left half of the subarray from l to r should be equal to ⊕ of elements of the right half. Note that ⊕ denotes the bitwise XOR operation.

It is time to continue solving the contest, so Sasha asked you to solve this task.

Input
The first line contains one integer n(2≤n≤3⋅105) — the size of the array.

The second line contains n integers a1,a2,…,an (0≤ai<220) — array itself.

Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1 is even number.

Examples
Input

5
1 2 3 4 5

Output

1

Input

6
3 2 2 3 7 6

Output
3

Input

3
42 4 2

Output

0

给出一个数组,求在这个数组中有多少个符合要求的区间。要求如下:设区间为(l,r)

  1. l+r-1为偶数
  2. al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar

首先说明XOR运算
1 XOR 1=0 | 0 XOR 0=0
1 XOR 0=1 | 0 XOR 1=1
且XOR满足运算的

  1. 交换律

    a ^ b = b ^ a
    
  2. 结合律

    a ^ (b ^ c) = (a ^ b) ^ c
    
  3. 分配率

    a ^ (b+c) = a ^ b + a ^ c
    
  4. 自反

    a ^ b ^ b = a ^ 0 = a
    

利用自反的性质,可以实现一些小操作(与题目无关),如
交换两个数 : a=a ^ b; b=a ^ b; a=a ^ b;
判断两个数是否相等: a ^ b ==0

回到正题,记
XOR(al,ar)表示 al⊕al+1⊕…⊕amid⊕amid+1⊕amid+2⊕…⊕ar
由异或性质不难得出 A ^ B = C,A = B ^ C
故判断条件可进一步写为
0⊕al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar
0=al⊕al+1⊕…⊕amid⊕amid+1⊕amid+2⊕…⊕ar
0=XOR(al,ar)
这样得出一个结论,若在区间(l,r)上XOR(al,ar)=0,则对于任意的 i 属于区间(l,r) (包括区间端点) ,有XOR(al,ai)==XOR(ai+1,ar)
Ps:如有错误还请指正
记product[i] 表示 XOR(a0,ai),根据异或自反的性质,条件 0=XOR(al,ar) 可变为product[l-1] == product[r]。
由此得知,首先要预处理所给数组,得出product[]

此外,题目中另一个要求为 l+r-1 为偶数。所以l,r不能同奇偶,(l-1),r一定同奇偶。
所以,现在只需要找出有多少同奇偶位置上product[]相同的位置,即当 i%2==j%2 (i<j)时,满足条件product[i] == product[j]的数对(i,j)有多少对


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
const int maxn=1<<21;

int store[2][maxn];
int main(){
    ios::sync_with_stdio(false);

    int len;
    cin>>len;
    memset(store,0,sizeof(store));
    store[0][0]++;
    int temp,num=0;;
    ll ans=0;
    rep(i,1,len+1){
        cin>>temp;
        num=num ^ temp;
        ans+=store[i%2][num];
        store[i%2][num]++;
    }

    cout<<ans<<endl;
    re 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值