2019牛客暑期多校训练营(第七场)H Pair 数位dp

本文介绍了一种使用数位动态规划(DP)的方法来解决涉及二进制AND和XOR运算的组合计数问题。通过计算不满足特定条件的组合数量,进而求得满足条件的组合总数。文章提供了详细的算法实现代码,包括如何定义状态转移方程以及边界条件。

题目链接
在这里插入图片描述
在这里插入图片描述
题意:
给你A,B,C,三个数其中1<=x<=A,1<=y<=B,要你判断有多少对x,y是满足:

  • (x and y) >C
  • (x xor y) <C
    满足上述的任意一个即可;

分析:
数位dp,这里直接球满足题意的有点麻烦,不妨我们球不满足题意的,这样就用所有的组合情况减去不满足题意的就好了;
定义dp[pos][i][j][k][l]为二进制中第pos位,是否为上界A,是否为上界为B,是否是and的上界,是否是xor的下界

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
ll dp[35][2][2][2][2];//位数,是否是A的上界,是否是B的上界,是否是满足and的上界(也就是等于),是否是满足xor下界(也是等于)
ll A,B,C;
ll dfs(int pos,int A_bound,int B_bound,int and_bound,int xor_bound){
    if(pos<0) return 1;
    if(dp[pos][A_bound][B_bound][and_bound][xor_bound]!=-1) return dp[pos][A_bound][B_bound][and_bound][xor_bound];

    int A_b=1,B_b=1,and_b=1,xor_b=0;

    if(A_bound) A_b=(A>>pos&1);
    if(B_bound) B_b=(B>>pos&1);
    if(and_bound) and_b=(C>>pos&1);
    if(xor_bound) xor_b=(C>>pos&1);

    ll ans=0;
    for(int i=0;i<=A_b;i++){
        for(int j=0;j<=B_b;j++){

            if((j&i)> and_b) continue;
            if((j^i)< xor_b) continue;

            ans+=dfs(pos-1 , A_bound&&(i==A_b) , B_bound&&(j==B_b) , and_bound&&((j&i)==and_b) , xor_bound&&((j^i)==xor_b));
        }
    }
    return dp[pos][A_bound][B_bound][and_bound][xor_bound]=ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&A,&B,&C);
        memset(dp,-1,sizeof dp);
        ll ans=dfs(31,1,1,1,1);
        ans-=max(0ll,A-C+1);//去掉x=0与 y>=C的异或结果数目
        ans-=max(0ll,B-C+1);//去掉x=0与 y>=C的异或结果数目
        printf("%lld\n",A*B-ans);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值