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. django、celery异步发邮件

    django.celery异步发邮件 django自带的send_mail发邮件功能执行发邮件功能会因为网络的原因造成花费的时间过长,为了解决这个问题,可以用celery + redis代替 安装包: ...

  2. coding++:漫画版-了解什么是分布式事务?

    —————  第二天  ————— ———————————— 假如没有分布式事务: 在一系列微服务系统当中,假如不存在分布式事务,会发生什么呢?让我们以互联网中常用的交易业务为例子: 上图中包含了库存 ...

  3. hadoop(十)hdfs上传删除文件(完全分布式七)|12

    集群测试 上传小文件到集群,随便选择一个小文件上传到hdfs的根目录 [shaozhiqi@hadoop102 hadoop-3.1.2]$ bin/hdfs dfs -put wcinput/wc. ...

  4. centos7 NAT链接配置(静态ip/修改网卡名为eth0)|1

    NAT的静态ip设置并且修改网卡名为eth0 1 cd /etc/sysconfig/network-scripts/ mv eno16777736  ifcfg-eth0 #修改名称 vi eth0 ...

  5. Win安装docker

    Windows Docker 安装 win7.win8 系统 win7.win8 等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirror ...

  6. mysql几个操作数据库命令符下的常用命令

    1.导出整个数据库 mysqldump -u用户名 -p密码 数据库名 > 导出的文件名 C:\Users\jack> mysqldump -uroot -pmysql sva_rec & ...

  7. mysql命令行参数 --- 这些参数不同于 mysqldump 后的 那些参数(下边文章开头有链接) :2种类型的参数 含义是不一样的

    mysql命令行参数  --- 这些参数不同于  mysqldump  后的 那些参数   :2种类型的参数 含义是不一样的 一,mysql命令行参数 Usage: mysql [OPTIONS] [ ...

  8. Serval and Parenthesis Sequence CodeForces - 1153C

    题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法. 合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的. 题解:既然 ...

  9. Flask接口开发过程中的心得2019.10.03

    完善了一下慕课网实战中的post接口开发,得到了一些进步: 代码如下: #coding=utf-8 from flask import Flask from flask import request ...

  10. Os-Hax: 1 靶机记录

    靶机地址:172.16.1.197 Kali地址:172.16.1.108 1 信息搜集 靶机首页 相关信息查看 端口扫描: 开放22和80 目录扫描: 访问http://172.16.1.197/c ...