UVa11038 How Many O's? (计数)
来源:互联网 发布:淘宝红号查询 编辑:程序博客网 时间:2024/04/25 21:07
Description
Problem E: How many 0's?
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?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.
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.
Sample input
10 11100 2000 5001234567890 23456789010 4294967295-1 -1
Output for sample input
122929876543043825876150题意: 求[n, m]之间包含0的数字的个数思路:先转化为前缀问题,就是cal(m)-cal(n-1),枚举每一位,如果第i位是0的话,那么为了不让数大于原本的数的话,那么它的左边取[1, a-1]的时候,右边可以取任意值如果取a的话,那么只能取0到右边的最大;如果是非0的话,那么让这位为0的话,就可以左边随便取,右边随便取
#include <iostream>#include <cstdio>#include <cstring>typedef long long LL;using namespace std;LL n,m;LL cal(LL x){ if(x<0) return 0; LL ans=1,cnt=1,tmp=0; while(x>=10) { LL cur=x%10; x/=10; if(cur) ans+=x*cnt; else ans+=(x-1)*cnt+tmp+1; tmp+=cur*cnt; cnt*=10; } return ans;}int main(){ while(~scanf("%lld%lld",&m,&n)) { if(n==-1&&m==-1) break; printf("%lld\n",cal(n)-cal(m-1)); } return 0;}
1 0
- UVa11038 How Many O's? (计数)
- UVa11038 How Many O's?
- UVA11038- How Many O's?(组合数学)
- uva11038 How Many O's? 数位dp
- UVa11038 - How Many O's?(统计0的个数)
- Uva 11038 - How Many O's? 解题报告(计数)
- UVA 11038 - How Many O's?(计数问题)
- UVA - 11038 How Many O's? (计数)
- uva 11038 How Many O's? 组合计数
- uva 11038 - How Many O's?(计数问题)
- UVa 11038 How Many 0's?(数学计数)
- UVa-11038 How Many O's? (整数区间分解)
- Uva 11038 - How Many O's?
- uva 11038 How Many O's?
- UVaOJ 11038 How Many O's?
- UVA 11038 - How Many O's? (数学题)
- uva 11038 - How Many O's?
- UVa:11038 How Many O's?
- 在Word中选择多个自选图形对象
- NYOJ--40--公约数和公倍数
- JAVA的整型与字符串相互转换
- 浅析Java中的反射机制原理
- hdu 1829
- UVa11038 How Many O's? (计数)
- 同步与异步的初步理解
- 让windows8 变成无线路由器的方法
- Leetcode51 N-Queens
- Sed简介
- 【Java多线程】之十:Timer and TimerTask
- Swift调用C代码的方法
- 华为上机 循环报数 java
- 零基础学python-1.4 hello world