美文网首页动态规划
2019牛客第七场H题 (Pair) 数位DP

2019牛客第七场H题 (Pair) 数位DP

作者: 叔丁基锂_ | 来源:发表于2019-08-09 16:10 被阅读0次

题意:给三个数a,b,c,求pair<x,y> ,其中x\in [1,a], y\in [1,b] ,并且满足下列至少一条条件:

  • x\wedge y>c
  • x\oplus y <c

题解:由于两个数都是位运算,考虑数位dp。又因为两个情况都没有包含等号,所以考虑都不满足这两个条件的pair,即x\wedge y\le c, x\oplus y\ge c 。然后注意我们求的pair是不包含0的,所以直接数位dp的答案需要减去满足上述条件含有0的pair。

然后就是一个常规的数位dp。

#include <algorithm>
#include <cstring>
#include <iostream>
#define int long long
using namespace std;
int diga[32], digb[32], digc[32];
int dp[32][2][2][2][2];

int go(int s, bool upa, bool upb, bool upand, bool upxor)
{
    if (s == -1)
    {
        return 1;
    }
    if (dp[s][upa][upb][upand][upxor] != -1)
    {
        return dp[s][upa][upb][upand][upxor];
    }
    int topa = upa ? diga[s] : 1;
    int topb = upb ? digb[s] : 1;
    int topc_xor = upxor ? digc[s] : 0;
    int topc_and = upand ? digc[s] : 1;
    int res = 0;
    for (int a = 0; a <= topa; a++)
    {
        for (int b = 0; b <= topb; b++)
        {
            if ((a & b) > topc_and)
                continue;
            if ((a ^ b) < topc_xor)
                continue;
            res += go(s - 1, upa && a == topa, upb && b == topb, upand && ((a & b) == topc_and), upxor && ((a ^ b) == topc_xor));
        }
    }
    return dp[s][upa][upb][upand][upxor] = res;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    int _;
    cin >> _;
    while (_--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        for (int i = 0; i < 32; i++)
        {
            diga[i] = (a >> i) & 1;
            digb[i] = (b >> i) & 1;
            digc[i] = (c >> i) & 1;
        }
        memset(dp, -1, sizeof(dp));
        int ans = go(31, 1, 1, 1, 1);
        ans -= max(0ll, a - c + 1) + max(0ll, b - c + 1);
        cout << a * b - ans << endl;
    }
}


相关文章

  • 2019牛客第七场H题 (Pair) 数位DP

    题意:给三个数a,b,c,求pair ,其中 ,并且满足下列至少一条条件: 题解:由于两个数都是位运算...

  • [数位dp] Pair

    输入正整数A,B,C,统计满足1≤x≤A,1≤y≤B且至少满足下列条件之一:①x and y > C②x xor ...

  • 《剑指 Offer (第 2 版)》第 21 题:调整数组使得奇

    第 21 题:调整数组使得奇数位于偶数之前 传送门:AcWing:调整数组顺序使奇数位于偶数前面,牛客网 onli...

  • 数位dp

    数位dp的入门题HDU - 2089 不要62题意:求区间[n,m]之间,不含有4和(连续的)62的数字个数。分析...

  • 数位dp

    数位dp是解决一类选择有约束的数字的个数的问题的解法,就是数一个区间有多少个满足题目条件的数字的个数,通常暴...

  • 数位DP

    1. 计数问题 原题链接[https://www.acwing.com/problem/content/340/]...

  • 19秋招前端知识小结(二)

    2019秋招h5前端知识简要整理,来源于牛客题后评论区等。 C05新增内容 -$51 CSS3新增属性 -$52 ...

  • DP训练——数位DP

    数位DP BZOJ1026题意求到间,不含前导零且相邻两个数字之差至少为的正整数的个数。题解状态定义:表示当前处理...

  • 2019牛客第七场E题 (Find the median) 思维

    题意:给n个操作,每次和 (1e9范围内)即往数组里面插所有 的所有数,求每次操作后的中位数 题解:区间离散化然后...

  • 2019牛客第五场G题 (subsequence 1) DP

    题意:给一个数字字符串s和t,求s中有多少个子序列比t更大 题解:如果子序列比t更长,那么只要开头不是0都可以,暴...

网友评论

    本文标题:2019牛客第七场H题 (Pair) 数位DP

    本文链接:https://www.haomeiwen.com/subject/dqphjctx.html