A Bit Fun
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2089 Accepted Submission (s): 1040
Problem DescriptionThere 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.
InputThe 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.
OutputFor every case, you shoshould output "Case # t:" at first, without quotes.
TIs the case number starting from 1.
Then follows the answer.
Sample Input
23 61 3 52 45 4
Sample Output
Case #1: 4 Case #2: 0
You can use an array to record the number of occurrences, and then subtract an element from a range or value.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <queue> # include <vector> # include <string> # include <map> # include <cstdlib> # include <ctime> using namespace std; const int maxn = 100000 + 1000; int a [maxn]; int h [100]; int n, m; void add (int x) {int cur = 0; while (x) {h [cur ++] + = (x % 2); x = x/2 ;}} void del (int x) {int cur = 0; while (x) {h [cur ++]-= (x % 2); x = x/2 ;}} int re () {int ans = 0; for (int I = 0; I <35; I ++) {if (h [I]> 0) ans + = (1 <I) ;}return ans ;} int main () {int t, ca = 1; scanf ("% d", & t); while (t --) {scanf ("% d ", & n, & m); memset (h, 0, sizeof (h); for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); int temp = 0; long ans = 0; int cur = 0; for (int I = 0; I <n; I ++) {cur = max (I, cur); while (temp | a [cur]) <m & cur <n) {temp = (temp | a [cur]); add (a [cur]); cur ++;} if (cur> I) {ans + = (cur-I); del (a [I]); temp = re () ;}} printf ("Case # % d :", ca ++); cout <ans <endl;} return 0 ;}
Hdu 4737 A Bit Fun (2013 Chengdu network competition)