码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4722 数位dp

时间:2017-03-24 21:53:38      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:map   print   dash   space   rip   ack   php   for   class   

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

标签:map   print   dash   space   rip   ack   php   for   class   

原文地址:http://www.cnblogs.com/hsd-/p/6613500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!