Hdu4737a bit fun

Source: Internet
Author: User

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

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.