数位统计DP-计数问题

数位统计DP-计数问题

分情况讨论:【a, b】,0~9

count(n, x),1~n中x出现的次数。

【a, b】中x出现的次数, 用前缀和:count(b, x) - count(a-1, x);

1 ~ n,x = 1

n = abcdefg 分别求出1在每一位上出现的次数

eg:比如求1在第4位(d)上出现的次数?

1 <= xxx1yyy<=abcdefg

(1) xxx = 000 ~ abc-1, yyy = 000~999, abcd * 1000

(2) xxx = abc

(2.1) d < 1, abc1yyy > abc0efg,  0

(2.2) d = 1, efg + 1;

(2.3 d > 1, yyy = 000 ~ 999, 1000

数位统计DP-计数问题

 如果x = 0,那么001 ~ abc - 1

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10;
/*
001~abc-1, 999
abc
    1. num[i] < x, 0
    2. num[i] == x, 0~efg
    3. num[i] > x, 0~999
*/
int get(vector<int> num, int l, int r)
{
    int res = 0;
    for (int i = l; i >= r; i -- ) res = res * 10 + num[i];
    return res;
}
int power10(int x)
{
    int res = 1;
    while (x -- ) res *= 10;
    return res;
}
int count(int n, int x)
{
    if (!n) return 0;

    vector<int> num;
    while (n)
    {
        num.push_back(n % 10);
        n /= 10;
    }
    n = num.size();

    int res = 0;
    for (int i = n - 1 - !x; i >= 0; i -- )
    {
        if (i < n - 1)
        {
            res += get(num, n - 1, i + 1) * power10(i);
            if (!x) res -= power10(i);
        }

        if (num[i] == x) res += get(num, i - 1, 0) + 1;
        else if (num[i] > x) res += power10(i);
    }

    return res;
}

int main()
{
    int a, b;
    while (cin >> a >> b , a)
    {
        if (a > b) swap(a, b);

        for (int i = 0; i <= 9; i ++ )
            cout << count(b, i) - count(a - 1, i) << ' ';
        cout << endl;
    }

    return 0;
}