题意就是问从n到m之间有多少个0。数位dp枚举每一位dp[i][j]表示到i为有j个0的有多少
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
LL dp[22][22];
int dig[20];
LL F(int pos,int num,int q,int f)//q标记是不是有前导0
{
if(pos==0)return q?1:num;
if(!q&&!f&&dp[pos][num]!=-1)
return dp[pos][num];
int End=f?dig[pos]:9;
LL ans=0;
for(int i=0;i<=End;i++)
{
if(q)
{
if(i==0)
ans+=F(pos-1,num,q,f&&(i==End));
else ans+=F(pos-1,num,0,f&&(i==End));
}
else
{
if(i==0)
ans+=F(pos-1,num+1,0,f&&(i==End));
else ans+=F(pos-1,num,0,f&&(i==End));
}
}
if(q==0&&f==0)
dp[pos][num]=ans;
return ans;
}
LL solve(LL n)
{
if(n<0) return 0;
if(n==0) return 1;
int len=0;
while(n)
{
dig[++len]=n%10;
n/=10;
}
return F(len,0,1,1);
}
int main()
{
int i,j,t,ca=1;
scanf("%d",&t);
LL n,m;
memset(dp,-1,sizeof(dp));
while(t--)
{
scanf("%lld%lld",&n,&m);
printf("Case %d: %lld\n",ca++,solve(m)-solve(n-1));
}
return 0;
}