数位DP--HDU4722(Good Numbers)

来源:互联网 发布:jquery 数组转字符串 编辑:程序博客网 时间:2024/06/05 21:52

Good Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 901    Accepted Submission(s): 336


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.

 

/********************************************************************* Problem:4722--Good Number* source:HDU* 分析:记忆化搜索部分,pre表示前面各位数字之和对该数取模的结果* author:sgx* date:2013/09/15* PS:出现了十几次奇葩错误之后终于艰难AC。。。。*********************************************************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define LL __int64const int maxn=25;LL dp[maxn][12];LL digit[maxn];LL l,r;LL DFS(int pos,int pre,bool limit){    if(pos==-1)        return pre==0;    if(!limit&&dp[pos][pre]!=-1)        return dp[pos][pre];     LL res=0,end=limit?digit[pos]:9;     for(int i=0;i<=end;i++)     {        int new_pre=(pre+i)%10;         res+=DFS(pos-1,new_pre,limit&&i==end);     }     if(!limit)         dp[pos][pre]=res;     return res;}LL solve(LL n)//传参没传好,WA十几次;{     int len=0;     while(n)     {         digit[len++]=n%10;         n/=10;     }     return DFS(len-1,0,true);}int main(){     int test;     scanf("%d",&test);     for(int ii=1;ii<=test;ii++)     {         memset(dp,-1,sizeof(dp));         scanf("%I64d%I64d",&l,&r);         printf("Case #%d: %I64d\n",ii,solve(r)-solve(l-1));     }     return 0;}


 

原创粉丝点击