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 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).
For each case, print the case number and the number of zeroes written down by Jimmy.
5
10 11
100 200
0 500
1234567890 2345678901
0 4294967295
Case 1: 1
Case 2: 22
Case 3: 92
Case 4: 987654304
Case 5: 3825876150
求n-m中0的个数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef long long ll;
ll a[20];
ll dp[20][20];//第i位前面有j个0
ll dfs(ll pos,ll iszero,ll s,ll lim)//当前位置,前面是否全是0,前面0的个数,前一位是否到了最大值
{
if(pos==-1)
{
if(iszero)//如果所有的数位都是0,那么该数字是0,则算一个有效0
return 1;
else
return s;
}
if(!lim&&dp[pos][s]!=-1&&!iszero)
return dp[pos][s];
int up=lim?a[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(iszero)
ans+=dfs(pos-1,i==0,0,lim&&i==up);//如果前面全是0,判段当前位置是不是0
else
ans+=dfs(pos-1,0,s+(i==0),lim&&i==up);//如果前面不全是0,如果i是0那么此时的0是一个有效0
}
if(!lim&&!iszero)
dp[pos][s]=ans;
return ans;
}
ll solve(ll n)
{
int i=0;
while(n)
{
a[i++]=n%10;
n/=10;
}
return dfs(i-1,1,0,1);
}
int main()
{
int t;
scanf("%d",&t);
int kase=1;
memset(dp,-1,sizeof(dp));
while(t--)
{
ll n,m;
scanf("%lld%lld",&n,&m);
printf("Case %d: ",kase++);
printf("%lld\n",solve(m)-solve(n-1));
}
return 0;
}