POJ3252Round Numbers(數位dp)

自為風月馬前卒發表於2018-09-12

題意

給出區間$[A, B]$,求出區間內的數轉成二進位制後$0$比$1$多的數的個數

$1 leqslant A, B leqslant 2,000,000,000$

Sol

比較zz的數位dp

直接在二進位制下dp就好

$f[i][ze][on]$表示第$i$位,填了$ze$個$0$,$on$個1的方案數

#include<cstdio>
#include<cstring>
#include<iostream>
// #include<map>
using namespace std;
#define LL long long
const LL MAXN = 101;
LL A, B;
LL num[MAXN], tot, f[MAXN][MAXN][MAXN];
LL dfs(LL x, bool lim, LL ze, LL on) {
    if(x == 0) return 
        (ze != -1) && (on != -1) && (ze >= on);
    if(!lim && f[x][ze][on]) return f[x][ze][on];
    LL ans = 0;
    for(LL i = 0; i <= (lim ? num[x] : 1); i++) {
        if(i == 0) ans += dfs(x - 1, lim && (i == num[x]), ze == -1 ? 1 : ze + 1, on);
        else {
            if(on == -1) ans += dfs(x - 1, lim && (i == num[x]), 0, 1);
            else ans += dfs(x - 1, lim && (i == num[x]), ze, on + 1);
        }
    }
    if(!lim) f[x][ze][on] = ans;
    return ans;
}
LL solve(LL x) {
    tot = 0;
    while(x) num[++tot] = x % 2, x >>= 1;
    return dfs(tot, 1, -1, -1);
}
int main() {
    cin >> A >> B;
    cout << solve(B) - solve(A - 1);
    return 0;
}
/*
1234 4444
2
*/

 

相關文章