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][15];

int calc(int x){
    int cn=0;
    while(x){
        x/=10;
        ++cn;
    }
    return cn;
}

void dfs(int pos,bool state){
    if(pos>wei){
        ++ans;
        return;
    }
    if(pos==1){
        if(state){
            rep(i,1,d[pos]){
                w[pos]=i;
                dfs(pos+1,i==d[pos]);
            }
        }else{
            rep(i,1,9){
                w[pos]=i;
                dfs(pos+1,state);
            }
        }
    }else{
        if(state){
            rep(i,1,d[pos]){
                if(w[pos-1]%i==0){
                    w[pos]=i;
                    dfs(pos+1,i==d[pos]);
                }
            }
        }else{
            rep(i,1,9){
                if(w[pos-1]%i==0){
                    w[pos]=i;
                    dfs(pos+1,state);
                }
            }
        }
    }
}

int main(){

    cin>>T;
    while(T--){
        scanf("%d%d",&L,&R);

        L--;
        int tempL=L;
        wei=calc(L);
        rep2(i,wei,1){
            d[i]=(L%10);
            L/=10;
        }
        ans=0;
        if(tempL==0){
            ans=0;
        }else{
            dfs(1,true);
        }
        ans1=ans;

        mem(dp,0);
        rep(i,1,9) dp[1][i]=1;
        rep(i,2,wei-1){
            rep(j,1,9){
                rep(k,1,9){
                    if(j%k==0){
                        dp[i][j]+=dp[i-1][k];
                    }
                }
            }
        }
        rep(i,1,wei-1){
            rep(j,1,9) ans1+=dp[i][j];
        }

        wei=calc(R);
        rep2(i,wei,1){
            d[i]=(R%10);
            R/=10;
        }
        ans=0;
        dfs(1,true);
        ans2=ans;

        mem(dp,0);
        rep(i,1,9) dp[1][i]=1;
        rep(i,2,wei-1){
            rep(j,1,9){
                rep(k,1,9){
                    if(j%k==0){
                        dp[i][j]+=dp[i-1][k];
                    }
                }
            }
        }
        rep(i,1,wei-1){
            rep(j,1,9) ans2+=dp[i][j];
        }

        printf("%d\n",ans2-ans1);
    }

    return 0;
}
时间: 2024-10-27 11:27:38

hdu 5179 beautiful number(构造,,,,)的相关文章

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≤

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 4781 Beautiful Soup 构造

并不是很难的一个构造,我在比赛的时候把题目读错了,补题的时候想得比较粗糙,迟迟没过这题,之后想法慢慢细致起来,还是将这题过了. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0

【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

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&

hdu 5179 数位dp

题目链接: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): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1

HDU Redraw Beautiful Drawings 判断最大流是否唯一解

点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1660    Accepted Submission(s): 357 Problem Description Alice and Bob are playing together. Alice is crazy about

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