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

题目链接:https://ac.nowcoder.com/acm/contest/887/H

题意:给定A,B,C,求有多少对(x,y)满足x&y>C或者x^y<C,其中1<=x<=A,1<=y<=B。

思路:首先逆向考虑,求有多少对(x,y)满足x&y<=C且x^y>=C,然后用A*B去减它即可。然后就是数位dp模板题,用dp[pos][la][lb][land][lxor]表示到第pos位的个数,la位表示是否是A的上限,lb表示是否是B的上限,land表示 与 的上限是否是C,lxor表示 异或 的下限是否是C,因为该dp表示的值与输入的A,B,C有关,所以每次都要memset置-1。因为dfs存在x或y为0的情况,而x、y最小是1,所以要减掉x或y为0的情况。x为0的个数有max(0,B-C+0),即y>=C;同理,y为0有max(0,A-C+0)种。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long LL;
LL dp[35][2][2][2][2],A,B,C,ans;
int T,a[35],b[35],c[35];

void init(){
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    LL aa=A,bb=B,cc=C;
    int pos=0;
    while(aa){
        a[pos++]=aa%2;
        aa/=2;
    }
    pos=0;
    while(bb){
        b[pos++]=bb%2;
        bb/=2;
    }
    pos=0;
    while(cc){
        c[pos++]=cc%2;
        cc/=2;
    }
}

LL dfs(int pos,int la,int lb,int land,int lxor){
    if(pos<0) return 1;
    if(dp[pos][la][lb][land][lxor]!=-1) return dp[pos][la][lb][land][lxor];
    LL res=0;
    int up1=1,up2=1,up3=1,up4=0;
    if(la) up1=a[pos];
    if(lb) up2=b[pos];
    if(land) up3=c[pos];
    if(lxor) up4=c[pos];
    for(int i=0;i<=up1;++i)
        for(int j=0;j<=up2;++j){
            if((i&j)>up3) continue;
            if((i^j)<up4) continue;
            res+=dfs(pos-1,la&&(i==a[pos]),lb&&(j==b[pos]),land&&((i&j)==c[pos]),lxor&&((i^j)==c[pos]));
        }
    return dp[pos][la][lb][land][lxor]=res;
}

int main(){
    scanf("%d",&T);
    while(T--){
        memset(dp,-1,sizeof(dp));
        scanf("%lld%lld%lld",&A,&B,&C);
        init();
        ans=dfs(30,1,1,1,1);
        ans=ans-max(A-C+1,0LL)-max(B-C+1,0LL);
        printf("%lld\n",A*B-ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/FrankChen831X/p/11332935.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值