POJ 3286 How many 0's?(几多0?)
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?)的更多相关文章
- POJ 3286 How many 0's?
题目链接 题意 :写下m到n之间所有的数,会写多少个0. 思路 :先算0到m的,再算0到n的,最后相减. 网上有位大神是这么推的,看下面.... 首先转化成求 [0, x] 中所有数中,含有的 0 的 ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- 创建或打开解决方案时提示"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 ...
- 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 ...
- 字符串数组初始化0 与memset 0 效率的分析
转自:http://www.xuebuyuan.com/1722207.html 结合http://blog.sina.com.cn/s/blog_59d470310100gov8.html来看. 最 ...
- bootstrap2.0与3.0的区别
在阅读这篇bootstrap2.0与3.0的区别的文章之前,大家一定要先了解什么是响应式网站设计?推荐大家看看这篇"教你快速了解响应式网站设计" . 我觉得bootstrap的可视 ...
- 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 ...
- 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,当 ...
随机推荐
- JS和CSS的多浏览器兼容(2)
2.Css的浏览器兼容性 方法一,根据不同的浏览器加载不同的css file <!DOCTYPE html> <html> <head> <title> ...
- JavaEE基础(五)
1.Java语言基础(数组概述和定义格式说明) A:为什么要有数组(容器) 为了存储同种数据类型的多个值 B:数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基 ...
- HDU 3652:B-number(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:求数位含有13和可以被13整除的数字个数. 思路:记录3种状态: st == 0 表示 从最高位到第 ...
- 微信公众平台消息接口API指南
简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器会使 ...
- 动态规划之LCS(最大公共子序列)
#include <stdio.h> #include <string.h> int b[50][50]; int c[50][50]; int length = 0; voi ...
- Ajax案例:三级联动查询员工的信息(三张表进行内连接)
需求分析: 通过数据库连接池,可以查询到数据库中员工的各种信息,通过点击下拉框的方式实现三级联动,即:没有点击city下拉框,后面两个下拉框中没有值,这样,点击city下拉框,department下拉 ...
- 20150601_Andriod 打开新窗体
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="htt ...
- Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20619 Accepted: 8083 De ...
- The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20304 ...
- 【20160924】GOCVHelper MFC增强算法(1)
//递归读取目录下全部文件(flag为r的时候递归) void getFiles(string path, vector<string>& files,string ...