• 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 (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
    ",pos+((1+cnt)*cnt/2));
        return 0;
    }
  • 相关阅读:
    二叉树前中后、层次遍历
    二叉查找树-插入、查找某个值节点、查找前驱或后继、删除
    查找算法-二分、插值、斐波那契
    8个排序算法
    split函数实现
    strlen、strcmp、strcat、strlen、memmove
    thinkphp5 404 file_put_contents 无法打开流:权限被拒绝
    小程序之旅——第六站(模板首页)
    小程序之旅——第五站(登陆)
    小程序之旅——第三站(数据库用户权限设计)
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9094400.html
Copyright © 2020-2023  润新知