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
",Sol(r,r)-2*Sol(l-1,r)+Sol(l-1,l-1));
return 0;
}