Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

Input

Input starts with an integer T (≤ 11000), denoting the number of test cases.

Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

Output

For each case, print the case number and the number of zeroes written down by Jimmy.

Sample Input

Output for Sample Input

5

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

Case 1: 1

Case 2: 22

Case 3: 92

Case 4: 987654304

Case 5: 3825876150

题意:给你两个数m,n。问你在m~n之间0的个数。

这题类似lightoj1032,只不过这题是求0的个数。求0的个数一定要考虑是否是首位,我们知道0不能为第一位,所以这题在搜索上就加一个判断

条件,看一下搜索到的是否为首位。

#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
LL dp[33][33];
LL dig[33];
LL dfs(LL x , LL cnt , LL flag , LL first)
{
if(x == 0)
{
if(first)
return 1;
else
return cnt;
}
if(dp[x][cnt] != -1 && !flag && !first)
return dp[x][cnt];
LL sum = 0;
LL gg = flag ? dig[x] : 9;
for(int i = 0 ; i <= gg ; i++)
{
if(first)
{
sum += dfs(x - 1 , cnt , i == gg && flag , first && i == 0);
}
else
{
if(i == 0)
{
sum += dfs(x - 1 , cnt + 1 , i == gg && flag , 0);
}
else
{
sum += dfs(x - 1 , cnt , i == gg && flag , 0);
}
}
}
if(!flag && !first)
dp[x][cnt] = sum;
return sum;
}
LL Gets(LL x)
{
memset(dig , 0 , sizeof(dig));
int temp = 0;
if(x == 0)
dig[1] = 0;
while(x)
{
dig[++temp] = x % 10;
x /= 10;
}
return dfs(temp , 0 , 1 , 1);
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--)
{
ans++;
LL n , m;
memset(dp , -1 , sizeof(dp));
cin >> n >> m;
//cout << Gets(m) << ' ' << Gets(n - 1) << endl;
cout << "Case " << ans << ": " << Gets(m) - Gets(n - 1) << endl;
}
return 0;
}

lightoj 1140 - How Many Zeroes?(数位dp)的更多相关文章

  1. LightOJ 1140 How Many Zeroes? (数位DP)

    题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...

  2. light oj 1140 - How Many Zeroes? 数位DP

    思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  3. UVALive - 6575 Odd and Even Zeroes 数位dp+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...

  4. LightOJ 1032 - Fast Bit Calculations 数位DP

    http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...

  5. lightoj 1021 - Painful Bases(数位dp+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...

  6. LightOJ 1140 How Many Zeroes

    题意:写出一个给定区间的每个数,求出一共写了多少个零. 解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的 ...

  7. LightOJ - 1140 统计0的数位 数位DP

    注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

  8. 数位dp(D - How Many Zeroes? LightOJ - 1140 )

    题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...

  9. LightOJ 1140: How Many Zeroes? (数位DP)

    当前数位DP还不理解的点: 1:出口用i==0的方式 2:如何省略状态d(就是枚举下一个数的那个状态.当然枚举还是要的,怎么把空间省了) 总结: 1:此类DP,考虑转移的时候,应当同时考虑查询时候的情 ...

随机推荐

  1. Chrome 跨域 disable-web-security 关闭安全策略

    谷歌浏览器暂时关闭跨域. 当遇到以下情况,则可以简单的使用 关闭Chrome 安全策略跨域 开发时跨域,上线后,部署在一个域名下没有跨域问题 开发时,临时解决跨域问题 只有开发时用这个,其他时候,就不 ...

  2. 关于定时器Scheduled(cron)的问题

    定时器配置步骤参考:http://blog.csdn.NET/sd4000784/article/details/7745947 下面给出cron参数中各个参数的含义: CRON表达式    含义 & ...

  3. 100天搞定机器学习|Day13-14 SVM的实现

    昨天我们学习了支持向量机基本概念,重申数学推导原理的重要性并向大家介绍了一篇非常不错的文章.今天,我们使用Scikit-Learn中的SVC分类器实现SVM.我们将在day16使用kernel-tri ...

  4. UE4 打包详细流程

    这两天试着把之前做的一个UE4项目在安卓机上运行下,于是乎有了下面的一个打包血泪史. 首先呢,肯定是下载好了UE的源码了,我用的是4.18. 安装步骤可以先参考下官方的教程http://api.unr ...

  5. VSTO之PowerPoint(PPT)插件开发常用API汇总

    VSTO简介 VSTO(Visual Studio Tools for Office )是VBA的替代,使得开发Office应用程序更加简单,并且用VSTO来开发office应用程序可以使用Visua ...

  6. MemCached的telnet命令行参数

    1.启动Memcache 常用参数 -p <num>      设置TCP端口号(默认不设置为: 11211) -U <num>      UDP监听端口(默认: 11211, ...

  7. SpringBoot开发案例之打造十万博文Web篇

    前言 通过 Python 爬取十万博文之后,最重要的是要让互联网用户访问到,那么如何做呢? 选型 从后台框架.前端模板.数据库连接池.缓存.代理服务.限流等组件多个维度选型. 后台框架 SpringB ...

  8. 【Java笔记】【Java核心技术卷1】chapter3 D1JavaStandard

    package chapter3;/*有包名,命令行编译javac -d . 名字.java(注意空格)运行时用java chapter3.JavaStandard*/ public/*访问修饰符*/ ...

  9. Ubuntu 16.04 硬盘安装

    自己的宏碁4741G 笔记本,已经用了6年了,最近感觉越来越慢,晚上突然想起装个Linux 玩玩,说干就干,选择了用户比较多的Ubuntu,网上下载16.04的版本,结合网上搜索到的安装教程,看似简单 ...

  10. Go包管理工具dep

    dep是一个golang依赖管理工具,需要在Go 1.7及更高的版本中使用. 1. 安装 安装dep工具的方式有很多种,如果是mac电脑的话,只需要如下命令: brew install dep 对于L ...