2015
09-17

# Good Numbers

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.

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).

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).

2
1 10
1 20

Case #1: 0
Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
#define LL  __int64
const int MAXN = 111;

LL dp[MAXN][11]={0};///注意这里的范围,注意这里的dp[i][j]，i表示位，表示最低位是第i位，
///然后该位上可以放0-9这十种情况下的数目
int dig[MAXN]={0};
LL dfs(int p,int m,bool flag)
{
if(p==0)return (m==0);
if(flag&&dp[p][m]!=-1)return dp[p][m];
int len=flag?9:dig[p];

LL res=0;
for(int i=0;i<=len;++i)
{
res+=dfs(p-1,(m+i)%10,flag||i!=len);
}
if(flag) dp[p][m]=res;
return res;
}

LL get(LL x)
{
if(x<0)return 0;
//memset(dig,0,sizeof(dig));

int tot=0;
while(x!=0)
{
dig[++tot]=x%10;
x/=10;
}
return dfs(tot,0,0);
}

int main()
{
int t;
cin>>t;
memset(dp,-1,sizeof(dp));
int cas=1;
while(t--)
{
LL x,y;
cin>>x>>y;
printf("Case #%d: %I64d\n",cas++,get(y)-get(x-1));
// cout<<"Case #"<<cas++<<": "<<get(y)-get(x-1)<<endl;
// printf("Case #%d: %I64d\n",cas++,get(y)-get(x-1));
}
return 0;
}