A Bit Fun

A Bit Fun

Time Limit : 5000/2500ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 13
Problem Description
There are n numbers in a array, as a[sub]0[/sub], a[sub]1[/sub] ... , a[sub]n-1[/sub], and another number m. We define a function f(i, j) = a[sub]i[/sub]|a[sub]i+1[/sub]|a[sub]i+2[/sub]| ... | a[sub]j[/sub] . 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[sup]30[/sup]) Then n numbers come in the second line which is the array a, where 1 <= a[sub]i[/sub] <= 2[sup]30[/sup].
 
Output
For every case, you should output "Case #t: " at first, without quotes. The [I]t[/I] 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
 
Source
2013 ACM/ICPC Asia Regional Chengdu Online
 
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main()
 6 {
 7     int T,A,B,sign;
 8     long a[100000],m,n,sum,tmp,t,i,j;
 9     scanf("%d",&T);
10     t=1;
11     while(T--)
12     {
13         sign=0;
14         scanf("%ld%ld",&n,&m);
15         for(i=0;i<n;i++)
16             scanf("%ld",&a[i]);
17         for(i=0;i<n;i++)
18         {
19            tmp=a[i];
20            for(j=i;j<n;j++)
21             {
22                 tmp=tmp|a[j];
23                 if(tmp<m)
24                     sign++;
25                 else
26                     break;
27             }
28         }
29         printf("Case #%d: %d
",t,sign);
30         t++;
31     }
32     return 0;
33 }
View Code
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
原文地址:https://www.cnblogs.com/Wurq/p/3750248.html