hdu 4722 Good Numbers( 数位dp入门)
Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3467 Accepted Submission(s): 1099Problem DescriptionIf 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 <= 1018).OutputFor test case X, output "Case #X: " first, then output the number of good numbers in a single line.Sample Input21 101 20Sample OutputCase #1: 0
Case #2: 1HintThe answer maybe very large, we recommend you to use long long instead of int.
Source
题意:给定区间A到B(用 long long),求其间好数有多少,好数是指每位数字加起来的和对10取余结果是0。
当时不明白何为学长讲的边界,另外数位dp还有递推版,但是递推太容易出错,还是记忆化搜索比较容易理解~
#include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std;
ll dp[][]; // 统计第几位数余数为几有多少个
int num[]; //存放每位数字
ll dfs(int site,int mod,int f)
{
if(site == )
return mod == ? :; // 递归结束条件,并判断最后一位时是否余数为0
if(!f && dp[site][mod] != -) //记忆化搜索
return dp[site][mod];
int len = f == ?num[site]:; //若当前位为边界則当前位只枚举0-当前,否则枚举0-9
ll ans = ;
for(int i = ; i <= len; i++)
ans += dfs(site-,(mod+i)%,f && i == len);//f判断是否为边界
if(!f)
dp[site][mod] = ans; //边界不能存,因为之前计算的是不看边界的,便于以后用到,如果存了,就是一个错误的统计
return ans;
}
ll solve(ll digit)
{
memset(num,,sizeof(num));
int l = ;
while(digit)
{
num[++l] = digit%;
digit /= ;
}
return dfs(l,,);
}
int main(void)
{
int t,cnt = ;
ll a,b;
memset(dp,-,sizeof(dp));
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld",&a,&b);
printf("Case #%d: %lld\n",cnt++,solve(b)-solve(a-));
}
return ;
}
hdu 4722 Good Numbers( 数位dp入门)的更多相关文章
- hdu 4722 Good Numbers 数位DP
数位DP!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include< ...
- HDU 2089 - 不要62 - [数位DP][入门题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- Hdu 2089 不要62 (数位dp入门题目)
题目链接: Hdu 2089 不要62 题目描述: 给一个区间 [L, R] ,问区间内不含有4和62的数字有多少个? 解题思路: 以前也做过这个题目,但是空间复杂度是n.如果数据范围太大就GG了.今 ...
- hdu 3555 Bomb(数位dp入门)
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- 【数位DP】 HDU 4722 Good Numbers
原题直通车: HDU 4722 Good Numbers 题意: 求区间[a,b]中各位数和mod 10==0的个数. 代码: #include<iostream> #include& ...
- HDU 2089 不要62【数位DP入门题】
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 数位DP入门
HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...
- hdu3555 Bomb 数位DP入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...
- xbz分组题B 吉利数字 数位dp入门
B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...
随机推荐
- PAT甲级——A1105 Spiral Matrix【25】
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- sip会话流程以及sip介绍(1)
参考连接 :https://www.2cto.com/kf/201609/546336.html https://www.w3cschool.cn/session_initiation_protoco ...
- Python学习day14-函数进阶(2)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- MathType插件安装
1 安装包下载 版本号:7.4 下载 提取码:fxma 2 安装方法 用安装包内的Key激活即可.软件激活后不能升级. 注意:必须断网或者加入防火墙阻止联网使用! 3 可能遇到的问题 当安装完Math ...
- [Ceoi2007]Royaltreasury
#1945. [Ceoi2007]Royaltreasury Online Judge:Bzoj-1945 Label:树形Dp,高精度 题目描述 在很久很久以前的一个王国里,王国的财产开始变得越来越 ...
- Windows API 第20篇 GetVolumeNameForVolumeMountPoint
函数原型: BOOL GetVolumeNameForVolumeMountPoint( ...
- Django项目:CRM(客户关系管理系统)--68--58PerfectCRM实现king_admin批量生成上课记录
# kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...
- MYSQL数据库练习题操作(select)大全
1.创建表 表一:student学生use) .create table student( sno ) primary key not null comment'学号(主码)', sname ) no ...
- PAT甲级——A1048 Find Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 有趣的纯CSS实现动态晴阴雨雪
我们先来看看实现的效果吧 非常的美腻,对吧.这个是纯css,且单标签实现的哦~ 先贴完整代码,我们再来看看这个里面究竟有什么可以借鉴的知识点 <!DOCTYPE html> <htm ...