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整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...
随机推荐
- Linux查看温度
step 1: centos $ sudo yum install lm_sensors ubuntu $ sudo apt-get install lm_sensors step2$ sudo se ...
- 《DSP using MATLAB》Problem 8.26
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- ssm项目 maven 项目pon.xml 配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 2014-VGG-《Very deep convolutional networks for large-scale image recognition》翻译
2014-VGG-<Very deep convolutional networks for large-scale image recognition>翻译 原文:http://xues ...
- 渗透神器----BurpSuite_pro_v2.1
burp2.1版本 主要有两个文件 大小有270M 目前是最新的版本 下载地址 链接: https://pan.baidu.com/s/1UjdwBN-S2TbgZ8iKiBcEhw 提取码: 9h7 ...
- oracle创建新的连接(表空间?数据库?)
一.创建用户名密码 create user username identified by password --username 是用户名:password 是密码 二.给用户附权.撤权 gra ...
- oracle数据库忘记sys密码如何改密码
ORACLE服务器操作: 1.win+R打开dos窗口cmd 2.输入 sqlplus/nolog出现 3.输入 conn / as sysdba 出现 4. alter user sys ident ...
- 一些hbase的shell查询语句
华为bids(不想吐槽)种种原因只能用hbase shell查询,在此记录下自己探索的hbase shell 免得下次要用还得去找 scan 'ogg_sel_ioc_sv_product_name_ ...
- TZOJ 4651 zry和他的的灯泡们(lca)
描述 zry有一个收集灯泡的习惯,他把灯泡的阴极都共地,阳极连成一颗树,这样的话,他只要在任意一个灯泡的阳极加上合适的电压,所有灯泡都能亮起来.不幸的是,有一对灯泡之间的阳极连线断掉了,这样的话,这颗 ...
- CSS中position和header和overflow和background
<!DOCTYPE html> <!--CSS中position属性--> <html lang="en"> <head> < ...