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;
}

原文地址:https://www.cnblogs.com/quanjun/p/11972343.html

时间: 2024-08-05 09:36:45

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)Total Submission(s): 801    Accepted Submission(s): 518 Problem Description Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is the number of A's digits). We call A as "

5179 beautiful number(数位dp)

原题链接 题意:求[l,r]中高位%低位等于0的数字个数.(不含0)分析:此题有三种方法.1.暴搜,毕竟最多才10个位.2.数位dp,预处理好整体的,再处理细节. dp[i][j]表示第i位上的数字位j的情况数,dp[i][j]+=dp[i-1][k](j%k==0) 3.猜想这样的数字并不多,于是打表. 数位dp #include <cstdio> #include <iostream> #include <cmath> #include <algorithm&

Balanced Number (数位dp)

Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box wit

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

SPOJ MYQ10 10649. Mirror Number (数位dp)

SPOJ MYQ10 10649. Mirror Number (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中镜像回文的个数. 0 <= a<=b <= 10^44 分析: 看到题目和数据范围就知道是数位dp了. 很明显镜像回文只有0,1,8,跟回文的一题一样,在dfs的时候得开个辅助数组记录前面已经选择的数字. 注意还得去掉前导0. 代码: /* * Author: illuz <iilluzen[at]gmail.com

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

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

题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有关,和前一项有关,和当前状态有关,我们定义dp[i][j][k]第i位前面j状态k的后面的最佳情况. 状态有7种: 0什么都没,1刚开始第一个上坡,2已经第一个上坡了可以转折了,3第一个下坡04刚开始第二个上坡,5已经第二个上坡可以转折了,6第二个下坡 然后数位DP一下就好了. 注意,要开ull,3

CF 55D Beautiful numbers (数位DP)

题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018) 思路: 数字很大,不能暴力.但是想要知道一个数是否为Beautiful number时,至少得等到它的所有位都出现吧?不然如何确定其实可以被整除的呢? 分析一下,类似2232和3232等这样的数字,这两个只是出现了2和3而已,他们的lcm都是6,所以有可以压缩统计的地方就是lcm,开一维来存储.接下来考虑

CodeForces - 55D Beautiful numbers (数位DP)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num