AcWing 1082. 数字游戏

题目传送门

无脑模板,套进去\(AC\)

#include <bits/stdc++.h>

using namespace std;

const int N = 35;

int l, r;
int a[N], al;
int f[N][N];
/**
 *
 * @param pos 当前的数位
 * @param st  前一位填写的是什么
 * @param op  是不是贴上界
 * @return    在当前情况下,后续符合条件的有多少个
 */
int dp(int pos, int st, int op) {
    if (!pos) return 1; //如果走完所有情况,还活着,说明前面的都符合条件,就是一个合理解++
    if (!op && ~f[pos][st]) return f[pos][st];
    int res = 0, up = op ? a[pos] : 9;
    for (int i = st; i <= up; i++)//注意起始点,i >= pre
        res += dp(pos - 1, i, op && i == a[pos]);
    return op ? res : f[pos][st] = res;
}

int calc(int x) {
    memset(f, -1, sizeof f);
    al = 0;
    while (x) a[++al] = x % 10, x /= 10;
    return dp(al, 0, 1);
}

int main() {
    while (cin >> l >> r)
        cout << calc(r) - calc(l - 1) << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15793107.html