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≤lrN) 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

  • 1≤N≤2×105
  • 0≤Ai<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 (1≤lrN) that satisfy the condition.


Sample Input 1

4
2 5 4 6

Sample Output 1

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

9
0 0 0 0 0 0 0 0 0

Sample Output 2

45

Sample Input 3

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

Sample Output 3

37转化成位运算,每位只能有一个
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int ans[26],n,a[200006];
ll pos=0,cnt=0;
int main()
{
    scanf("%d",&n);
    int k=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        int flag=1;
        for(int j=0;j<=20;j++)
        {
            if(a[i]&(1<<j)) ans[j]++;
            if(ans[j]>1) {flag=0;}
        }
        if(!flag)
        {
            pos+=cnt;
            while(k<i)
            {
                int ok=1;
                for(int j=0;j<=20;j++)
                {
                    ans[j]-=(a[k]&(1<<j))?1:0;
                    if(ans[j]>1) ok=0;
                }
                k++;
                cnt--;
                if(ok) break;
                else pos+=cnt;
            }
        }
        cnt++;
    }
    printf("%lld\n",pos+((1+cnt)*cnt/2));
    return 0;
}

原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9094400.html

时间: 2024-11-06 21:00:31

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

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个整数,第

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

hdu 4825 xor sum(字典树+位运算)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 4144    Accepted Submission(s): 1810 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

[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.用乘除代替加减,但是一想,

2014百度之星第三题Xor Sum(字典树+异或运算)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 4445    Accepted Submission(s): 652 Problem Description Zeus 和 Prometheus 做了一个游戏.Prometheus 给 Zeus 一个集合,集合中包括了N个正整数.随后 Prometheus 将向 Ze

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