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[],n,a[];
ll pos=,cnt=;
int main()
{
scanf("%d",&n);
int k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
int flag=;
for(int j=;j<=;j++)
{
if(a[i]&(<<j)) ans[j]++;
if(ans[j]>) {flag=;}
}
if(!flag)
{
pos+=cnt;
while(k<i)
{
int ok=;
for(int j=;j<=;j++)
{
ans[j]-=(a[k]&(<<j))?:;
if(ans[j]>) ok=;
}
k++;
cnt--;
if(ok) break;
else pos+=cnt;
}
}
cnt++;
}
printf("%lld\n",pos+((+cnt)*cnt/));
return ;
}

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

  1. [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算

    [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算 题目大意: 一个长度为\(n(n\le10^6)\)的序列\(A\).\(m(m\le10^6)\)次 ...

  2. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

  3. CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)

    题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...

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

    题中只有两个条件:任意区间异或值不等于0或m. 如果只考虑区间异或值不等于 0,则任意两个前缀异或值不能相等. 而除了不能相等之外,还需保证不能出现任意两个前缀异或值不等于m. 即 $xor[i]$^ ...

  5. Codeforces Global Round 8 D. AND, OR and square sum(位运算)

    题目链接:https://codeforces.com/contest/1368/problem/D 题意 给出一个大小为 $n$ 的数组 $a$,每次可以选两个下标不同的元素,一个赋为二者相与的值, ...

  6. Codeforces Global Round 8 D. AND, OR and square sum (贪心,位运算)

    题意:有\(n\)个数,选择某一对数使二者分别\(or\)和\(and\)得到两个新值,求操作后所有数平方和的最大值. 题解:不难发现每次操作后,两个数的二进制表示下的\(1\)的个数总是不变的,所以 ...

  7. Go位运算

    目录 &(AND) |(OR) ^(XOR) &^(AND NOT) << 和 >> & 位运算 AND | 位运算 OR ^ 位运算 XOR & ...

  8. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

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

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

随机推荐

  1. Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)

    Permissions Best Practices 在安装的过程中,用户非常easy忽略权限请求. 假设一个用户相应用感觉沮丧或者操心泄漏个人信息,那么这些用户就会不用他或者卸载它. 怎样规避这个问 ...

  2. 如何使用scss/sass

    SCSS 与 Sass 异同:http://sass.bootcss.com/docs/scss-for-sass-users/: 欢迎加入前端交流群来py: 转载请标明出处! 废话不多说,直接进入正 ...

  3. [JZOJ NOIP2018模拟10.20 B组]

    T1:原根(math) 题目链接: http://172.16.0.132/senior/#contest/show/2532/0 题目: 题解: 一个数m原根的个数是$\phi{(\phi{(m)} ...

  4. 12.boost有向图无向图(矩阵法)

    #include <iostream> #include <boost/config.hpp> //图 #include <boost/graph/adjacency_m ...

  5. xBIM 基础03 基本模型操作

    系列目录    [已更新最新开发文章,点击查看详细]  本篇将使用基本的代码示例来表示如何使用xBIM.我们将介绍持久存储的四个基本功能,即 CRUD(创建,检索,更新和删除).以下示例通常适用于IF ...

  6. 关于如何让cell一直保持选中?

    在M上 1.cell的展示,一直都是依靠数据源的支持.所以,必须要在数据源里面新增bool,默认为false 在V上 2.cell的setModel方法里面,将数据源的新增bool赋值为cell的是否 ...

  7. Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 在这篇博客里,我采用了非官网的安装步骤,来进行安装.走了弯路,同时,也是不建议.因为在大数据领域和实际生产里,还是要走正规的为好. Ubuntu16.04下Mongodb(离线安 ...

  8. Nashorn——在JDK 8中融合Java与JavaScript之力--转

    原文地址:http://www.infoq.com/cn/articles/nashorn 从JDK 6开始,Java就已经捆绑了JavaScript引擎,该引擎基于Mozilla的Rhino.该特性 ...

  9. java日期类型与字符串类型的相互转换

    package cn.zwq.convert; import java.text.ParseException; import java.text.SimpleDateFormat; import j ...

  10. 【原创】Google的文本内容对比代码

    /* * Diff Match and Patch * * Copyright 2006 Google Inc. * http://code.google.com/p/google-diff-matc ...