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.

InputThe 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 <= 10
18).OutputFor 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.

 这题有两种做法,找规律或者数位dp
法一找规律:从0开始打表会发现每10个数都有一个good number 0 - 9, 10- 19 …… 这样假如要求0 到123,只需要求 0 - 119的 有12个good numbers,再暴力求120 - 123的即可。
法二数位dp:这道题在数位dp中算是模板入门了吧。

法一代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 #include <cmath>
6 #include <vector>
7 #include <map>
8 #include <queue>
9 #include <set>
10 #include <cstring>
11 using namespace std;
12 typedef long long ll;
13 #define inf 0x3f3f3f3f
14 char st[12];
15 ll x;
16 int main()
17 {
18 int t;
19 ll a, b;
20 scanf("%d", &t);
21 ll cnta = 0, cntb = 0;
22 for(int cas = 1; cas <= t; ++cas) {
23 cnta = 0;
24 cntb = 0;
25 scanf("%lld %lld", &a, &b);
26 a--;
27 if(a < 0) cnta--;
28 a = max(a, ll(0));
29 ll ma = a % 10;
30 cnta += a / 10;
31 for(ll i = 0; i <= ma; ++i) {
32 ll sum = 0;
33 ll v = a / 10 * 10+ i;
34 while(v) {
35 sum += v % 10;
36 v /= 10;
37 }
38 if(sum % 10 == 0) cnta++;
39 }
40 ll mb = b % 10;
41 cntb += b / 10;
42 for(ll i = 0; i <= mb; ++i) {
43 ll sum = 0;
44 ll v = b / 10 * 10+ i;
45 while(v) {
46 sum += v % 10;
47 v /= 10;
48 }
49 if(sum % 10 == 0) cntb++;
50 }
51
52 printf("Case #%d: %lld\n", cas, cntb - cnta);
53 }
54 return 0;
55 }

法二代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 #include <cmath>
6 #include <vector>
7 #include <map>
8 #include <queue>
9 #include <set>
10 #include <cstring>
11 using namespace std;
12 typedef long long ll;
13 #define inf 0x3f3f3f3f
14 ll dp[22][188];
15 ll ed[22];
16 ll dfs(int pos, ll sum, bool lmt) {
17 if(pos == 0) {
18 if(sum % 10 == 0) return 1;
19 return 0;
20 }
21
22 if(!lmt && dp[pos][sum] != -1) return dp[pos][sum];
23 ll ans = 0;
24 ll up = lmt? ed[pos] : 9;
25 for(ll i = 0; i <= up; ++i) {
26 ans += dfs(pos - 1, sum + i, lmt && i == ed[pos]);
27 }
28 if(!lmt) dp[pos][sum] = ans;//统计状态
29 return ans;
30 }
31 ll solv(ll x) {
32 if(x < 0) return 0;
33 int len = 0;
34 while(x) {
35 ed[++len] = x % 10;
36 x /= 10;
37 }
38
39 return dfs(len, 0, 1);
40 }
41
42 int main()
43 {
44 int t;
45 ll a, b;
46 scanf("%d", &t);
47 memset(dp,-1,sizeof(dp));
48 for(int cas = 1; cas <= t; ++cas) {
49
50 scanf("%lld %lld", &a, &b);
51 printf("Case #%d: %lld\n", cas, solv(b) - solv(a - 1));
52 }
53 return 0;
54 }


HDU - 4722 Good Numbers 【找规律 or 数位dp模板】的更多相关文章

  1. hdu 4722 Good Numbers(规律题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4722 [题意]: 找GoodNumbers一个数N,如果它每一个位数字之和可以整除10,那么它就是GoodNum ...

  2. 【数位DP】 HDU 4722 Good Numbers

    原题直通车: HDU  4722  Good Numbers 题意: 求区间[a,b]中各位数和mod 10==0的个数. 代码: #include<iostream> #include& ...

  3. BZOJ_1662_[Usaco2006 Nov]Round Numbers 圆环数_数位DP

    BZOJ_1662_[Usaco2006 Nov]Round Numbers 圆环数_数位DP Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺 ...

  4. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

  5. POJ 3286 How many 0's(数位DP模板)

    题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num ...

  6. hdu 4722 Good Numbers( 数位dp入门)

    Good Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. hdu 4722 Good Numbers 数位DP

    数位DP!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include< ...

  8. HDU 4722:Good Numbers(数位DP)

    类型:数位DP 题意:定义一个Good Number 为 一个数所有位数相加的和%10==0.问[A,B]之间有多少Good Number. 方法: 正常“暴力”的定义状态:(i,d,相关量) 定义d ...

  9. HDU 4722 Good Numbers

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 Good Numbers Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. Jmeter二次开发——自定义函数

    在之前的博文中,Jmeter二次开发--基于Java请求,已介绍了Jmeter二次开发的基础情况,上次分享的是java请求开发,今天来分享下Jmeter中的函数开发.聊到Jmeter的函数,知道Jme ...

  2. 注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途

    注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途 介绍: 如果你想将在SpringBoot项目中的配置类进行排序,那么用到spring-boot-au ...

  3. ES入门及安装软件

    es介绍 Elasticsearch,简称es,是一款高扩展的分布式全文检索引擎.它可以近乎实时的存储,检索数据.es是面向文档型的数据库,一条数据就是一个文档,用json做为文档序列化的格式.es是 ...

  4. 京东零售mockRpc实践

    https://mp.weixin.qq.com/s/A0T6ySub0DfQiXJAbWm2Qg jsf协议是基于tcp的而且对数据进行了序列化.加密等操作,直接截获的方式很难实现.最后决定注入自己 ...

  5. Linux网络数据包的揭秘以及常见的调优方式总结

    https://mp.weixin.qq.com/s/boRWlx1R7TX0NLuI2sZBfQ 作为业务 SRE,我们所运维的业务,常常以 Linux+TCP/UDP daemon 的形式对外提供 ...

  6. 实用 nginx.conf 用法大全

    服务器拒绝非GET方式请求保障安全性,因为 DELETE.POST.PUT 是可以修改数据的. Nginx 解决方案 在 nginx.conf 配置文件的网站配置区域中添加如下代码片段: 非 GET ...

  7. C# 8.0 可空(Nullable)给ASP.NET Core带来的坑

    Nullable reference types(可为空引用类型) 可为空引用类型不讲武德 C#8.0 引入了"可为空引用类型"和"不可为空引用类型",使我们能 ...

  8. Zookeeper语法

    ZooKeeper 是一个典型的分布式数据一致性解决方案,分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知.集群管理.Master 选举.分布式 ...

  9. MySql(一)表类型(存储引擎)

    MySql(一)表类型(存储引擎) 一.MYSQL存储引擎概述 二.存储引擎的特性对比 2.1 MyISAM 2.2 InnoDB 2.2.1 自动增长列 2.2.2 外键约束 2.2.3 存储方式 ...

  10. SpringBoot整合JavaMail发送邮件

    JavaMail是SUN提供给广大Java开发人员的一款邮件发送和接受的一款开源类库,支持常用的邮件协议,如:SMTP.POP3.IMAP,开发人员使用JavaMail编写邮件程序时,不再需要考虑底层 ...