bzoj 1833 数位dp

很裸的数位dp。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std;

const int N = 2e5 + 7;
const int M = 1e7 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;

LL f[20][20], L, R;
int s[20], tot;

LL dp(int p, int cnt, bool limit, bool zero, int val) {
    if(p == -1) return cnt;
    if(!limit && !zero && ~f[p][cnt]) return f[p][cnt];
    LL ans = 0;
    int up = limit ? s[p] : 9;
    for(int i = 0; i <= up; i++) {
        if(!val) ans += dp(p - 1, cnt + (!zero && (!i)), limit && (i == up), zero && (!i), val);
        else ans += dp(p - 1, cnt + (val == i), limit && (i == up), zero && (!i), val);
    }
    if(!limit && !zero) f[p][cnt] = ans;
    return ans;
}

LL solve(LL x, int val) {
    if(x < 1) return 0;
    tot = 0;
    for(LL i = x; i; i /= 10) s[tot++] = i % 10;
    memset(f, -1, sizeof(f));
    return dp(tot - 1, 0, 1, 1, val);
}

int main() {
    scanf("%lld%lld", &L, &R);
    for(int i = 0; i < 10; i++) {
        printf("%lld ", solve(R, i) - solve(L - 1, i));
    }
    puts("");
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/9589275.html