hdu 4737 二分或暴力

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

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

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;
}
*/
				
时间: 2024-08-09 22:01:57

hdu 4737 二分或暴力的相关文章

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

HDU - 2089 不要62 (暴力或数位DP)

Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列. 你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新

hdu 4775 Infinite Go(暴力)

题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋,总共走了n步,黑棋和白棋交替走,如果一片棋的上下左右被封死,那么该片棋子就会被吃掉,问说最后黑白棋各剩多少个. 解题思路:比较恶心的模拟题,相邻相同色的棋子要用并查集连接,并且要记录每片棋子还剩的空格数,如果空格数为0的话说明该片棋子被其他颜色围住,则要剔除掉,不且将相邻的位置不同色的棋空格数加1.主要是细节上的问题. 样例 8 7 5 5 4 5 3 5 3 4 4 4 3 3 4 6 18 1 3 1 4 2 2 1

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in