HDU4737 A Bit Fun 位运算

来源:互联网 发布:手机变魔术软件蟑螂 编辑:程序博客网 时间:2024/05/17 01:19

有什么途径可以申请加入ACM校队? 

A Bit Fun

Time 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 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.
 

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 <= 230) Then n numbers come in the second line which is the array a, where 1 <= ai <= 230.
 

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
23 61 3 52 45 4
 

Sample Output
Case #1: 4Case #2: 0
 

Source
2013 ACM/ICPC Asia Regional Chengdu Online


题意:告诉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;}




0 0
原创粉丝点击