Xor and Sum(位运算)

题目描述

给定一个大小为N的数组A,第i个元素为Ai。

问有多少的子区间[LR],满足区间数值异或和等于区间数值和,即:

Al xor Al+1 xor…xor Ar = Al + Al+1 +…+Ar(l+1表示下标)
      a和b的xor即为a和b二进制表示按位取xor得到新数c的十进制表示
5和12的xor计算如下:

510=01012

(12)10=(1100)2

01012xor11002=(1001)2

(1001)2=(9)10

输入

第一行给定一个整数N。
第二行给定N个整数,第i个数即为Ai。
1≤N≤2×10^5
0≤A_i≤2^30

输出

输出满足条件的子区间LR的数量。

样例输入

10
0 0 740 361 473 0 0 826 479 974

样例输出

18

题解:

xor运算可以视为二进制下没有进位的加法,加法运算本身是有进位的加法。

那么可以简单得出这样一个性质:对于一个区间而言,如果异或和加法答案一样,那么把区间缩小答案肯定还是一样;如果异或和加法答案不一样,那么把区间扩大答案肯定还是不一样。

于是我们就可以枚举区间右端点,去寻找最小的左端点,这个区间异或等于区间和,那么以这个区间右端点的合法区间个数就是区间的长度(左端点往里缩都是合法的)。

这个可以预处理出前缀和还有前缀异或和,用双指针维护出来。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<string>
 6 #include<queue>
 7 #include<stdlib.h>
 8 #include<math.h>
 9 #define per(i,a,b) for(int i=a;i<=b;++i)
10 #define rep(i,a,b) for(int i=a;i>=b;--i)
11 #define inf 0xf3f3f3f
12 #define ll long long int
13 using namespace std;
14 int p[200005];
15 int s[200005];
16 int z[200005];
17 int main()
18 {
19     int m,a;
20     cin>>m;
21     z[0]=0;s[0]=0;
22     per(i,1,m)
23     {
24         cin>>a;
25         s[i]=s[i-1]+a;
26         z[i]=z[i-1]^a;
27     }
28     ll l=0,sum=0;
29     per(i,1,m)
30     {
31         while((z[i]^z[l])!=(s[i]-s[l])) l++;
32         sum+=i-l;
33     }
34     cout<<sum;
35     return 0;
36 }

-

原文地址:https://www.cnblogs.com/jiamian/p/12446120.html

时间: 2024-08-30 01:40:31

Xor and Sum(位运算)的相关文章

CF D. Ehab and the Expected XOR Problem 贪心+位运算

code: #include <bits/stdc++.h> #define N 1000000 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int vis[N],b[N]; void solve() { int n,m,i,j,cur=1,cnt=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&a

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

Xor Sum 2(位运算)

D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There is an integer sequence A of length N. Find the number of the pairs of integers l and r (1≤l≤r≤N) that satisfy the following condition: Al xor Al+1 xor 

[leetcode] Sum of Two Integers--用位运算实现加法运算

问题: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 分析: 这里要求我们不能用加法.减法等运算符来实现加法运算.这里应该使用位运算来实现加法运算,实际上,这也是计算机CPU内部实现加法运算的方案. x XOR y真值表: x y output 0 0 0 0 1 1

hdu 5344 (多校联赛) MZL&#39;s xor --- 位运算

here:    首先看一下题吧:题意就是让你把一个序列里所有的(Ai+Aj) 的异或求出来.(1<=i,j<=n) Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all (Ai+Aj)(1≤i,j≤n) The xor of an array B is defined as B1 xor B2...xor B

通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,

delphi 按位运算 not and or xor shl shr

delphi 按位运算 not and or xor shl shr unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; But

计蒜客15430 XOR Queries(Trie处理位运算问题)

题意: 给出一个长度为n的数组C,回答m个形式为(L, R, A, B)的询问, 含义为存在多少个不同的数组下标k属于[L, R]满足C[k] XOR A >= B(式中XOR为异或运算). T组测试数据. 每组第一行为两个整数n, m.(1 <= n, m <= 5e4). 第二行n个整数表示数组C.(0 <= C[i] <= 1e9). 接下来m行,第i行四个整数L[i], R[i], A[i], B[i](1 <= L[i] <= R[i] <= n,

【模拟+递归+位运算】POJ1753-Flip Game

由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组倒序看作一个二进制数num.我们假设1代表翻转,0代表不翻转,可以发现以下规律:0 xor 1=1,1 xor 1=0;0 xor 0=0,1 xor 0=1,恰巧满足异或运算.我们假设另一个二进制数i∈[0,2^16),通过异或运算就可以模拟出所有清形. 用check和i进行&操作可以求出以哪些位

位运算 使用技巧

位运算简介及实用技巧(一):基础篇 什么是位运算? 程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算说穿了,就是直接对整数在内存中的二进制位进行操作.比如,and运算本来是一个逻辑运算符,但整数与整数之间也可以进行and运算.举个例子,6的二进制是110,11的二进制是1011,那么6 and 11的结果就是2,它是二进制对应位进行逻辑运算的结果(0表示False,1表示True,空位都当0处理): 110 AND 1011 ---------- 0010  -->  2 由于位运算