题目链接:https://codeforces.com/contest/1245/problem/F

题意:给定一个区间(L,R),a、b两个数都是属于区间内的数,求满足 a + b = a ^ b 的实数对个数。

题解:看到求区间内满足一定条件的数的个数,应该用数位dp,数位dp基本操作是编写出solve函数调用记忆化搜索,那么考虑solve(R,R)是求0到R满足条件的答案,solve(L-1,R)求a属于0到L-1,b属于0到R满足条件的答案,solve(L-1,L-1)是ab都属于0到L-1满足条件的个数,那么最终的答案 = solve(R,R) - solve(L-1,R) - solve(R,L-1) + solve(L-1,L-1) ,这是一个二维容斥。

a + b = a ^ b又可以转化为a & b = 0,这两者是相互等价的,那么就可以直接数位dp了,因为a&b=0,所以ab每一位相与都是0,依次来进行数位dp的记忆化搜索。

AC代码:

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 33;
ll dp[maxn][2][2],L[33],R[33];
ll dfs(int pos,int nowa,int nowb){ // nowa,nowb记录是否受上一位所限制
if(pos == 0) return 1;
if( dp[pos][nowa][nowb] != -1) return dp[pos][nowa][nowb];//如果搜索过直接返回
int maxa = nowa?L[pos]:1;//最高位是1
int maxb = nowb?R[pos]:1;//最高位是1
ll res = 0;
for(int i = 0;i<=maxa;i++){
for(int j = 0;j<=maxb;j++){
if((i&j) == 0){//二层循环枚举一个数位的2*2的情况
res += dfs(pos-1,nowa&(i == maxa),nowb&(j == maxb));
}
}
}
dp[pos][nowa][nowb] = res;
return res;
}
ll cal(int l,int r){
if(l<0 || r<0) return 0;
memset(dp,-1,sizeof(dp));
for(int i=1;i<=31;i++)//因为枚举的数是二进制存储,所以要以二进制形式保存
{//数位分离
L[i]=l&1;
R[i]=r&1;
l/=2;
r/=2;
}
return dfs(31,1,1);
}
ll solve(){
int l,r;
scanf("%d%d",&l,&r);
return cal(r,r)-2*cal(l-1,r)+cal(l-1,l-1);//二维容斥
} int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
scanf("%d",&t);
while(t--){
printf("%lld\n",solve());
}
return 0;
}

codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)的更多相关文章

  1. 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 ...

  2. [cf 1245 F] Daniel and Spring Cleaning

    题意: 求区间$[l,r]$内有多少有序数对$(a,b)$满足$a+b=a\bigoplus b$. $l,r\leq 10^9$. 题解: 有用的就一句话: 求区间内一元组可以一维容斥,同理求二元组 ...

  3. 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, ...

  4. CF1245F: Daniel and Spring Cleaning

    CF1245F: Daniel and Spring Cleaning 题意描述: 给定区间\([L,R]\),其中 \((0\leq L,R\leq 10^9)\),问在区间内有多少数对\((x,y ...

  5. spring boot高性能实现二维码扫码登录(中)——Redis版

    前言 本打算用CountDownLatch来实现,但有个问题我没有考虑,就是当用户APP没有扫二维码的时候,线程会阻塞5分钟,这反而造成性能的下降.好吧,现在回归传统方式:前端ajax每隔1秒或2秒发 ...

  6. spring boot高性能实现二维码扫码登录(下)——订阅与发布机制版

     前言 基于之前两篇(<spring boot高性能实现二维码扫码登录(上)——单服务器版>和<spring boot高性能实现二维码扫码登录(中)——Redis版>)的基础, ...

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

    While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it ...

  8. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  9. Codeforces Gym100623J:Just Too Lucky(数位DP)

    http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...

随机推荐

  1. python3包、模块、类、方法的认识

    包>>模块>>类>> 函数 包:就是一个目录,import time from+import导入包中的部分模块 直接到类 from budaoguan.common ...

  2. Python之四:控制流

    1.If 逻辑判断: if a: b elif c: d else: e 先判断a语句块的值是否为真,如果为真,则执行b语句块,如果不为真则转到elif判断c语句块的值是否为真,如果为真执行d语句块, ...

  3. 主机名由localhost变成bogon是怎么回事,怎样变回localhost这个名字?

    如何解决这个问题修改你的 DNS 为公共 DNS,例如 114.114.114.114 或者谷歌的 8.8.8.8.然后修改你的主机名: sudo hostname localhost 出现这个问题的 ...

  4. 归并排序 ALDS1_5_B:Merge Sort

    Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...

  5. java实现判断两个二叉树是否相同

    1.定义树节点类:节点值.左节点.右节点.构造器 2.先判断树是否为空的情况 3.树不为空时,判断节点所指的值是否相等,若相等,则递归判断节点的左右节点是否相同,相同则返回true /** * Def ...

  6. 一个简单的java web项目 仅实现添加

    连接数据库已经进行判断 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分 ...

  7. centos8 常用软件

    防火墙 GUI版 https://blog.csdn.net/qq_36492368/article/details/80432259 dnf install -y firewall-config d ...

  8. PHP不使用第三个变量,如何实现两个变量值互换(变量值自定)

    1.使用函数: $a = 123; $b = 456; list($b,$a) =array($a,$b); 2.数学算式:$a =$a+$b-$b; $a =2; $b =1; $a =$a+$b; ...

  9. linux - mysql:安装mysql

    安装环境 系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...

  10. 登录时 按Enter 进入登录界面 或者下一行

    function keyLogin() { if (event.keyCode == 13) //回车键的键值为13 $(".btn-submit").click(); //调用登 ...