POJ 3286- How many 0's?(组合数学_区间计数)

How many 0's?
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

Sample Output

1
22
92
987654304
3825876150

题意:求区间内出现多少个零。和POJ 2282差不多。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
LL b[12]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000};
LL get_res(LL n)
{
    int i;
    LL left,m;
    LL cnt=0;
    for(i=1;i<12;i++){
            left=n/b[i]-1;
        cnt+=left*b[i-1];
        m=(n%b[i]-n%b[i-1])/b[i-1];
        if(m>0)
            cnt+=b[i-1];
        else if(m==0)
            cnt+=n%b[i-1]+1;
        if(n<b[i])
            break;
    }
    return cnt;
}

int main()
{
    LL n,m;
    while(~scanf("%lld %lld",&n,&m)){
        if(n==-1&&m==-1) break;
        if(n>m) swap(n,m);
        printf("%lld\n",get_res(m)-get_res(n-1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值