数字游戏

https://www.acwing.com/problem/content/1084/

  • \(f[i][j]:有i位且最高位为j的不增数.\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 15;
int f[N][10];

void init() {
    for (int i = 0; i <= 9; ++i) f[1][i] = 1;
    for (int i = 2; i < N; ++i)
        for (int j = 0; j <= 9; ++j)
            for (int k = j; k <= 9; ++k)
                f[i][j] += f[i - 1][k];
}

int dp(int n) {
    if (!n) return 1;
    vector<int> num;
    while (n) num.pb(n % 10), n /= 10;
    int res = 0, last = 0;
    n = num.size();
    for (int i = n - 1; i >= 0; --i) {
        int x = num[i];
        for (int j = last; j < x; ++j) res += f[i + 1][j]; //枚举左边
        if (x < last) break;//此时所有情况已在上一层算完
        last = x;
        if (!i) ++res; //右边的分支
    }
    return res;
    
}

int main() {
    IO;
    init(); 
    int l, r;
    while(cin >> l >> r) cout << dp(r) - dp(l - 1) << '\n';
    return 0;
}

posted @ 2021-02-02 02:10  phr2000  阅读(46)  评论(0编辑  收藏  举报