While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute 1+31+3 using the calculator, he gets 22 instead of 44. But when he tries computing 1+41+4, he gets the correct answer, 55. PuFzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!

So, when he tries to compute the sum a+ba+b using the calculator, he instead gets the xorsum a⊕ba⊕b (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).

As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers ll and rr, how many pairs of integers (a,b)(a,b) satisfy the following conditions:

a+b=a⊕ba+b=a⊕b

l≤a≤rl≤a≤r

l≤b≤rl≤b≤r

However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of testcases.

Then, tt lines follow, each containing two space-separated integers ll and rr (0≤l≤r≤1090≤l≤r≤109).

Output

Print tt integers, the ii-th integer should be the answer to the ii-th testcase.

Example

Input

3
1 4
323 323
1 1000000

Output

8
0
3439863766

Note

a⊕ba⊕b denotes the bitwise XOR of aa and bb.

For the first testcase, the pairs are: (1,2)(1,2), (1,4)(1,4), (2,1)(2,1), (2,4)(2,4), (3,4)(3,4), (4,1)(4,1), (4,2)(4,2), and (4,3)(4,3).

给你l,r;问你[l,r]中有多少对数满足a+b = a^b

a+b=a^b其实就是求二进制中每一位都不同的对数,考虑容斥定理,假设我们知道solve(l,r)就是求[1,l],[1,r]中有多少对答案。

那么最终答案就是solve(r,r)-2solve(l-1,r)+solve(l-1,l-1),然后数位dp,dp[i][sa][sb]表示考虑第i位,a是否到达的最大值,b是否到达了最大值。然后枚举即可。

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int L,R;ll f[33][2][2];
ll dp(int p,bool Lim_x,bool Lim_y){
if(p==-1)return 1;
ll&g=f[p][Lim_x][Lim_y];
if(g!=-1)return g;
g=0;
int Up_x=Lim_x?(L>>p)&1:1,
Up_y=Lim_y?(R>>p)&1:1;
for(int i=0;i<=Up_x;++i)
for(int j=0;j<=Up_y;++j)
if(!(i&j))
g+=dp(p-1,Lim_x&&i==Up_x,Lim_y&&j==Up_y);
return g;
}
inline ll Sol(int l,int r){
if(l<0)return 0;
memset(f,-1,sizeof f);
L=l,R=r;
return dp(log2(R+1)+1,1,1);
}
int main(){
int t,l,r;
scanf("%d",&t);
while(t--)
scanf("%d%d",&l,&r),
printf("%lld\n",Sol(r,r)-2*Sol(l-1,r)+Sol(l-1,l-1));
return 0;
}
 

CodeForces - 1245F Daniel and Spring Cleaning (数位DP)的更多相关文章

  1. Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp

    F. Daniel and Spring Cleaning While doing some spring cleaning, Daniel found an old calculator that ...

  2. CF1245F: Daniel and Spring Cleaning

    CF1245F: Daniel and Spring Cleaning 题意描述: 给定区间\([L,R]\),其中 \((0\leq L,R\leq 10^9)\),问在区间内有多少数对\((x,y ...

  3. codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)

    题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...

  4. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  5. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  6. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  7. [cf 1245 F] Daniel and Spring Cleaning

    题意: 求区间$[l,r]$内有多少有序数对$(a,b)$满足$a+b=a\bigoplus b$. $l,r\leq 10^9$. 题解: 有用的就一句话: 求区间内一元组可以一维容斥,同理求二元组 ...

  8. Codefroces 1245 F. Daniel and Spring Cleaning

    传送门 考虑简单的容斥 设 $F(n,m)$ 表示 $a \in [1,n] , b \in [1,m]$ 的满足 $a+b=a \text{ xor } b$ 的数对的数量 那么答案即为 $F(r, ...

  9. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

随机推荐

  1. 家庭版记账本app开发完成

    经过这几天关于android的相关学习,对于家庭版记账本app以及开发结束. 实现的功能为:用户的注册.登录.添加支出账单.添加收入账单.显示所有的该用户的账单情况(收入和支出).生产图表(直观的显示 ...

  2. python通俗讲解闭包

    通俗理解闭包 先来看看什么是闭包吧 闭包是引用了自由变量的函数.这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外.所以,有另一种说法认为闭包是由函数和与其相关的引用环境组合 ...

  3. javascript 入门(1)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta lang ...

  4. CVPR2018关键字分析生成词云图与查找

    今日目标:爬取CVPR2018论文,进行分析总结出提到最多的关键字,生成wordCloud词云图展示,并且设置点击后出现对应的论文以及链接 对任务进行分解: ①爬取CVPR2018的标题,简介,关键字 ...

  5. mysql导出

    --all-databases , -A 导出全部数据库. mysqldump -uroot -p --all-databases --all-tablespaces , -Y 导出全部表空间. my ...

  6. Java 方法之形参和实参 、堆、栈、基本数据类型、引用数据类型

    * 形式参数:用于接收实际参数的变量(形式参数一般就在方法的声明上) * 实际参数:实际参与运算的变量 * 方法的参数如果是基本数据类型:形式参数的改变不影响实际参数. * * 基本数据类型:byte ...

  7. canvas 实现光线沿不规则路径运动

    canvas 实现光线沿不规则路径运动 此文章为原创,请勿转载 1.svg实现 2.canvas实现 3.坑点 svg让动画沿着不规则路径运动 查阅svg文档后发现,svg动画运动有两种实现方式,且都 ...

  8. 一个不错的java学习博客

    http://iteye.blog.163.com/blog/static/18630809620131484835129/

  9. 疲劳驾驶打瞌睡?python保障您的驾驶安全

    道路千万条,安全第一条!疲劳驾驶可谓交通事故几大罪魁祸首之一,根据美国一项研究显示,司机睡眠不足4小时,交通事故肇事几率等同于醉驾. 为了减少疲劳驾驶现象,驾驶员疲劳检测应运而生.这是一项安全技术,可 ...

  10. sqli-labs通关教程----21~30关

    第二十一关 第二十一关我们正常登陆后看到,uname后面变成了一堆字母 这是经过base64编码之后的样子,所以就照葫芦画瓢,将我payload的uname后面的部分转码成base64,这里可以用正常 ...