题目大意: 如果一个数的个数位和能被10整除,则称为Good Number,问区间[A,B]的Good Number 的个数。
分析:数位dp,dp[i][j]表示有i位,数位和除10余j的数的个数。
代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
LL dp[25][10];
char a[25];
void prepare()
{
dp[0][0]=1;
for(int i=0;i<20;i++)
for(int j=0;j<10;j++)
for(int x=0;x<10;x++)//枚举下一位的数
dp[i+1][(j+x)%10]+=dp[i][j];
}
LL solve(LL limit)
{
if(limit<0) return 0;
sprintf(a+1,"%I64d",limit);
int len=strlen(a+1),p=0;
LL ans=0;
for(int i=1;i<=len;i++)
{
for(int j=0;j<a[i]-'0';j++)
ans+=dp[len-i][(10-(j+p)%10)%10];
p+=a[i]-'0';
}
if(p%10==0) ans++;
return ans;
}
int main()
{
int T,cas=1;
LL A,B;
prepare();
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&A,&B);
printf("Case #%d: %I64d\n",cas++,solve(B)-solve(A-1));
}
return 0;
}