2019??偷谄邎?chǎng)H題 (Pair) 數(shù)位DP

題意:給三個(gè)數(shù)a,b,c,求pair<x,y> ,其中x\in [1,a], y\in [1,b] ,并且滿足下列至少一條條件:

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

題解:由于兩個(gè)數(shù)都是位運(yùn)算,考慮數(shù)位dp。又因?yàn)閮蓚€(gè)情況都沒(méi)有包含等號(hào),所以考慮都不滿足這兩個(gè)條件的pair,即x\wedge y\le c, x\oplus y\ge c 。然后注意我們求的pair是不包含0的,所以直接數(shù)位dp的答案需要減去滿足上述條件含有0的pair。

然后就是一個(gè)常規(guī)的數(shù)位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;
    }
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容