Bitwise And Queries

Time limit: 1500 ms
Memory limit: 128 MB

You are given QQ queries of the form a\ b\ xa b x. Count the number of values yy such that a \leq y \leq ba≤y≤b and x\ \& \ y = xx & 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^51≤Q≤10​5​​
  • 1 \leq a \leq b \leq 10^{18}1≤a≤b≤10​18​​
  • 0 \leq x \leq 10^{18}0≤x≤10​18​​
Input Output
4
1 10 3
5 10 0
1 63 7
32 100 32
2
6
8
37
 

x&y==x,说明x二进制表示上,有1的地方,y也要有1,是0的地方,y可以是0或者1,记忆化瞎几把搜一下就行了,每次可以选择的有0或1,再判断一下能否取0或1。

lr表示此时这个位置y能否取任意数,只要前面x为0,y为1时,递归选0的话,就说明y后面的数可以任意取了,因为前面有1选了0,那后面无论选什么都不可能超过上界了。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
const int inf = 0x3f3f3f3f;
const int mod = ;
LL dp[][];
int A[],X[];
LL solve(int cur,int lr){
if(cur == )return ;
if(dp[cur][lr] != -)return dp[cur][lr];
if(lr){
if(A[cur]){
return dp[cur][lr] = solve(cur + ,lr);
}
else {
return dp[cur][lr] = * solve(cur + ,lr);
}
}
else {
if(A[cur]){
if(X[cur])return dp[cur][lr] = solve(cur + ,lr);
else return dp[cur][lr] = ;
}
else {
if(X[cur])return dp[cur][lr] = solve(cur+,) + solve(cur+,lr);
else return dp[cur][lr] = solve(cur+,lr);
}
}
}
void print(int *x){
for(int i = ; i < ; i++)
printf("%d",x[i]);
puts("");
}
int main(){
#ifdef local
freopen("in", "r", stdin);
#endif
int T;
scanf("%d",&T);
while(T--){
LL x,y,a;
scanf("%lld%lld%lld",&x,&y,&a);
x--;
for(int i = ; i < ; i++){
A[i] = a & ;
a >>= ;
}
reverse(A,A+);
// print(A);
for(int i = ; i < ; i++){
X[i] = x & ;
x >>= ;
}
reverse(X,X+);
// print(X);
memset(dp,-,sizeof dp);
LL res = solve(,);
for(int i = ; i < ; i++){
X[i] = y & ;
y >>= ;
}
reverse(X,X+);
// print(X);
memset(dp,-,sizeof dp);
res = solve(,) - res;
printf("%lld\n",res);
}
}

Bitwise And Queries的更多相关文章

  1. CF-1451 E Bitwise Queries 异或 交互题

    E - Bitwise Queries 传送门 题意 有一组序列,长度为 \(n(4\le n \le 2^{16})\),且 \(n\) 为 2 的整数次幂,序列中数值范围为 [0,n-1], 每次 ...

  2. 【做题记录】CF1451E2 Bitwise Queries (Hard Version)

    CF1451E2 Bitwise Queries (Hard Version) 题意: 有 \(n\) 个数( \(n\le 2^{16}\) ,且为 \(2\) 的整数次幂,且每一个数都属于区间 \ ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. 实践 HTML5 的 CSS3 Media Queries

    先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...

  5. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  6. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  7. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  8. 使用CSS3 Media Queries实现网页自适应

    原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...

  9. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

随机推荐

  1. AngularJS1.X学习笔记3-内置模板指令

    前面学习了数据绑定指令,现在开始学习内置模板指令.看起来有点多,目测比较好理解.OK!开始! 一.ng-repeat 1.基本用法 <!DOCTYPE html> <html lan ...

  2. sqoop 操作从hdfs 导入到mysql中语句

    将hdfs下/dw/dms/usr_trgt下的文件导入到mysql中test数据库下usr_trgt表中 sqoop-export   --connect jdbc:mysql://mysqlDB: ...

  3. php常用的优化手段

    由于工作码成狗,抽闲整理了下内容,以下是网上流传比较广泛的30种SQL查询语句优化方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. ...

  4. 利刃 MVVMLight 6:命令基础

    在MVVM Light框架中,事件是WPF应用程序中UI与后台代码进行交互的最主要方式,与传统方式不同,mvvm中主要通过绑定到命令来进行事件的处理, 因此要了解mvvm中处理事件的方式,就必须先熟悉 ...

  5. Asp .Net MVC4笔记之目录结构

    认识MVC从目录结构开始,从基本创建开始. App_Data 文件夹:App_Data 文件夹用于存储应用程序数据. App_Start:启动文件的配置信息,包括很重要的RouteConfig路由注册 ...

  6. MySQL最常用分组聚合函数

    一.聚合函数(aggregation function)---也就是组函数 在一个行的集合(一组行)上进行操作,对每个组给一个结果. 常用的组函数: AVG([distinct] expr) 求平均值 ...

  7. Python实现二叉树的四种遍历

    对于一个没学过数据结构这门课程的编程菜鸟来说,自己能理解数据结构中的相关概念,但是自己动手通过Python,C++来实现它们却总感觉有些吃力.递归,指针,类这些知识点感觉自己应用的不够灵活,这是自己以 ...

  8. 【转载】stm32中断学习

    中断对于开发嵌入式系统来讲的地位绝对是毋庸置疑的,在C51单片机时代,一共只有5个中断,其中2个外部中断,2个定时/计数器中断和一个串口中断,但是在STM32中,中断数量大大增加,而且中断的设置也更加 ...

  9. JS的内置对象以及JQuery中的部分内容

     [js中的数组]              1  数组的概念:可以再内存中连续存储的多个有序元素的结构                元素的顺序:称为下标,通过下标查找对应元素.           ...

  10. 修改linux系统时间和同步

    date 查看当前时间 date -s 15:14:13 修改时间 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 修改时区 yes cront ...