Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3944 Accepted Submission(s): 1255
Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).
Output
For test case X, output “Case #X: ” first, then output the number of good numbers in a single line.
Sample Input
2
1 10
1 20
Sample Output
Case #1: 0
Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
[题意][计算区间里有多少个数各位上的数加起来是10的倍数]
【题解】【数位dp】
【dp预处理,f[i][j]表示i位数模10余数为j的有多少个(当从前一位递推到当前位时,不管前面几位加起来模10余数是几,都可以找到相应的数使它是10的倍数,所以可以直接继承)】
【在查找的时候要用last记录前面几位的和(因为当枚举到当前位时,低位上的数依然会影响答案)】
【依然用前缀和来确定答案】
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
ll f[20][50],h[20],tot;
ll n,m,T;
inline void dp()
{
int i,j,k;
f[0][0]=1;
for(k=1;k<20;++k)
for(i=0;i<10;++i)
for(j=0;j<10;++j)
f[k][i]+=f[k-1][(i+j)%10];
return;
}
inline ll math(ll n)
{
ll i,j,sum=0,last=0;
memset(h,0,sizeof(h)); tot=0;
while(n) h[++tot]=n%10,n/=10;
for(i=tot;i>0;--i)
{
for(j=0;j<h[i];++j)
sum+=f[i-1][((0-(last+j))%10+10)%10];
last+=h[i];
}
return sum;
}
int main()
{
dp();
scanf("%I64d",&T);
for(int i=1;i<=T;++i)
{
scanf("%I64d%I64d",&n,&m);
printf("Case #%d: %I64d\n",i,math(m+1)-math(n));
}
return 0;
}