hdu 4737 A Bit Fun 数列(2013 ACM/ICPC Asia Regional Chengdu Online 1010)

来源:互联网 发布:华为电视盒安装软件 编辑:程序博客网 时间:2024/05/17 04:21
#include <cstdio>#include <string>#include <cmath>#include <iostream>#include <algorithm>using namespace std;#define LL __int64const LL maxn=1e5+10;LL a[maxn],c[33],x;void add(LL n){    LL i=0;    while(n)    {        if(n&1)c[i]++;        n=n>>1;        i++;    }}void sub(LL n){    LL i=0;    while(n)    {        if(n&1)        {            c[i]--;            if(c[i]==0){x=(x^(1<<i));}        }        n=n>>1;        i++;    }}int main(){    LL T,tt=0;    scanf("%I64d",&T);    while(T--)    {        LL n,m,i,j,k,ans=0,num,st,en;        scanf("%I64d%I64d",&n,&m);        for(i=0;i<n;i++)            scanf("%I64d",&a[i]);        memset(c,0,sizeof(c));        st=x=en=0;//st表示开始位置,和结尾位置。        for(i=0;i<n;i++)        {            x=(x|a[i]);            en++;            add(a[i]);//记录各个位数上的1的个数            if(x<m)            {                ans+=(en-st);//加上以a[en-1]结尾的子串            }            else            {                while(x>=m)                {                    //cout<<x<<endl;                    sub(a[st]);//反抑或a[st],相当于删去子串[st,en-1]中的st,使得子串抑或值小于m                    st++;                }                ans+=(en-st);            }            //cout<<ans<<endl;        }        printf("Case #%I64d: %I64d\n",++tt,ans);    }        return 0;} 


原创粉丝点击