HDU 47,372 points or violence

Source: Internet
Author: User

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

Problem descriptionthere is 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 the different pairs of (I, J) where F (i, J) < M.
Inputthe first line has a number T (T <=), indicating the number of test cases.
For each test case, first line contains, numbers n and M. (1 <= n <= 100000, 1 <= m <=) then n numbers c Ome in the second line which is the array a, where 1 <= ai <= 230.
Outputfor every case, you should output ' case #t: ' At first, without quotes. The TIs 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

<pre name= "code" class= "CPP" &GT;/**HDU 47,372 points or the topic of violence: Given a column number, find out how many sub-range satisfies all the numbers in the interval or the value is less than M solution thinking: My method is to open a number num[n][35],num[ I][J] Indicates how many of the first I number of J bits on the number of 1, and then starting from 1 to enumerate the beginning of the interval, the binary search does not meet the first point less than M can be, the complexity O (NLOGN) teammates are direct violence O (n*n), do not know why faster than mine ==*/# 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++)////This sentence is very important, do not use Num[i][len]=num[i-1][len]bit[len under the following]; Because Len of I does not necessarily have i-1 big 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;} /**998 81 2 3 4 5 6 7 88 71 2 3 4 5 6 7 84 41 2 3 410 232 3 5 7 8 1 2 4 15 57*//***//attached violence code # include <iostream> #inclu    De <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;} */


HDU 47,372 points or violence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.