Codeforces 1053 B

题:http://codeforces.com/contest/1053/problem/B

题意:给定n个数,你可以对一个数的二进制形式的1移位,可执行多次这种操作,问有多少个区间满足对各数执行任意次操作后满足区间异或为0.

思路:

满足异或值为0的区间,必须满足一下条件:

1.区间中二进制1的个数和为偶数个;

2.区间二进制1的个数最大值的两倍不超过区间和.

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int M=1e6+5;
ll a[10],countt[M];
int main(){
    ll ans=0;
    int n;
    scanf("%d",&n);
    int sign=0;
    a[0]=1;
    for(int i=1;i<=n;i++){
        ll x;
        scanf("%I64d",&x);
        int t=0;
        while(x){
            if(x&1)
                t++;
            x>>=1;
        }
        countt[i]=countt[i-1]+t;
        sign^=t&1;
        ans+=a[sign];
        ll maxx=0;
        for(int j=i;i-j<=64&&j>=1;j--){
            maxx=max(maxx,countt[j]-countt[j-1]);
            if((countt[i]-countt[j-1])%2==0&&2*maxx>countt[i]-countt[j-1])
                ans--;
        }
        a[sign]++;
    }
    printf("%I64d",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/starve/p/12240489.html