1586:【 例 2】数字游戏

/*
reference:
    
Date:
    2019.10.08
sol:
    
*/
#include
using namespace std;
#define int long long
template inline void rd(T &x){x=0;char c=getchar();int f=0;while(!isdigit(c)){f|=c=='-';c=getchar();}while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x=f?-x:x;}
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dwn(i,a,b) for(int i=(a);i>=(b);--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define ee(i,u) for(int i=head[u];i;i=e[i].next)

int l,r,len;
int dp[15][10];
int bit[10+5];

inline int dfs(int pos,int pre,bool limit){
    if(pos>len)return 1;
    if(dp[pos][pre]!=-1 && !limit)return dp[pos][pre];
    int up=limit?bit[len-pos+1]:9;
    int res=0;
    rep(i,pre,up){
        res+=dfs(pos+1,i,i==up && limit);
    } 
    if(!limit)dp[pos][pre]=res;
    return res;
}

inline int work(int x){
    len=0;
    while(x){
        bit[++len]=x%10;
        x/=10;
    }
    mem(dp,-1);
    return dfs(1,0,1);
}

#undef int
int main(){
#define int long long
    #ifdef WIN32
    freopen("test.txt","r",stdin);
    #endif
    while(~scanf("%lld%lld",&l,&r)){
        printf("%lld\n",work(r)-work(l-1));
    }
    return 0;
}

你可能感兴趣的:(1586:【 例 2】数字游戏)