POJ 3286 How many 0's?(几多0?)

Time Limit: 1000MS   Memory Limit: 65536K

【Description】

【题目描述】

A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he write down?

一个本尼迪克16号僧侣写下m与n之间并且包括mn的所有十进制自然数,m ≤ n。这之间他写下了多少个0?

【Input】

【输入】

Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, m ≤ n. The last line of input has the value of m negative and this line should not be processed.

多组输入。每行有两个无符号32位整数m和n,m ≤ n。最后一行的m是负数,因此不该被执行。

【Output】

【输出】

For each line of input print one line of output with one integer number giving the number of 0's written down by the monk.

对于每行输入数据输出一行一个整数,表示这个僧侣写了多少个0.

【Sample Input - 输入样例】

【Sample Output - 输出样例】

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

-1 -1

1

22

92

987654304

3825876150

【题解】

如果不考虑数据范围,这道题应该就是入门级别的数位DP(此处可参考HDU 2089)

不过这道的数据范围是unsigned int,无符号减法可能有问题,所以还是直接上int64吧。

这道题在具体的思路上基本一致,因为数据范围大了,就需要压缩一下空间,不能任性。

之后的计算方式大概可以变为:

计算0~N写了几个0,即个位上0写的次数,十位上0…,百位上的0…以此类推。

每一位按照其出现0的区间间隔与区间长度,计算这一位上写了几次0。

百位

[1000, 1099]

[2000, 2099]

[3000, 3099]

[4000, 4099]

……

十位

[100, 109]

[200, 209]

[300, 309]

[400, 409]

……

个位

[0, 0]

[10, 10]

[20, 20]

[30, 30]

……

以100为例:

  个位:直接100/10 +1 = 11(懒)。

  十位:100拆分成1 00

    其中1表示进位的次数,完整区间数1-0=1,剩余区间的元素个数 0–0+1=1。

    所以十位数的次数=0*10+1 = 1。

  百位:到达最高位,结束。

  结果:11+1 = 12。

以200为例:

  个位:20 0,200/10 +1 = 21。

  十位:2 10,完整区间数2-1=1,剩余区间的元素个数0-0+1=10。

    剩余10>9,所以超过9的元素最多只能提供10个0

    十位数的次数=1*10+10 = 11。

  百位:结束。

  结果:21+11 = 32。

其他数字的结果以此类推,就能计算0~N写了几个0。

【代码 C++】

 #include<cstdio>
__int64 cmp[];
void rdy(){
int i;
for (cmp[] = i = ; i < ; ++i) cmp[i] = cmp[i - ] * ;
}
__int64 calculate(__int64 now){
if (now < ) return ;
int i = ;
__int64 right, left, opt = now / + ;
while (){
left = now / (cmp[i + ]);
right = now % (cmp[i + ]);
if (right < now){
opt += (left - )*(cmp[i]);
if (right >= cmp[i]) opt += cmp[i];
else opt += right + ;
++i;
}
else break;
}
return opt;
}
int main(){
rdy();
__int64 a, b;
while (scanf("%I64d%I64d", &a, &b)){
if (a < ) return ;
printf("%I64d\n", calculate(b) - calculate(a - ));
}
return ;
}

POJ 3286 How many 0's?(几多0?)的更多相关文章

  1. POJ 3286 How many 0's?

    题目链接 题意 :写下m到n之间所有的数,会写多少个0. 思路 :先算0到m的,再算0到n的,最后相减. 网上有位大神是这么推的,看下面.... 首先转化成求 [0, x] 中所有数中,含有的 0 的 ...

  2. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  3. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  4. 创建或打开解决方案时提示"DotNetCore.1.0.1-SDK.1.0.0.Preview2-003131-x86"错误的解决方案

    提示"DotNetCore.1.0.1-SDK.1.0.0.Preview2-003131-x86"错误的解决方案: 1.检查是否有C:\Program Files (x86)\d ...

  5. Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead的解决办法

    今天在导入工程进Eclipse的时候竟然出错了,控制台输出的是: [2013-02-04 22:17:13 - takepicture] Android requires compiler compl ...

  6. 字符串数组初始化0 与memset 0 效率的分析

    转自:http://www.xuebuyuan.com/1722207.html 结合http://blog.sina.com.cn/s/blog_59d470310100gov8.html来看. 最 ...

  7. bootstrap2.0与3.0的区别

    在阅读这篇bootstrap2.0与3.0的区别的文章之前,大家一定要先了解什么是响应式网站设计?推荐大家看看这篇"教你快速了解响应式网站设计" . 我觉得bootstrap的可视 ...

  8. centos7.2环境elasticsearch-5.0.1+kibana-5.0.1+zookeeper3.4.6+kafka_2.9.2-0.8.2.1部署详解

    centos7.2环境elasticsearch-5.0.1+kibana-5.0.1+zookeeper3.4.6+kafka_2.9.2-0.8.2.1部署详解 环境准备: 操作系统:centos ...

  9. ASP.Net MVC3安全升级导致程序集从3.0.0.0变为3.0.0.1

    开发环境一般引用的是本机 C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies下的System.Web.Mvc.dll,当 ...

随机推荐

  1. 为 Macbook 安装 wget 命令

    没想到装个这个命令那么麻烦, 还要装 xcode?..... 好吧,按步骤来成功装上 http://www.arefly.com/mac-wget/ 其實Mac也自帶了一個Curl同樣也可以下載網頁, ...

  2. armv8(aarch64)linux内核中flush_dcache_all函数详细分析【转】

    转自:http://blog.csdn.net/qianlong4526888/article/details/12062809 版权声明:本文为博主原创文章,未经博主允许不得转载. /* *  __ ...

  3. Oracle集合运算符 交集 并集 差集

     集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集  一.union求并集,公共部分只有包含一次 例:求emp表ename中含’A‘或含有‘M’ SQL&g ...

  4. Pascal's Triangle

    class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector< ...

  5. $IFS和set

    $IFS是内部字段分隔符的缩写.它决定Bash解析字符串时将怎样识别字段,或单词分界线.默认为(空格.制表符.换号) 修改$IFS: [xiluhua@vm-xiluhua][~/shell_scri ...

  6. HTML5 拖拽复制功能的实现方法

    Internet Explorer 9FirefoxOpera 12ChromeSafari 5 v1.0代码部分 <!DOCTYPE html><html><head& ...

  7. 简单选择排序(Java)

    简单选择排序: 每一趟在整个记录中找到最小的那个作为有序序列的第i个记录. class SelectSort{ public void p(int[] a){ for(int i=0;i<a.l ...

  8. LA 3907 Puzzle

    问题描述:先给你s个禁止串,求不包含禁止串的最长串,如果存在,打印字典序最大. 数据范围:s <= 1000, 禁止串长度不超过50. 分析:不匹配问题实际上等同于匹配问题.假设我们已经有满足条 ...

  9. 提高 Linux 上 socket 性能

      http://www.cnblogs.com/luxf/archive/2010/06/13/1757662.html 基于Linux的Socket网络编程的性能优化   1 引言    随着In ...

  10. 静态方法和类成员方法(Python)

    静态方法和成员方法分别在创建时分别被装入Staticmethod 类型和 Classmethod类型的对象中.静态方法的定义没有 self参数,且能够被类本身直接调用,类方法在定义时需要名为 cls的 ...