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是否到达了最大值。然后枚举即可。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long dp[35][2][2];
  4. long long ans(int l,int r,int x,int sa,int sb){
  5. if(x==-1)return 1;
  6. if(dp[x][sa][sb]!=-1)return dp[x][sa][sb];
  7. int ma=1,mb=1;
  8. if(sa)ma=(l>>x)&1;
  9. if(sb)mb=(r>>x)&1;
  10. dp[x][sa][sb]=0;
  11. for(int i=0;i<=ma;i++){
  12. for(int j=0;j<=mb;j++){
  13. if((i&j)==0){
  14. dp[x][sa][sb]+=ans(l,r,x-1,sa&(i==ma),sb&(j==mb));
  15. }
  16. }
  17. }
  18. return dp[x][sa][sb];
  19. }
  20. long long ans(int l,int r){
  21. if(l<0||r<0)return 0;
  22. memset(dp,-1,sizeof(dp));
  23. return ans(l,r,30,1,1);
  24. }
  25. void solve(){
  26. int l,r;
  27. scanf("%d%d",&l,&r);
  28. cout<<ans(r,r)-2*ans(l-1,r)+ans(l-1,l-1)<<endl;
  29. }
  30. int main(){
  31. int t;
  32. scanf("%d",&t);
  33. while(t--){
  34. solve();
  35. }
  36. }

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. Angular + Leaflet 实现房源数据可视化(附github源码)

    这是什么?租房信息展示平台 宏观的租房数据可视化微观的房源信息展示多条件搜索等等 链接地图搜租房​ 来龙去脉 受 @李国宝 的地图搜租房启发,利用其提供的开放API,配合自己在前端和地理信息系统方面的 ...

  2. python中使用adb命令的方法

    在python中使用adb命令,可以导入os模块. 1 简单的adb命令. 如:os.system('adb version') 2 稍微复杂的adb命令. 如:os.system('adb shel ...

  3. Windows添加自定义开机用户登录启动程序

    默认的启动程序 Ctrl+shift -> Esc调用任务管理器-->启动项选项即可完成计算机开机自启动选项,不过这里只有系统默认添加的. 添加自定义开机启动程序 Windows+R调用运 ...

  4. 教你如何在5分钟轻松部署squid正向代理

    正向代理是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返 ...

  5. MySQL数据库:运算符

    运算符 比较运算符: > 大于 < 小于 >= 大于等于 <= 小于等于 = 等于 != 不等于 范围运算符: between...and...(包含边界值) 类似于 > ...

  6. OAuthon2.0机制详解

    最近在忙企业微信和钉钉的第三方应用开发,需要获取一些信息,第一个就是这个OAuthon2.0,先详细了解下概念和流程 一.应用场景 我们要想用第三方播放器播放你的云盘账号里面的一些秘密视频资源,为了要 ...

  7. 【使用篇二】Quartz自动化配置集成(17)

    出处:https://www.jianshu.com/p/49133c107143 定时任务在企业项目比较常用到,几乎所有的项目都会牵扯该功能模块,定时任务一般会处理指定时间点执行某一些业务逻辑.间隔 ...

  8. js对文中某一处关键字自动检索和全文检索

    部分检索: 代码: <%@ page language="java" contentType="text/html; charset=utf-8" pag ...

  9. python文件的使用

    文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容.概念上,文件是数据的集合抽象,类似地,函数是程序的集合和抽象.用文件形式组织和表达数据更有效也更为灵活.文件包括两种类型:文本文件和二进制 ...

  10. 多次调用settimeout 如何使用单例模式

    <script> function aaa() { window.counter = window.counter||1; console.log(window.counter); win ...