码迷,mamicode.com
首页 > 其他好文 > 详细

beautiful number 数位dp

时间:2019-04-17 21:01:04      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:bool   ber   pre   sizeof   display   inpu   limit   hide   fine   

    题意:  求高位往低位递减且  高位%低位==0(相邻) 数字数量

 

唯一要注意的就是前导零!!!!!!(正因为这个前导零   一开始的pre设置为0       )

比如  11  10 09 08 07 06 05 .。。。。说明要判断前导零

技术图片
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
#define N 10+5
ll dp[N][15];
ll a[N];

ll dfs(int pos,int pre,   bool lead,bool limit  )
{
    if(!pos)return 1;

    if(!limit&&!lead&&dp[pos][pre]!=-1)return dp[pos][pre];
    ll ans=0;
    int up=limit?a[pos]:9;
    rep(i,0,up)
    {
        if(lead||pre>=i&&i&&pre%i==0)
        ans+=dfs(pos-1,i,   lead&&i==0,limit&&i==a[pos]);
    }
    if(!limit&&!lead)dp[pos][pre]=ans;
    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,0,true,true);
}
int main()
{
    int cas;
    RI(cas);
    CLR(dp,-1);
    while(cas--)
    {
        ll a,b;
        cin>>a>>b;
        printf("%lld\n",solve(b)-solve(a-1));
    }
    return 0;
}
View Code

 

beautiful number 数位dp

标签:bool   ber   pre   sizeof   display   inpu   limit   hide   fine   

原文地址:https://www.cnblogs.com/bxd123/p/10726023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!