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

Xor Sum 2(位运算)相关推荐

  1. codeforces: D. AND, OR and square sum【位运算and和or的诡计】

    分析 x和y变成x|y , x&y 他们的和不变的,然后我们直到这样变只会让他们的差放大,放大的话平方和肯定更大 对于n个元素而言,多次and和or并不会改变每个位上的1的总数,我们先把每个位 ...

  2. UVA12716 LA6657 GCD XOR【GCD+位运算+前缀和】

    Given an integer N, find how many pairs (A, B) are there such that: gcd(A, B) = A xor B where 1 ≤ B ...

  3. 位运算的应用----->实现加减乘除

    首先我们想想位操作符->  ^ (异或) 异或可以相当于无进位相加 同时我们的进位为信息也可以用 (x & y)<<1来表示 那么我么的"+"可以相当于是 ...

  4. AtCoder - arc098_b Xor Sum 2(尺取+位运算)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求 AlxorAl+1xor...xorAr=Al+Al+1+...+ArA_l\ xor\ A_{l+1}\ xor\ ...\ ...

  5. 位运算--异或运算XOR

    c++编程中的基础数学问题 02位运算–异或 今天分享一下在编程当中尤其是在数学部分非常重要的一个部分支–位运算.别看位运算就那么几个简单的运算符号,如果能好好利用就能特定的问题中大放异彩. 这里我们 ...

  6. 【位运算 异或】51nod区间xor

    [位运算 异或]51nod区间xor 题目 思路 O(1)求异或前缀和 证明博客可以看这个 代码 #include<bits/stdc++.h> using namespace std; ...

  7. CodeForces - 1174D Ehab and the Expected XOR Problem(构造+思维+位运算)

    题目链接:点击查看 题目大意:给出一个 n,再给出一个 x,要求构造一个数列,满足该数列的所有子串的异或和都不等于 0 且都不等于 x,在满足上面的条件下尽可能长 题目分析:因为这个题目最终的目标是需 ...

  8. 位运算详解+竞赛常见用法总结

    目录 一.位运算详解 二.位运算应用 1.快速幂 2.给定一个数组A, 长度为n,求下面这段程序的值 3.数数字 4.数数字 2 5.nim博弈问题: 6.树状数组 7.判断一个数x是不是2的某次方 ...

  9. 【面试锦囊】位运算介绍与经典例题总结

    原创公众号:bigsai 原创不易,如果有收获请不要吝啬你的赞赞! 文章已收录在 全网都在关注的数据结构与算法学习仓库 欢迎star 前言 位运算隐藏在编程语言的角落中,其神秘而又强大,暗藏内力,有些 ...

最新文章

  1. 谷歌旗下DeepMind开发出编程机器人,已达人类程序员平均水平!
  2. Oracle优化02-锁和阻塞
  3. 中国湿敏元器件市场研发方向预测与前景趋势研究报告2022版
  4. 动手实现深度学习pytroch版
  5. 【安卓开发 】Android初级开发(五)自定义View
  6. wince手机投屏代码_除了 iOS,这些手机系统你肯定没用过
  7. Django中的视图(view)
  8. 以父之名想表达什么_科幻电影《猩球崛起3:终极之战》究竟想表达什么?
  9. Linux的常用基础命令
  10. Firefox的缓存问题
  11. 计算机代码坑人小程序bat,运用bat写的整人小程序有哪些?
  12. c语言实验报告模板电子版,c语言实验报告模板1.pdf
  13. 一文读懂配置管理(CM)
  14. Linux 日志系统、auditd用户审计、kdump故障定位
  15. 初学JAVA GUI
  16. 网络传输大端序_大端 小端和网络字节序说明
  17. RK键盘出现上下左右键对应890←输出,Del键输出q,q键有删除功能解决
  18. restTemplate请求设置请求头信息
  19. ​[c/c++后台开发面经系列]4 Zoom面经(含答案)
  20. 小米android11适配计划,小米、vivo、一加等宣布Android 11 beta版本适配计划

热门文章

  1. SuperMap.IS.AjaxScript之常用功能实现研究及使用
  2. 【Java从0到架构师】Redis 进阶 - 持久化(RBD、AOF)、高可用(主从复制、哨兵机制、Cluster)
  3. 软件_避免用chrome进行网站配置调试[博]
  4. 前端显示文本时的格式设置
  5. python学习精华——成长篇
  6. docker安装启动mysql5.6_mysql5.6在ubuntu下的docker中安装的方法详解
  7. python35安装教程_python详细安装教程,非常值得看的一篇文章
  8. ROS入门 工程结构
  9. MDC功能软件-感知融合算法介绍
  10. 饭卡 01背包 DP