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 ...
随机推荐
- Jerome: Vulnhub Walkthrough
nmap 扫描探测: ╰─ nmap -p1-65535 -sV -A -O -sT 10.10.202.135Starting Nmap 7.70 ( https://nmap.org ) at 2 ...
- LeetCode刷题191130 --基础知识篇 二叉搜索树
休息了两天,状态恢复了一下,补充点基础知识. 二叉搜索树 搜索树数据结构支持许多动态集合操作,包括Search,minimum,maximum,predecessor(前驱),successor(后继 ...
- windows 下使用批处理执行 postgresql 命令行操作
1.准备好命令文件 loraserver.sql create role loraserver_as with login password 'dbpassword'; create role lor ...
- oracle数据库system表空间增长过大的问题
网上些解决方法,就是关闭审计,之前也有同事推荐这样,下面就是关闭审计的步骤. VALUE=DB即审计开启,改成FALSE即可. SQL> show parameter audit_trail; ...
- 用PYTHON首选的GUI库WXPYTHON做程序界面
大家好,我是A8U神经网络,今天又要跟大家分享一下wxWidgets开发神经网络程序界面的一些经验,希望对开发有兴趣的朋友有所帮助.跨平台的GUI工具库以GTK +,Qt和wxWidgets闻名. G ...
- 表单生成器(Form Builder)之伪造表单数据番外篇——指定范围随机时间
为了伪造一些尽量真实的假数据,也真是够费劲的.上一篇笔记记录了一下获取一个随机车辆牌照,这篇笔记记录一下怎么获取一个随机时间.这篇就不说那么多废话了,直接上代码 // 获取指定范围的随机数 var g ...
- linux系统的特殊符号
符号 作用 示例 # 注释符号,井号后的内容不会执行 echo #WORD ~ 当前用户的家目录 cd ~ ; 命令顺序执行,前面执行成功与否对后面没影响 COMMAND1 ; COMMAND2 &a ...
- openstack-neutron安装与配置
一.实验目的: 1.理解neutron服务在OpenStack中的作用 2.掌握在控制节点上安装配置neutron的方法和步骤 3.掌握在计算节点上安装与配置neutron的方法和步骤 二.实验步骤: ...
- web攻击与防御技术--SQL注入
sql注入是WEB安全中比较重要而复杂的一个东西.而且危害性比较大. 一般来讲SQL注入的话首先要识别注入点.一个是看能不能注入另外一个是看是什么类型的注入比如说字符,数字,搜索(?)等等 GET.P ...
- 【洛谷5794】[THUSC2015] 解密运算(模拟)
点此看题面 大致题意: 对于一个字符串,我们在其末尾添加一个'.',将字符串视作一个环,则可以从\(n+1\)个位置断开得到\(n+1\)个新串.现将这\(n+1\)个新串按字典序排序('.'的字典序 ...