• HDU2089 不要62 题解 数位DP


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089
    题目大意:求区间 \([l,r]\) 范围内不包含数字“4”且不包含连续的数字“62”的数的数量。
    解题思路:
    数位DP 入门题。
    我们开一个函数 dfs(int pos, int stat, bool limit) ,其中:

    • pos表示目前将要访问的数的位置;
    • pre表示前一个值是不是6,因为这里涉及到62连数(而单个4不需要存储之前的状态, stat \(=1\) 说明前一个数是6,\(=0\) 活命前一个数不是6),所以我们在访问到后一位的时候要考虑前一位,这里pre就用于存储前一位;
    • limit表示是不是在边界位置(limit \(=1\) 表示目前处于限制范围内;\(=0\) 表示目前未处于限制范围内)。

    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    
    int a[20], f[20][2];
    
    int dfs(int pos, int stat, bool limit) {
        if (pos < 0) return 1;  // 说明找到了1种情况
        if (!limit && f[pos][stat] != -1) return f[pos][stat];
        int up = limit ? a[pos] : 9;
        int tmp = 0;
        for (int i = 0; i <= up; i ++) {
            if (i == 4 || stat && i == 2) continue;
            tmp += dfs(pos-1, i==6, limit && i==up);
        }
        if (!limit) f[pos][stat] = tmp;
        return tmp;
    }
    
    int get_num(int x) {
        int pos = 0;
        while (x) {
            a[pos++] = x % 10;
            x /= 10;
        }
        return dfs(pos-1, 0, true);
    }
    
    int main() {
        int l, r;
        memset(f, -1, sizeof(f));
        while (~scanf("%d%d", &l, &r) && l) {
            printf("%d\n", get_num(r) - get_num(l-1));
        }
        return 0;
    }
    
  • 相关阅读:
    Java反射----------------判断对象是否为空
    docker安装MongoDB创建用户,并用工具Robo连接简单CRUD
    Ubuntu 配置ip地址
    java时间的处理
    oracle my2_ep解密
    oracle 查询前7天的数据
    多表修改和多表删除
    迭代器遍历Map、List、Set
    冒泡排序
    Java有那两类异常?
  • 原文地址:https://www.cnblogs.com/quanjun/p/11963373.html
Copyright © 2020-2023  润新知