前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces - 1245F Daniel and Spring Cleaning (数位DP)

CodeForces - 1245F Daniel and Spring Cleaning (数位DP)

作者头像
风骨散人Chiam
发布2020-10-28 11:29:24
4310
发布2020-10-28 11:29:24
举报
文章被收录于专栏:CSDN旧文

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

代码语言:javascript
复制
3
1 4
323 323
1 1000000

Output

代码语言:javascript
复制
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是否到达了最大值。然后枚举即可。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档