Good Numbers

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

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

题意:给定区间A到B(用 long long),求其间好数有多少,好数是指每位数字加起来的和对10取余结果是0。

当时不明白何为学长讲的边界,另外数位dp还有递推版,但是递推太容易出错,还是记忆化搜索比较容易理解~

  1. #include<iostream>
  2. #include<vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <math.h>
  7. #include<algorithm>
  8. #define ll long long
  9. #define eps 1e-8
  10. using namespace std;
  11. ll dp[][]; // 统计第几位数余数为几有多少个
  12. int num[]; //存放每位数字
  13. ll dfs(int site,int mod,int f)
  14. {
  15. if(site == )
  16. return mod == ? :; // 递归结束条件,并判断最后一位时是否余数为0
  17. if(!f && dp[site][mod] != -) //记忆化搜索
  18. return dp[site][mod];
  19. int len = f == ?num[site]:; //若当前位为边界則当前位只枚举0-当前,否则枚举0-9
  20. ll ans = ;
  21. for(int i = ; i <= len; i++)
  22. ans += dfs(site-,(mod+i)%,f && i == len);//f判断是否为边界
  23. if(!f)
  24. dp[site][mod] = ans; //边界不能存,因为之前计算的是不看边界的,便于以后用到,如果存了,就是一个错误的统计
  25. return ans;
  26. }
  27. ll solve(ll digit)
  28. {
  29. memset(num,,sizeof(num));
  30. int l = ;
  31. while(digit)
  32. {
  33. num[++l] = digit%;
  34. digit /= ;
  35. }
  36. return dfs(l,,);
  37. }
  38. int main(void)
  39. {
  40. int t,cnt = ;
  41. ll a,b;
  42. memset(dp,-,sizeof(dp));
  43. scanf("%d",&t);
  44. while(t--)
  45. {
  46. scanf("%lld %lld",&a,&b);
  47. printf("Case #%d: %lld\n",cnt++,solve(b)-solve(a-));
  48. }
  49. return ;
  50. }

hdu 4722 Good Numbers( 数位dp入门)的更多相关文章

  1. hdu 4722 Good Numbers 数位DP

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

  2. HDU 2089 - 不要62 - [数位DP][入门题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  3. Hdu 2089 不要62 (数位dp入门题目)

    题目链接: Hdu 2089 不要62 题目描述: 给一个区间 [L, R] ,问区间内不含有4和62的数字有多少个? 解题思路: 以前也做过这个题目,但是空间复杂度是n.如果数据范围太大就GG了.今 ...

  4. hdu 3555 Bomb(数位dp入门)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  5. 【数位DP】 HDU 4722 Good Numbers

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

  6. HDU 2089 不要62【数位DP入门题】

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  8. hdu3555 Bomb 数位DP入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...

  9. xbz分组题B 吉利数字 数位dp入门

    B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...

随机推荐

  1. PAT甲级——A1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  2. PKU--3628 Bookshelf 2(01背包)

    题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...

  3. 09.Hibernate中的事务与并发

    事务1. 什么是事务 * 事务就是逻辑上的一组操作,组成事务的各个执行单元,操作要么全都成功,要么全都失败. * 转账的例子:冠希给美美转钱,扣钱,加钱.两个操作组成了一个事情! 2. 事务的特性 * ...

  4. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  5. 写js过程中遇到的一个bug

    <div class="func_Div" id="xxcx"><span>信息查询</span>              ...

  6. Spring注解驱动开发(一)-----组件注册

    注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...

  7. MyBatis配置文件(七)--environments运行环境

    一.environments配置信息: environments的作用是用来配置数据库信息,可以配置多个,其有两个可配的子元素,分别是:事务管理器transactionManager和数据源dataS ...

  8. 左神算法书籍《程序员代码面试指南》——1_08构造数组的MaxTree

    [题目] 将一个没有重复数字的数组中的数据构造一个二叉树 每个节点都是该子树的最大值 [要求] 时间复杂度为O(N)[题解] 使用单调栈,栈的顺序是维持从大到小排序 通过使用单调栈,将数组中中所有数的 ...

  9. java文件配置MySQL

    MybatisConfig.java文件 import com.alibaba.druid.pool.DruidDataSource; import com.xman.common.mybatis.S ...

  10. Codec入门

    Codec 提供了一些公共的编解码实现,比如Base64, Hex, MD5等等. 工具类 package com.cxl.beanutil.util; import org.apache.commo ...