牛客多校第六场-H-Pair

链接:https://ac.nowcoder.com/acm/contest/887/H
来源:牛客网

题目描述

Given three integers A, B, C. Count the number of pairs < x ,y> (with 1≤x≤Aand1yB)
such that at least one of the following is true:
- (x and y) > C
- (x xor y) < C
 
("and", "xor" are bit operators)

输入描述:

The first line of the input gives the number of test cases,T(T100). T test cases follow.

For each test case, the only line contains three integers A, B and C.
1≤A,B,C≤10^9

输出描述:

For each test case, the only line contains an integer that is the number of pairs satisfying the condition given in the problem statement.
示例1

输入

3
3 4 2
4 5 2
7 8 5

输出

5
7
31

数位dp求 x&y<=c && x^y>=c的个数然后用所有方案剪掉
具体见代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int T,A,B,C;
int a[35],b[35],c[35];
ll f[35][2][2][2][2];
void cal(int x,int v[])
{
    for (int i=0;i<=30;i++) v[i]=(x>>i)&1;
}
ll dfs(int pos,bool lima,bool limb,bool limand,bool limxor)
//          位       x<a     y<b         x&y<c       x^y>c
{
    if (pos==-1) return 1;
    if (f[pos][lima][limb][limand][limxor]!=-1) return f[pos][lima][limb][limand][limxor];

    int aa=lima?a[pos]:1;   //lima=1说明之前一直相等当前位取要<=a,否则说明之前<a了当前位可以乱取
    int bb=limb?b[pos]:1;
    int c1=limand?c[pos]:1; //同理如果之前x&y一直等于c那么当前位要x&y<=c,否则就可以乱取
    int c2=limxor?c[pos]:0;
    ll &ret=f[pos][lima][limb][limand][limxor];
    ret=0;

    for (int i=0;i<=aa;i++)
    for (int j=0;j<=bb;j++)
    {
        if ((i&j)>c1) continue;
        if ((i^j)<c2) continue;
        ret+=dfs(pos-1,lima&&i==aa,limb&&j==bb,limand&&(i&j)==c1,limxor&&(i^j)==c2);
    }
    return ret;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&A,&B,&C);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(f,-1,sizeof(f));
        cal(A,a); cal(B,b); cal(C,c);
        ll ans=dfs(30,1,1,1,1);
        ans-=max(0,A-C+1);
        ans-=max(0,B-C+1);  //减掉x,y为0的情况
        printf("%lld\n",(ll)A*B-ans);

    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/tetew/p/11324428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值