hdu2089

基本的数位dp

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

#define D(x) x

const int MAX_DIGIT = 35;

int n, m;
int f[MAX_DIGIT];
int memoize[MAX_DIGIT][2][2][2];

void to_digits(int a)
{
    for (int i = 0; i < MAX_DIGIT; i++)
    {
        f[i] = a % 10;
        a /= 10;
    }
}

int dfs(int digit, bool less, bool contain, bool six)
{
    if (digit == -1)
    {
        return !contain;
    }
    if (memoize[digit][less][contain][six] != -1)
    {
        return memoize[digit][less][contain][six];
    }
    int limit = less ? 9 : f[digit];
    int ret = 0;
    for (int i = 0; i <= limit; i++)
    {
        if (i == 4 || (six && i == 2))
        {
            ret += dfs(digit - 1, less || i < f[digit], true, false);
            continue;
        }
        if (i == 6)
        {
            ret += dfs(digit - 1, less || i < f[digit], contain, true);
            continue;
        }
        ret += dfs(digit - 1, less || i < f[digit], contain, false);
    }
    memoize[digit][less][contain][six] = ret;
    return ret;
}

int main()
{
    while (scanf("%d%d", &n, &m), n | m)
    {
        n--;
        to_digits(n);
        memset(memoize, -1, sizeof(memoize));
        int ans_n = dfs(32, false, false, false);
        to_digits(m);
        memset(memoize, -1, sizeof(memoize));
        int ans_m = dfs(32, false, false, false);
        printf("%d\n", ans_m - ans_n);
    }
    return 0;
}
View Code

 

posted @ 2015-01-29 20:02  金海峰  阅读(521)  评论(0编辑  收藏  举报