子段异或

子段异或

http://www.nowcoder.com/questionTerminal/c41f41cd9bdc4d3097b2e754f2f49856

子段异或
https://ac.nowcoder.com/acm/contest/3005/D
图片说明

亦或运算中
连续一段的运算 a[0]^...a[i]^a[i+1]^...^a[i+k]^...^a[n] 中若出现相同的亦或前缀和,
如a[0]^...^a[i]的亦或前缀和 与 a[0]^...^a[i+k]的亦或前缀和相等,则a[i+1]^...^a[i+k]=0
如 1 2 4 7 2 1 中 有1^2=3 1^2^4^7^2^1=3 则1^2^4^7^2^1=0
另配合map食用更佳

#pragma warning (disable :4996)
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 20;

map<ll, int> a;
ll A[N];
ll ans = 0, sum = 0;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%lld", &A[i]);
    a[0] = 1;
    for (int i = 0; i < n; i++) {
        sum = sum ^ A[i];
        ans += a[sum];
        a[sum]++;
    }
    cout << ans << endl;
}
全部评论

相关推荐

10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务