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 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的更多相关文章
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
- 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 ...
- 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 ...
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...
- 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 ...
- 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 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
随机推荐
- Mac Electron App 签名后打开闪退
背景 昨天在测试 Mac Electron App 打包,发现不签名的应用能够正常打开,签了名的打开反而会崩溃. 寻因 首先我怀疑是不是自己代码导致闪退,但是在一番查找后,发现还根本没到执行我的代码就 ...
- python部署mariadb主从架构
主机部署: import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.repo ...
- 如何在linux上有2个python的情况下安装gensim
安装python的问题 https://blog.51cto.com/liqingbiao/2083869 安装gensim https://blog.csdn.net/zhujiyao/articl ...
- BIM工程信息管理系统-EF实体框架数据操作基类
EF实体框架数据操作基类主要是规范增.改.查.分页.Lambda表达式条件处理,以及异步操作等特性,这样能够尽可能的符合基类这个特殊类的定义,实现功能接口的最大化重用和统一. 1.程序代码 /// & ...
- JQuery 实现多个checkbox 只选中一个
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- python 基础学习笔记(5)--文件操作
**python 的文件操作** - [ ] 使用python来读写文件是非常简单的操作,我们使用open()来打开一个文件,获取到文件的语柄,然后通过文件语柄就可以进行各种各样的操作了. - [ ] ...
- 导入Jar报错An internal error occurred during: "Building workspace". zip END header not found
百度了好久都没有找到答案,后来新建了一个文件夹,再build path就正常了,不知道为什么
- subprocess之check_out用法
在python3中使用subprocess的check_out方法时,因为该输出为byte类型,所以如果要查看具体的内容时需要进行转码,如果转码不对话,会影响内容输出的可读性,如下: #1,输出解码不 ...
- C++编译器优化技术:RVO、NRVO和复制省略
现代编译器缺省会使用RVO(return value optimization,返回值优化).NRVO(named return value optimization.命名返回值优化)和复制省略(Co ...
- App 自动化框架设计思路
最近在整理和学习Appium+Java 自动化框架,对APP自动化框架的部分设想参考了一些文章,先进行整理下: 框架的思路一: 思考引入:https://www.cnblogs.com/yunfeio ...