数位dp写多少零

Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

Input

Input starts with an integer T (≤ 11000), denoting the number of test cases.

Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

Output

For each case, print the case number and the number of zeroes written down by Jimmy.

Sample Input

5

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

Sample Output

Case 1: 1

Case 2: 22

Case 3: 92

Case 4: 987654304

Case 5: 3825876150

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
LL dp[20][20];
LL a[20];
LL dfs(LL len,LL s,LL num,LL fp)//是判断前导零,也就是判断是不是这个数前面全是零,s=0表示此位数前面全是0,s=1表示不全是零。
{
    if(!len)return s==0?1:num;
     if(!fp && s && dp[len][num] != -1)
        return dp[len][num];
        LL n = fp ? a[len]:9;
    LL res = 0;
    for(LL i=0;i<=n;i++)
    {
        if(i==0)
        {
            if(s)
            {
                res+=dfs(len-1,s,num+1,fp&&i==n);
            }
            else
            {
                res+=dfs(len-1,0,num,fp&&i==n);
            }
        }
            else
            {
                res+=dfs(len-1,1,num,fp&&i==n);
            }
    }
    if(!fp&&s)
    {
        dp[len][num]=res;
    }
    return res;
}
 LL solve (long long x)
 {
     LL len=0;
     while(x)
     {
         a[++len]=x%10;
         x/=10;
     }
    return dfs(len,0,0,1);
 }
 int main()
 {
     LL t;
     cin>>t;
     long long m,n;
     for(LL i=1;i<=t;i++)
      {
          memset(dp,-1,sizeof(dp));
          memset(a,0,sizeof(a));
          cin>>m>>n;
         cout<<"Case "<<i<<": "<<solve(n)-solve(m-1)<<endl;
      }
 }

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/82713660