题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179

题目大意:

给你一个数 \(A = a_1a_2 \cdots a_n\) ,我们称 \(A\) 为“漂亮的数”当且仅当 \(a[i] \ge a[i+1]\) (\(1 \le i \lt n\)) 并且 \(a[i]\) mod \(a[j] = 0\) (\(1 \le i \lt n, i \lt j \le n\)),比如 \(931\) 就是一个漂亮的数。

求区间 \([L,R]\) 范围内有多少“漂亮的数”。

问题分析:

本题使用 数位DP 解决。

首先,要注意一个条件:

  • 对于任意 \(i \lt j\) ,\(a[i]\) mod \(a[j] = 0\)

这其实等同于对于任意满足条件 \(i \le k \lt j\) 的 \(k\) ,\(a[k]\) mod \(a[k+1] = 0\) 。

即:对于当前位置 \(pos\) ,它的前一位要么一直都是 \(0\) ,要么 \(a[pos+1]\) (即 \(pos\) 位的高一位)能被 \(a[pos]\) 整除。

我们可以开函数 dfs(int pos, int pre, bool limit) 来解决这个问题,其中:

  • pos 表示当前所处的数位;
  • pre 表示前一位的数值;
  • limit 表示当前是否处于限制状态。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int f[33][10], a[33];
void init() {
memset(f, -1, sizeof(f));
}
int dfs(int pos, int pre, bool limit) {
if (pos < 0) return 1;
if (!limit && f[pos][pre] != -1) return f[pos][pre];
int up = limit ? a[pos] : 9;
int down = pre ? 1 : 0;
int tmp = 0;
for (int i = down; i <= up; i ++) {
if (pre && pre % i) continue;
tmp += dfs(pos-1, i, limit && i==up);
}
if (!limit) f[pos][pre] = 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 T, L, R;
int main() {
init();
scanf("%d", &T);
while (T --) {
scanf("%d%d", &L, &R);
printf("%d\n", get_num(R)-get_num(L-1));
}
return 0;
}

HDU5179 beautiful number 题解 数位DP的更多相关文章

  1. HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)

    beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 5179 beautiful number(数位dp)

    原题链接 题意:求[l,r]中高位%低位等于0的数字个数.(不含0)分析:此题有三种方法.1.暴搜,毕竟最多才10个位.2.数位dp,预处理好整体的,再处理细节. dp[i][j]表示第i位上的数字位 ...

  3. HDU3709 Balanced Number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...

  4. HDU 3565 Bi-peak Number(数位DP)题解

    题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...

  5. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  6. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

  7. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  8. CF 55D Beautiful numbers (数位DP)

    题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...

  9. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

随机推荐

  1. HDFS概念

  2. Layout布局(补充)

    HBoxLayout和VBoxLayout HBoxLayout和VBoxLayout布局都比较简单,也叫箱式布局,它按照先后顺序进行横向布局或垂直布局.另外这两种布局也提供了pack属性支持,设置内 ...

  3. avalon2 第一个demo

    <!DOCTYPE html> <html> <head> <title>TODO supply a title</title> <m ...

  4. 【转载】Windows平台下利用APM来做负载均衡方案 - 负载均衡(下)

    概述 我们在上一篇Windows平台分布式架构实践 - 负载均衡中讨论了Windows平台下通过NLB(Network Load Balancer) 来实现网站的负载均衡,并且通过压力测试演示了它的效 ...

  5. PLSQL中的三种参数模式IN、OUT、IN OUT

    原文链接:https://www.cnblogs.com/zbj815/p/6854108.html 1.IN模式 IN模式是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改 ...

  6. Tomcat停,图片名字中文显示不出来

    Tomcat停,图片名字中文显示不出来     Tomcat下,图片名字中文显示不出来在tomcat的server.xml中加入URIEncoding="utf-8"<Con ...

  7. js 页面分享

    首先说分享到QQ空间的通用代码:<a href="javascript:void(0);" onclick="window.open('http://sns.qzo ...

  8. 1626 - Brackets sequence——[动态规划]

    Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...

  9. js 对象的深拷贝

    function deepCopy(obj) { var result = Array.isArray(obj) ? [] : {}; for (var key in obj) { if (obj.h ...

  10. CodeForces 906D (欧拉降幂)

    Power Tower •题意 求$w_{l}^{w_{l+1}^{w_{l+2}^{w_{l+3}^{w_{l+4}^{w_{l+5}^{...^{w_{r}}}}}}}}$ 对m取模的值 •思路 ...