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. android之间的各项信息传输类型

    首先是activity想fragment怎样动态的传输数据: 一:activity与fragment之间进行数据传递是,在Activity中将要传递的数据封装在一Bundle中,使用setArgume ...

  2. 2017蓝桥杯日期问题(C++B组)

    标题:日期问题小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的, ...

  3. NHibernate COUNT(*) 统计问题

    NHibernate这个框架用了有一年多了,相对有很大的优势,可以省去很多写Sql的时间. 但是如果你想用它做统计,那么有点抱歉,只能手动写写了.它内置的东西很难符合你的需求. 我遇到的问题是这样的. ...

  4. Django模拟ASP.NET MVC 自动匹配路由(转载)

    项目结构 操作步骤 1.创建项目结构如上图 2.在myapp目录下创建urls文件,代码: from django.conf.urls import patterns, url from untitl ...

  5. python3(二十二) oop

    """ 面向对象编程 """ __author__ = 'shaozhiqi' # 面向对象的程序设计把计算机程序视为一组对象的集合,而每个 ...

  6. Powershell基础---帮助系统

    帮助系统能带给我们什么? 1.快速找到命令,无需使用Bing或者Google 2.运行命令时候返回了错误信息,帮助系统可以告诉我们如何正确使用该命令 3.多个命令组合执行完成复杂的工作,帮助系统告诉我 ...

  7. leetcode 30 day challenge Counting Elements

    Counting Elements Given an integer array arr, count element x such that x + 1 is also in arr. If the ...

  8. std::string::append函数

    string& append (const string& str); string& append (const string& str, size_t subpos ...

  9. GeoGebra重复手段实现

    1.自定义工具部分可以在网上搜一些别人做的工具,主要是把自己经常做的一些任务做成工具,减少重复过程 2.列表部分的简单操作如图所示,实现对三个点的多项式拟合 3.通过序列指令格式可以做一个好玩的效果, ...

  10. 转载:URL链接中的不同用处

    ,井号:表示网页中的一个位置,被称之为锚点,常用于某个网页间不同位置的跳转,简单的说就是在一个网页中,URL 不变的情况下,通过添加"#buy"的字符在 URL 最后可以跳转到当前 ...