解题报告 『[ZJOI2010]数字计数(数位动规)』

原题地址

不了解数位DP的建议先看一下这位大佬的文章

然后这道模板题应该就能直接看懂代码了。

 

从最低位开始:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)

const int maxn = 20;

int l, r, len;
int num[maxn], dp[maxn][maxn];

void origin() {memset(dp, -1, sizeof(dp));}

int read() {
    int x = 0, flag = 0;
    char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') {
        flag = 1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return flag ? -x : x;
}

int dfs(int pos, int limit, int lead, int dig, int sum) {
    int ans = 0;
    if (pos > len) return sum;
    if (!limit && !lead && dp[pos][sum] != -1) return dp[pos][sum];
    int res = limit ? num[len - pos + 1] : 9;
    rep(i, 0, res) {
        if (!i && lead) ans += dfs(pos + 1, (i == res) && limit, 1, dig, sum + ((i || !lead) && (i == dig)));
        else ans += dfs(pos + 1, (i == res) && limit, 0, dig, sum + ((i || !lead) && (i == dig)));
    }
    if (!limit && !lead) dp[pos][sum] = ans;
    return ans;
}

int work(int x, int d) {
    origin();
    len = 0;
    while(x) {
        num[++len] = x % 10;
        x /= 10;
    }
    return dfs(1, 1, 1, d, 0);
}

void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    l = read(), r = read();
    rep(i, 0, 9) {
        write(work(r, i) - work(l - 1, i));
        printf(" ");
    }
    return 0;
}
View Code

 

从最高位开始:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)

const int maxn = 20;

int l, r, len;
int num[maxn], dp[maxn][maxn];

void origin() {memset(dp, -1, sizeof(dp));}

int read() {
    int x = 0, flag = 0;
    char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') {
        flag = 1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return flag ? -x : x;
}

int dfs(int pos, int limit, int lead, int dig, int sum) {
    int ans = 0;
    if (!pos) return sum;
    if (!limit && !lead && dp[pos][sum] != -1) return dp[pos][sum];
    int res = limit ? num[pos] : 9;
    rep(i, 0, res) {
        if (!i && lead) ans += dfs(pos - 1, (i == res) && limit, 1, dig, sum + ((i || !lead) && (i == dig)));
        else ans += dfs(pos - 1, (i == res) && limit, 0, dig, sum + ((i || !lead) && (i == dig)));
    }
    if (!limit && !lead) dp[pos][sum] = ans;
    return ans;
}

int work(int x, int d) {
    origin();
    len = 0;
    while (x) {
        num[++len] = x % 10;
        x /= 10;
    }
    return dfs(len, 1, 1, d, 0);
}

void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    l = read(), r = read();
    rep(i, 0, 9) {
        write(work(r, i) - work(l - 1, i));
        printf(" ");
    }
}
View Code
posted @ 2019-06-11 10:31  雲裏霧裏沙  阅读(158)  评论(0编辑  收藏  举报