CodeForces - 1245F Daniel and Spring Cleaning (数位DP)
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)的更多相关文章
- 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 ...
- CF1245F: Daniel and Spring Cleaning
CF1245F: Daniel and Spring Cleaning 题意描述: 给定区间\([L,R]\),其中 \((0\leq L,R\leq 10^9)\),问在区间内有多少数对\((x,y ...
- codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)
题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- 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 ...
- [cf 1245 F] Daniel and Spring Cleaning
题意: 求区间$[l,r]$内有多少有序数对$(a,b)$满足$a+b=a\bigoplus b$. $l,r\leq 10^9$. 题解: 有用的就一句话: 求区间内一元组可以一维容斥,同理求二元组 ...
- 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, ...
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
随机推荐
- Flask入门 之 没有装饰器的路由
有些时候,需要一个类似路由的功能,但又不能或者不想写装饰器,这该怎么办? so easy! eg: @app.route('login') def login(): return 'hello wor ...
- JuiceSSH:安卓平台免费好用的 SSH 客户端
为了解决上下班路上或者没带电脑时,查看 Linux 服务器日志或者紧急运维的需求,最终找到了 JuiceSSH 这款软件,强烈推荐给大家. 简介 JuiceSSH 是一个为 Android 打造的全功 ...
- Oracle NULL值
NULL值,用来描述记录中没有定义内容的字段值.在Oracle中,判断某个条件的值时,返回值可能是TRUE.FALSE或UNKNOWN. 如果查询一个列的值是否等于20,而该列的值为NULL,那么就是 ...
- day7作业
# day7作业 # 1. 使用while循环输出1 2 3 4 5 6 8 9 10 count = 1 while count < 11: if count == 7: count += 1 ...
- Python语法详解
python语法解析 目录 python语法解析 一.顺序结构 二.分支结构 2.1 if 的基本语法 2.2 if 的基本应用 三.循环结构 3.1 while 语法 3.1.1 语法结束条件 3. ...
- stand up meeting 12/18/2015 ~12/20/2015(weekend)
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 完成主页面设计和非功能性PDF reader UI设计实现 ...
- Problem F Free Weights
二分答案. 思路:对于二分给定的mid,即当前允许移动的最大重量,我们可以把小于改重量的标记一下,然后把没有标记的按照顺序放到另一个数组,然后判断是否满足两两相同. #include<bits/ ...
- Maven 命令深度理解
1.前言 Maven 命令看起来简单,一学即会 .其实,Maven 命令底层是插件的执行过程.了解插件和插件目标才有助于深刻的理解 Maven命令. 2.插件与命令的关系 Maven本质上是一个插件框 ...
- JavaScript函数作用域和声明提前(3.10.1 page.57)
<h4>3.函数作用域和声明提前</h4> <p> <!--<script type="text/javascript">-- ...
- 转:Cookies 和 Session的区别
转自:http://blog.csdn.net/axin66ok/article/details/6175522 1.cookie 是一种发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在 ...