F. Daniel and Spring Cleaning

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+3 using the calculator, he gets 2 instead of 4. But when he tries computing 1+4, he gets the correct answer, 5. Puzzled 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+b using the calculator, he instead gets the xorsum a⊕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 l and r, how many pairs of integers (a,b) satisfy the following conditions:

a+b=a⊕b

l≤a≤r

l≤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 t (1≤t≤100) — the number of testcases.

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

Output

Print t integers, the i-th integer should be the answer to the i-th testcase.

Example

input

3

1 4

323 323

1 1000000

output

8

0

3439863766

Note

a⊕b denotes the bitwise XOR of a and b.

For the first testcase, the pairs are: (1,2), (1,4), (2,1), (2,4), (3,4), (4,1), (4,2), and (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; long long dp[35][2][2];
long long ans(int l,int r,int x,int sa,int sb){ if(x==-1)return 1;
if(dp[x][sa][sb]!=-1)return dp[x][sa][sb];
int ma=1,mb=1;
if(sa)ma=(l>>x)&1;
if(sb)mb=(r>>x)&1;
dp[x][sa][sb]=0;
for(int i=0;i<=ma;i++){
for(int j=0;j<=mb;j++){
if((i&j)==0){
dp[x][sa][sb]+=ans(l,r,x-1,sa&(i==ma),sb&(j==mb));
}
}
}
return dp[x][sa][sb];
}
long long ans(int l,int r){
if(l<0||r<0)return 0;
memset(dp,-1,sizeof(dp));
return ans(l,r,30,1,1);
}
void solve(){
int l,r;
scanf("%d%d",&l,&r);
cout<<ans(r,r)-2*ans(l-1,r)+ans(l-1,l-1)<<endl;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
solve();
}
}

Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp的更多相关文章

  1. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  2. Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)

    题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...

  3. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  4. Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)

    yyb大佬的博客 这线段期望好神啊... 还有O(nlogn)FFTO(nlogn)FFTO(nlogn)FFT的做法 Freopen大佬的博客 本蒟蒻只会O(n2)O(n^2)O(n2) CODE ...

  5. Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)

    数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...

  6. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  7. Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

    题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  9. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

随机推荐

  1. react学习之js-xlsx导入和导出excel表格

    前记:最近真的挺忙的,一件事接着一件,都忘了我的React项目,尽管这是一个没写概率没写离散的夜晚,我决定还是先做做我的React 好了,进入正题 项目需求,需要导入和导出表单,发现前端已经强大到无所 ...

  2. unittest---unittest跳过用例

    我们在做自动化测试的时候,可能会遇到一些用例中间不用回归,想要进行跳过.直接注释的话,代码量修改过大,显然这个方法不妥,哪还有什么方法?unittest这个自动化框架可以帮助我们完成这个操作 自动跳过 ...

  3. Python学习笔记六(免费获取代理IP)

    为获取网上免费代理IP,闲的无聊,整合了一下,免费从三个代理网站获取免费代理IP,目的是在某一代理网站被限制时,仍可从可以访问的其他网站上获取代理IP.亲测可用哦!^_^  仅供大家参考,以下脚本可添 ...

  4. ceph安装笔记

    配置源 ceph版本为luminous [root@ceph-node1 ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-r ...

  5. Ubuntu18.04 卸载mysql5.7

    查看MySQL的依赖项:dpkg --list|grep mysql 要删除上面的这些. 开始卸载: sudo apt-get autoremove --purge mysql-server sudo ...

  6. JS数组去除空值

    /** * 扩展Array方法, 去除数组中空白数据 */ Array.prototype.notempty = function() { var arr = []; this.map(functio ...

  7. 微信小程序的坑(持续更新中)

    参与微信小程序开发有一段时间了,先后完成信息查询类和交易类的两个不同性质的小程序产品的开发:期间遇到各种各样的小程序开发的坑,有的是小程序基础功能不断改进完善而需要业务持续的适配,有的是小程序使用上的 ...

  8. 激活函数-Activation Function

    该博客的内容是莫烦大神的授课内容.在此只做学习记录作用. 原文连接:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow ...

  9. Go-数据类型以及变量,常量

    一.数据类型 1.字符串类型 string 2.数字类型 有符号整型: int: int 在32位机器上是int32 在64位机器是int64 int8: int8 表示数字范围是 正负2的7次方减1 ...

  10. Java生鲜电商平台-商城系统库存问题分析以及产品设计对逻辑/物理删除思考

    Java生鲜电商平台-商城系统库存问题分析以及产品设计对逻辑/物理删除思考 说明:在生鲜电商的库存设计,是后台的重点,也是难点,关乎商品是否存在超卖.商品的库存增加方式倒不难,直接在后台添加即可,而扣 ...