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

题目链接
在这里插入图片描述
在这里插入图片描述
题意:
给你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;
}


发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/99624841
今日推荐