hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737

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
  
  
2 3 6 1 3 5 2 4 5 4
 

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

<pre name="code" class="cpp">/**
hdu 4737 二分或暴力
题目大意:给定一列数,找出有多少子区间满足区间内所有数的或值小于m
解题思路:我的方法是开一个数num[n][35],num[i][j]表示前i个数的第j个二进制位上有多少个1了,然后从1开始枚举区间起点,二分查找不满足小于m
          的第一个点即可;复杂度O(nlogn)
          队友是直接暴力O(n*n)过的,不知道为什么比我的还快 ==
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100005;
int n,m;
int num[maxn][35],a[maxn],bit[35];
int judge(int n,int x)
{
    int sum=0;
    for(int i=0;i<33;i++)
    {
        if(num[n][i]-num[x-1][i]>0)
           sum+=(1<<i);
    }
    return sum;
}

int erfen(int x)
{
    int l=x,r=n,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(judge(mid,x)>=m)
            r=mid-1;
        else
            l=mid+1;
    }
    return r;
}

int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            int x=a[i],len=0;
            for(int j=0;j<33;j++)///这句很关键,不要在下面用num[i][len]=num[i-1][len]bit[len];因为i的len不一定有i-1的大
                num[i][j]=num[i-1][j];
            while(x)
            {
                bit[len]=x&1;
                num[i][len]+=bit[len];
                len++;
                x>>=1;
            }
        }
        /**
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<30;j++)
            {
                printf("%d",num[i][j]);
            }
            printf("\n");
        }*/
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int y=erfen(i);
            ans+=(y-i+1);
            //printf("%d %d\n",i,y);
        }
        printf("Case #%d: %d\n",++tt,ans);
    }
    return 0;
}
/**
99
8 8
1 2 3 4 5 6 7 8

8 7
1 2 3 4 5 6 7 8

4 4
1 2 3 4

10 23
2 3 5 7 8 1 2 4 15 57
*/


/***
//附上暴力代码
#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 1e5+10;

int a[maxn];

int main()
{
    int t,cas=1,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",a+i);
        int ans = 0;
        for(int i=0;i<n;i++){
            int tmp = a[i];
            if(tmp<m) ans++;
            for(int j=i+1;j<n;j++){
                tmp|=a[j];
                if(tmp<m) ans++;
                else break;
            }
        }
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}
*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值