码迷,mamicode.com
首页 > 其他好文 > 详细

Xor Sum 2(位运算)

时间:2018-05-26 21:51:39      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:AC   The   xor   有一个   art   answer   class   name   xor sum   

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 (1lrN) 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

  • 1N2×105
  • 0Ai<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 (1lrN) 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;
}

 

Xor Sum 2(位运算)

标签:AC   The   xor   有一个   art   answer   class   name   xor sum   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!