HDU 4722 数位dp

Posted 半根毛线code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4722 数位dp相关的知识,希望对你有一定的参考价值。

Good Numbers

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


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.
 

 

Source
 题意:对一个数字 每一位求和%10==0 问一个区间内有多少个这类数字
 题解:模板题
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define dazhi 2147483647
12 #define bug() printf("!!!!!!!")
13 #define M 200005
14 using namespace  std;
15 int t;
16 ll l,r;
17 ll bit[20];
18 ll dp[100][15];
19 ll functi(int pos ,int flag,int m)
20 {
21     if(pos==0) return (m==0);
22     if(flag&&dp[pos][m]!=-1) return dp[pos][m];
23     ll x=flag?9:bit[pos];
24     ll ans=0;
25     for(ll i=0;i<=x;i++)
26         ans+=functi(pos-1,flag||i<x,(m+i)%10);
27     if(flag) dp[pos][m]=ans;
28     return ans;
29 }
30 ll fun(ll num)
31 {
32     int len=0;
33     memset(bit,0,sizeof(bit));
34     while(num)
35     {
36         bit[++len]=num%10;
37         num/=10;
38     }
39     return functi(len,0,0);
40 }
41 int main()
42 {
43     while(scanf("%d",&t)!=EOF)
44     {
45         memset(dp,-1,sizeof(dp));
46          for(int i=1;i<=t;i++)
47          {
48              scanf("%I64d %I64d",&l,&r);
49              printf("Case #%d: %I64d\n",i,fun(r)-fun(l-1));
50          }
51     }
52     return 0;
53 }

 

以上是关于HDU 4722 数位dp的主要内容,如果未能解决你的问题,请参考以下文章

解题报告 (十四) 数位DP

简单数位DP

HDU 2089 数位dp入门

HDU 2089 不要62(数位dp)

hdu 2089 不要62 数位DP入门

HDU 3555Bomb 数位DP