zoukankan      html  css  js  c++  java
  • 数位DP模板

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int t;
    long long dp[19][19][2005];
    long long l, r;
    int digit[20];
    
    long long dfs(int len,...int if4..., bool limit)
    {
        if (len == 0)
            return 1; //个位
        if (!limit && dp[len][...])
            return dp[len][...];  //dp数组的内容应和dfs调用参数的内容相同,除了是否达到上限
        long long cnt = 0;
        int up_bound = (limit ? digit[len] : 9);
        for (int i = 0; i <= up_bound; i++)
        {
            if(...) continue; //剪枝
            ...;
            cnt += dfs(len-1, ..., limit && i == up_bound);
        }
        if (!limit) //完整状态
            dp[len][...] = cnt;
        return cnt;
    }
    
    long long solve(long long x)
    {
        int k = 0;
        while (x)
        {
            digit[++k] = x % 10;
            x /= 10;
        }
        return dfs(k,...,1)
    }
    
    int main()
    {
            memset(dp, 0, sizeof(dp));
            scanf("%lld%lld", &l, &r);   //有些题目其实并不需要用到long long
            printf("%lld
    ", solve(r) - solve(l - 1)); //只有满足区间减法才能用
        return 0;
    }
  • 相关阅读:
    冒泡排序-用函数写
    c#语言基础
    c#小知识点
    令人头疼的冒泡排序
    字符串 与函数
    数组 冒泡排序 打印菱形 随机生成不同的数
    if语句练习
    运算符练习
    类型转换
    C#初学
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9425497.html
Copyright © 2011-2022 走看看