How Many Zeroes? LightOJ - 1140

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<ctype.h>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
#define LL long long
LL dp[55][55],num[55];
LL dfs(int pos,int iszero,int st,int limit)//pow为数位,iszero判断是否为0,st上一个状态初始为0,limit是否达到上限
{
    if(pos<0)
    {
        if(iszero)
            return 1;
        else
            return st;
    }
    if(!limit&&!iszero&&dp[pos][st]!=-1)
        return dp[pos][st];
    LL ans=0;
    int len=limit?num[pos]:9;
    for(int i=0;i<=len;i++)
    {
        if(iszero)
            ans+=dfs(pos-1,i==0,0,limit&&i==len);
        else
            ans+=dfs(pos-1,0,st+(i==0),limit&&i==len);
    }
    if(!limit&&!iszero)
        dp[pos][st]=ans;
    return ans;

}
LL sv(LL a)
{
    int len=0;
    memset(dp,-1,sizeof(dp));
    memset(num,0,sizeof(num));
    while(a)
    {
        num[len++]=a%10;
        a/=10;
    }
    return dfs(len-1,1,0,1);
}
int main()
{
    LL m,n;
    while(~scanf("%lld%lld",&n,&m))
    {
      //  LL ee=sv(m)-sv(n-1);
        printf("%lld\n",sv(m)-sv(n-1));
    }

}

 

posted @ 2018-08-01 16:19  NoRain丶  阅读(106)  评论(0编辑  收藏  举报