The Counting Problem

Time limit:3000ms

Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be
1024 1025 1026 1027 1028 1029 1030 1031 1032 there are ten 0’s in the list, ten 1’s, seven 2’s, three 3’s, and etc.

Input

The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line ‘0 0’, which is not considered as part of the input.

Output

For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 10
44 497 346 542 1199 1748 1496 1403 1004 503 1714 190 1317 854 1976 494 1001 1960 00

Sample Output

1211111111
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106 16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200 413 1133 503 503 503 502 502 417 402 412 196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471 294 1256 296 296 296 296 287 286 286 247

题意

        计算[a,b]或[b,a]区间内0~9分别出现多少次。

思路

        数位DP一下。dp[val][pos][cnt][lead],val表示0~9的状态,pos表示位数,cnt表示pos位数上有cnt个val的状态,lead表示前导0是否存在。val不用多说,pos也不用多说,cnt这个状态很有意思表示这个数一共有多少个val,lead表示这个数是否有前导0,这里lead需要解释一下:lead的状态是必须的,当存在前导0的时候肯定没有问题不会计算错误,但是如果不存在前导0的时候呢现在是一个pos位数枚举到更少的位时如dp[val][pos-1][0][lead]此时已经不是前导0状态了但是还是需要计算pos-1位时0作为开头而不是前导的个数。如1234567计算到了1234000时(前4位)后三位是可以取存在前导0的情况的个数的。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long ll;

ll ar[10];
ll br[10];

int dit[10];

ll dp[10][10][10][2];

ll dg (int pos, int val, int cnt, bool lead, bool limit) {
    if (pos == -1) {
        if (lead) {
            return 0;
        }
        else {
            return cnt;
        }
    }
    if (!limit && dp[val][pos][cnt][lead] != -1) {
        return dp[val][pos][cnt][lead];
    }
    int nd = limit?dit[pos]:9;
    ll ans = 0;
    for (int i = 0; i <= nd; ++i) {
        if (lead) {
            if (i == 0) {
                ans += dg(pos-1, val, 0, 1, limit&&(i==nd));
            }
            else {
                ans += dg(pos-1, val, (i==val), 0, limit&&(i==nd));
            }
        }
        else {
            ans += dg(pos-1, val, cnt+(i==val), 0, limit&&(i==nd));
        }
    }
    if (!limit) {
        dp[val][pos][cnt][lead] = ans;
    }
    return ans;
}

void cal (ll k, ll *f) {
    int pos = 0;
    while (k) {
        dit[pos++] = (int)(k%10);
        k /= 10;
    }
    for (int i = 0; i <= 9; ++i) {
        f[i] = dg(pos-1, i, 0, 1, 1);
    }
}

int main () {
    ll a, b;
    memset(dp, -1, sizeof(dp));
    while (scanf("%lld %lld", &a, &b) != EOF && a+b) {
        if (a>b) {
            swap(a, b);
        }
        cal(a-1, ar);
        cal(b, br);
        for (int i = 0; i <= 9; ++i) {
            cout << br[i]-ar[i];
            if (i == 9) {
                cout << '\n';
            }
            else {
                cout << ' ';
            }
        }
    }
    return 0;
}

发表评论