hdu5179---beautiful number

dp[i][e],表示i位数,最高位为e,符合题目条件的数的个数,然后数位dp

/*************************************************************************
    > File Name: hdu5179.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月01日 星期日 12时56分21秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[12][12];
int bit[12];
int cnt;

int dfs(int cur, int e, bool flag, bool zero)
{
    if (cur == -1)
    {
        if (zero)
        {
            return 0;
        }
        return 1;
    }
    if (!flag && ~dp[cur][e])
    {
        return dp[cur][e];
    }
    int ans = 0;
    int end = flag ? bit[cur] : 9;
    for (int i = 0; i <= end; ++i)
    {
        if (zero && !i)
        {
            ans += dfs (cur - 1, 0, flag && (i == end), 1);
        }
        else if (zero && i)
        {
            ans += dfs (cur - 1, i, flag && (i == end), 0);
        }
        else
        {
            if (cnt - 1 == cur)
            {
                ans += dfs (cur - 1, i, flag && (i == end), 0);
            }
            else if (i && e % i == 0)
            {
                ans += dfs (cur - 1, i, flag && (i == end), 0);
            }
        }
    }
    if (!flag)
    {
        dp[cur][e] = ans;
    }
    return ans;
}

int calc (int n)
{
    cnt = 0;
    while (n)
    {
        bit[cnt++] = n % 10;
        n /= 10;
    }
    return dfs (cnt - 1, 0, 1, 1);
}

int main ()
{
    int t;
    memset (dp, -1, sizeof(dp));
    scanf("%d", &t);
    while (t--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", calc (r) - calc (l - 1));
    }
    return 0;
}
时间: 2024-08-26 20:45:03

hdu5179---beautiful number的相关文章

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\) 就是一个漂亮的数. 求区间 \([

Codeforces 55D Beautiful Number

Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. Input The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two

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 "

hdu 5179 beautiful number

beautiful number 问题描述 令 A = \sum_{i=1}^{n}a_i * {10}^{n-i}(1\leq a_i \leq 9)A=∑?i=1?n??a?i??∗10?n−i??(1≤a?i??≤9)(nn为AA的位数).若AA为“漂亮的数”当且仅当对于任意1 \leq i < n1≤i<n满足a[i] \geq a[i+1]a[i]≥a[i+1]且对于任意1 \leq i \leq n,i < j \leq n1≤i≤n,i<j≤n,满足a[i]a[i]

HDU 5179 beautiful number 离线处理

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 104 Problem Description Let A=∑ni=1ai?10n?i(1≤

Beautiful number

一个数是美丽的,当且仅当其可以被其所有的非0数位整除.求在区间[a,b]中有多少数是美丽的. a,b<=10^18 这道题很明显是数位DP的分格,f[i][j][k][sta]表示前i个数,取值是否贴紧,前i个数的值%2520的值,前i个数的lcm(这里用离散化存),的方案数,答案就是count(b)-count(a-1); 还有数位DP中的技巧就是加上一个状态j表示是否贴紧. 666HQ的代码 #include<cstdio> #include<iostream> #inc

cf B Very Beautiful Number

题意:给你两个数p和x,然后让你找出一个长度为p的数,把它的最后移到最前面之后得到的数是原来数字的x倍,有很多这样的数取最小. 思路:枚举最后一位,然后就可以推出整个的一个数,然后比较得到的数的第一个数字和枚举的数字是否相等既可以. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 7 int

ZOJ 2829 Beautiful Number(睡前一水)

链接:click here 题意:输出第N个能被3或者5整除的数,注意是||而不是&&. 代码: <pre name="code" class="cpp">//zoj 2829 #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <math.h> using

hdu 5179 beautiful number(构造,,,,)

题意: 一个如果称作是漂亮数,当且仅当满足: 每一位上的数字是[1,9],从高到时低数字大小降序,且有di%dj=0(i<j) 例:931 给一个区间[L,R],问这个区间里有多少个漂亮数. 1≤L≤R≤109 思路: 漂亮数一看就很少.可以直接构造. 哎,,,用了DP+构造,写了好久...其实很简单的一个DFS构造就行了. 过些天补上代码.先贴冗长代码~ 代码: int T,L,R; int wei; int ans1,ans2,ans; int d[15], w[15]; int dp[15

【HDOJ】5179 beautiful number

DFS. 1 /* 5179 */ 2 #include <iostream> 3 #include <algorithm> 4 #include <map> 5 #include <cstdio> 6 #include <cstring> 7 using namespace std; 8 9 #define MAXN 1500 10 11 map<int, bool> tb; 12 int a[MAXN], n, v; 13 int