HDU5179 beautiful number 题解 数位DP
题目链接: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的更多相关文章
- HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)
beautiful number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5179 beautiful number(数位dp)
原题链接 题意:求[l,r]中高位%低位等于0的数字个数.(不含0)分析:此题有三种方法.1.暴搜,毕竟最多才10个位.2.数位dp,预处理好整体的,再处理细节. dp[i][j]表示第i位上的数字位 ...
- HDU3709 Balanced Number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...
- HDU 3565 Bi-peak Number(数位DP)题解
题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...
- HDU 3709 Balanced Number(数位DP)题解
思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- HDU 5787 K-wolf Number (数位DP)
K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...
- CF 55D Beautiful numbers (数位DP)
题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
随机推荐
- HZOJ 回家
这篇博客大部分在写我的错解……明明很简单的一道题,知道正解后10分钟AC,然而几个错解打的想死…… 错解1 WA40: 鬼知道这40分哪来的……这也是考试最后很无奈地交上去的代码,最后剩20分钟时发现 ...
- 2018-8-10-UWP-WPF-解决-xaml-设计显示异常
title author date CreateTime categories UWP WPF 解决 xaml 设计显示异常 lindexi 2018-08-10 19:16:53 +0800 201 ...
- lrj 9.4.1 最长上升子序列 LIS
p275 d(i)是以Ai为结尾的最长上升子序列的长度 <算法竞赛入门经典-训练指南>p62 问题6 提供了一种优化到 O(nlogn)的方法. 文本中用g(i)表示d值为i的最小状态编号 ...
- 2019-1-29-win10-uwp-使用-Microsoft.Graph-发送邮件
title author date CreateTime categories win10 uwp 使用 Microsoft.Graph 发送邮件 lindexi 2019-01-29 16:36:3 ...
- tsung测试xmpp遇到no_free_userid
tsung里面可以配置xmpp的参数,设置一下 <option type="ts_jabber" name="userid_max" value=&quo ...
- java声明异常(throws)
在可能出现异常的方法上声明抛出可能出现异常的类型: 声明的时候尽可能声明具体的异常,方便更好的处理. 当前方法不知道如何处理这种异常,可将该异常交给上一级调用者来处理(非RuntimeExceptio ...
- 【u224】传送机
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 刷完牙洗完脸,黄黄同学就要上课去了.可是黄黄同学每次去上课时总喜欢把校园里面的每条路都走一遍,当然,黄 ...
- Python--day32--struct模块定制报头理论(什么是网络协议?网络协议的本质是什么?)
- P1066 汪老师玩卡片
题目描述 汪老师得到了一些卡片,这些卡片上标有数字0或5.现在他可以选择其中一些卡片排成一列,使得排出的一列数字组成的数最大,且满足被90整除这个条件.同时这个数不能含有前导0,即0不能作为这串数的首 ...
- 微信里首次跳转会到首页问题(window.location失效)
将window.location.href 换为location.href