|
A Bit FunTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2858 Accepted Submission(s): 1414
Problem Description
There are n numbers in a array, as a
0, a
1 ... , a
n-1, and another number m. We define a function f(i, j) = a
i|a
i+1|a
i+2| ... | a
j . 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.
Input
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 <= 2 30) Then n numbers come in the second line which is the array a, where 1 <= a i <= 2 30.
Output
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.
Sample Input
Sample Output
Source
|
题意:告诉n是数,求一个i到j( i<=j )的序列的或,得到的结果小于m,问这样的序列有多少
思路:每次加入一个数,若大于等于m,那么从当前序列开头开始删除数字,le指向开头数字对应的序号,i为当前序列末尾位置,开头那个数字对后面造成的影响为 i-le 。在加上最后le到n的可行序列。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,T;
int num[33];
int a[100009];
int main()
{
scanf("%d",&T);
int ca=1;
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
memset(num,0,sizeof num);
int tmp=0,le=0,ans=0;
for(int i=0; i<n; i++)
{
tmp|=a[i];
for(int j=0; j<30; j++) //加入当前位置上数的影响
if((1<<j) & a[i])
num[j]++;
while(le<=i && tmp>=m)
{
ans+=i-le;
for(int j=0; j<30; j++)
if((1<<j)&a[le])
{
num[j]--;
if(num[j]==0)
tmp^=(1<<j);
}
le++;
}
}
for(int i=le;i<n;i++) //结尾还有连续可加入的部分
ans+=n-i;
printf("Case #%d: %d\n",ca++,ans);
}
return 0;
}