Bitwise and Queries

Source: Internet
Author: User

Bitwise and Queriestime limit: Ms
Memory limit: MB

You are givenQQ queries of the formA\ b\ XABX. Count the number of valuesYY such thatA \leq y \leq ba≤ y≤b and < Span class= "Katex-mathml" >x\ \& \ y = x< Span class= "base textstyle uncramped" >x & y= x, where we denote By \&& the bitwise  and  operation.

Standard input

The first line contains a single integer qq.

Each of the following qq lines contains three integers a\ b\ xa b X, representing A query.

Standard output

Output qq lines, each containing a single integer representing the answer to a query.

Constraints and Notes
  • 1 \leq Q \leq 10^5 1 ≤q≤1 0 ? 5< Span class= "Fontsize-ensurer reset-size5 size5" >?
  • 1 \leq a \leq b \leq 10^{18}1 ≤a≤b ≤10 ?18??
  • 0 \leq x \leq 10^{18} 0 ≤x≤1 0? 1 8??
Input Output
41 10 35 10 01 63 732 100 32
26837

X&y==x, explain the x binary representation, there are 1 places, y also have 1, is 0 of the place, Y can be 0 or 1, memory blind a few searches on the line, each can choose 0 or 1, and then judge whether can take 0 or 1.

LR means that at this point y can take any number, as long as the previous x is 0,y is 1 o'clock, recursive selection of 0, it means that the number after Y can be arbitrarily taken, because there are 1 selected 0, then no matter what the choice is not beyond the upper bound.
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intN =200005;Const intINF =0x3f3f3f3f;Const intMoD =1000000007; LL dp[ $][2];inta[ $],x[ $]; LL Solve (intCurintLR) {    if(cur = = -)return 1; if(DP[CUR][LR]! =-1)returnDP[CUR][LR]; if(LR) {if(A[cur]) {returnDP[CUR][LR] = solve (cur +1, LR); }        Else {            returnDP[CUR][LR] =2* Solve (cur +1, LR); }    }    Else {        if(A[cur]) {if(X[cur])returnDP[CUR][LR] = solve (cur +1, LR); Else returnDP[CUR][LR] =0; }        Else {            if(X[cur])returnDP[CUR][LR] = Solve (cur+1,1) + Solve (cur+1, LR); Else returnDP[CUR][LR] = Solve (cur+1, LR); }    }}voidPrintint*x) {     for(inti =0; I < -; i++) printf ("%d", X[i]); Puts ("");}intMain () {#ifdef local freopen ("inch","R", stdin);#endif    intT; scanf ("%d",&T);  while(t--) {LL x,y,a; scanf ("%lld%lld%lld",&x,&y,&a); X--;  for(inti =0; I < -; i++) {A[i]= A &1; A>>=1; } Reverse (A,a+ -);//print (A);         for(inti =0; I < -; i++) {X[i]= x &1; X>>=1; } Reverse (X,x+ -);//print (X);memset (dp,-1,sizeofDP); LL Res= Solve (0,0);  for(inti =0; I < -; i++) {X[i]= y &1; Y>>=1; } Reverse (X,x+ -);//print (X);memset (dp,-1,sizeofDP); Res= Solve (0,0) -Res; printf ("%lld\n", RES); }}
View Code

Bitwise and Queries

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.