• hdu3709


    枚举+数位dp

    注意处理数字为0和1的情况。

    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #define D(x) 
    
    const int MAX_DIGIT = 20;
    
    long long n;
    int f[MAX_DIGIT];
    long long memoize[MAX_DIGIT][20*20*9];
    int pivot;
    
    int to_digits(long long a)
    {
        int ret = 0;
        while (a > 0)
        {
            f[++ret] = a % 10;
            a /= 10;
        }
        return ret;
    }
    
    long long dfs(int digit, bool less, int weight)
    {
        if (digit <= 0)
        {
            return weight == 0;
        }
        if (less && memoize[digit][weight] != -1)
        {
            return memoize[digit][weight];
        }
        int limit = less ? 9 : f[digit];
        long long ret = 0;
        for (int i = 0; i <= limit; i++)
        {
            int new_weight = weight + (digit - pivot) * i;
            if (new_weight < 0)
            {
                continue;
            }
            ret += dfs(digit - 1, less || i < f[digit], new_weight);
        }
        memoize[digit][weight] = ret;
        return ret;
    }
    
    long long work(long long n)
    {
        if (n < 0)
        {
            return 0;
        }
        int len = to_digits(n);
        long long ret = 0;
        for (int i = 1; i <= len; i++)
        {
            pivot = i;
            memset(memoize, -1, sizeof(memoize));
            ret += dfs(len, false, 0);
        }
        return ret - len + 1;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            long long a, b;
            scanf("%lld%lld", &a, &b);
            printf("%lld
    ", work(b) - work(a - 1));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Java常量初始化后不会再去重新获取
    Intellij IDEA自动编译问题
    Tomcat关闭日志输出
    MySQL命令行导出数据库
    补充Mysql5.7用法
    Linux下安装 mysql 5.7
    IE9 表格错位bug
    Ubuntu忘记管理员密码
    实体转换计算器
    js生成二维码参数设置
  • 原文地址:https://www.cnblogs.com/rainydays/p/4286398.html
Copyright © 2020-2023  润新知