您的位置:首页 > 其它

hdu 4737 A Bit Fun 尺取法

2016-08-07 22:05 295 查看

A Bit Fun

Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)


[align=left]Problem Description[/align]
There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)
The problem is really simple: please count the number of different pairs of (i, j) where f(i, j) < m.

[align=left]Input[/align]
The first line has a number T (T <= 50) , indicating the number of test cases.
For each test case, first line contains two numbers n and m.(1 <= n <= 100000, 1 <= m <= 230) Then n numbers come in the second line which is the array a, where 1 <= ai <= 230.

[align=left]Output[/align]
For every case, you should output "Case #t: " at first, without quotes. The t is the case number starting from 1.
Then follows the answer.

[align=left]Sample Input[/align]

2
3 6
1 3 5
2 4
5 4

[align=left]Sample Output[/align]

Case #1: 4
Case #2: 0

[align=left]Source[/align]

2013 ACM/ICPC Asia Regional Chengdu Online

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1000000007;
int a
;
int flag[40];
void init()
{
memset(flag,0,sizeof(flag));
}
void update(int x,int hh)
{
int sum=0;
while(x)
{
flag[sum++]+=x%2*hh;
x>>=1;
}
}
int getnum()
{
int ans=0;
for(int i=0;i<=35;i++)
{
if(flag[i])
ans+=1<<i;
}
return ans;
}
int main()
{
int x,y,z,i,t;
int T,cas=1;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&x,&y);
for(i=1;i<=x;i++)
scanf("%d",&a[i]);
int st=1;
int en=1;
int cnt=1;
ll ans=0;
int qu=0;
while(1)
{
while(getnum()<y&&en<=x)
update(a[en++],1);
if(getnum()<y)
{
ans+=(ll)(en-st)*(en-st+1)/2;
break;
}
ans+=max(0,en-st-1);
update(a[st++],-1);
}
printf("Case #%d: %I64d\n",cas++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: