Question: hdu4737a bit fun
The N number is given, and then the number of sub-strings is asked. The result of the OR operation on each sub-string is smaller than M.
Problem-solving ideas: the test data is relatively watery, so it is enough to be violent. Positive Solution: store each number in binary format, and start with head and tail pointing to 1. Each tail is ++, and find the farthest head from each tail. Then sum the child strings that each tail meets the condition. Note that when the sum of head to tail exceeds m, the head will be moved backwards. In this case, the number of head pairs should have a position of 1 --.
Code:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; typedef long ll; const int maxn = 1e5 + 5; int N; int num [maxn], M; int num [maxn] [35], CNT [35]; int T [35]; void Init () {T [0] = 1; for (INT I = 1; I <= 30; I ++) T [I] = T [I-1] * 2;} bool judge () {ll sum = 0; For (INT I = 0; I <= 30; I ++) if (CNT [I]) sum + = T [I]; if (sum <m) return true; return false;} ll solve () {ll ans = 0; for (I NT k = 1; k <= N; k ++) {for (INT I = 30; I> = 0; I --) {If (Num [k]> = T [I]) {num [k] [I] = 1; num [k]-= T [I];} elsenum [k] [I] = 0 ;}} int head, tail; head = tail = 1; for (INT I = 0; I <= 30; I ++) CNT [I] = 0; ll TMP; while (tail <= N) {for (INT I = 0; I <= 30; I ++) if (Num [tail] [I]) CNT [I] ++; If (! Judge () {While (Head <= tail &&! Judge () {// note the boundary of head movement for (INT I = 0; I <= 30; I ++) if (Num [head] [I]) CNT [I] --; head ++;} ans + = tail-head + 1; tail ++;} return ans;} int main () {int T; scanf ("% d", & T); Init (); For (int cas = 1; CAS <= T; CAS ++) {printf ("case # % d: ", CAS); scanf (" % d ", & N, & M); For (INT I = 1; I <= N; I ++) scanf ("% d", & num [I]); printf ("% i64d \ n", solve ();} return 0 ;}
Hdu4737a bit fun