POJ 3286How many 0's?

来源:互联网 发布:dnf优化防卡补丁 编辑:程序博客网 时间:2024/06/05 08:15
点击打开链接

How many 0's?
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2542 Accepted: 1321

Description

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

Input

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

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.

Sample Input

10 11100 2000 5001234567890 23456789010 4294967295-1 -1

Sample Output

122929876543043825876150

Source

Waterloo Local Contest, 2006.5.27

比如算4123中有多少个2

 

按位统计,,,先算各位,,个位是2的情况有413种,,,因为各位左边可以0~412,,,而右边没有数字,,,

然后是十位,,,十位是2的有41*10 + 1*4种,,当左边从0~40时,,,右边可以从0~9,,,而左边为41时,,右边只能从0~3

然后是百位,,,,百位有4*100种,,,,即左边从0~3,,右边从0~99

千位有  1*1000,,,左边没有数字,,,右边0~999,,,,

上面是计算1~9,,,,计算0的时候比较特殊,,,,原因是除了0这一个数字之外,,,,0不能做开头,,,

可以看到在求1~9的个数的时候,,,都是分为2部分相乘,,,这样0的处理也很简单,,只需把相乘的左半部分-1,,,,



#include<stdio.h>long long count[12]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000};long long find(long long x){    long long left,sum=0,w;    for(int a=1;a<12;a++)    {        left=x/count[a]-1;        sum+=left*count[a-1];        w=(x%count[a]-x%count[a-1])/count[a-1];        if(w>0)sum+=count[a-1];        else if(w==0)sum+=x%count[a-1]+1;        if(x<count[a])break;    }    return sum;}int main(){    long long m,n;    while(scanf("%lld%lld",&m,&n)&&n>=0)    {        printf("%lld\n",find(n)-find(m-1));    }    return 0;}


原创粉丝点击