UVa 11038 - How Many O's? (组合数学 数位统计)

UVA - 11038

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

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  nm ≤  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 nm ≤ n. The last line of input has the value of mnegative 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 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

Output for sample input

1
22
92
987654304
3825876150

Piotr Rudnicki

Source

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Counting ::  Exercises: Beginner

Root :: Prominent Problemsetters ::  Piotr Rudnicki

 Status



题意:

将[L, R]之间的所有数写出来,需要写多少个0?


引入 f(x) 表示0..x的所有数中0的个数

则原问题变为 f(R) - f(L-1) 

现在要计算f(x)

直接统计每一位为0的数

在网上看到的一篇题解,写的非常清楚非常详细 (传送门

虽然是英文,但是很简单


Explanation

Notation: \overline{a_n a_{n-1} \dots a_0} denotes an integer a = a_n 10^n + a_{n-1} 10^{n-1} + \cdots + a_0. That is a_n,\ a_{n-1},\ \dots,\ a_0 are the decimal digits of a, from left to right.

Let's solve an easier problem. How many 0's are there in numbers between 0 and b inclusive? If we denote this number by f(b) then the answer to our original problem is just f(n) - f(m - 1).

Let b = \overline{b_n b_{n-1} \dots b_0}. Let's find for each position 0 \le k < n how many times a zero appears there as we are counting from 0 to b.

If bk > 0, then by setting ak = 0, and choosing the other digits according to the constraints: 1 \le \overline{a_n \dots a_{k+1}} \le \overline{b_n \dots b_{k+1}}, and 0 \le \overline{a_{k-1} \dots a_0} < 10^k, we will have a positive integer a = \overline{a_n \dots a_0}, which is not greater than b, and the k-th digit of which exists and is equal to zero. There are 10^k \cdot \overline{b_n \dots b_{k+1}} such integers.

If bk = 0, same analysis as above applies, except that when \overline{a_n \dots a_{k+1}} = \overline{b_n \dots b_{k+1}} there are only 1 + \overline{b_{k-1} \dots b_0} ways to choose the digits to the right of ak. So in this case there are (10^k) \cdot (\overline{b_n \dots b_{k+1}} - 1)      + 1 + \overline{b_{k-1} \dots b_0} integers between 0 and b, in which the k-th digit is zero.

The total number of zeroes is the sum of the number of times a zero occurs in each position, plus 1 for the integer "0".





#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

char str[20];
LL POW10[20];
LL suf[40];

LL f(LL x) {
    if(x < 0) return 0;
    if(x == 0) return 1;
    sprintf(str, "%lld", x);
    int n = strlen(str);
    suf[n] = 0;
    for(int i=n-1; i>=0; i--)
        suf[i] = suf[i+1] + (str[i] - '0') * POW10[n-1-i];
    LL ans = 1;
    LL cur = str[0] - '0';
    for(int i=1; i<n; i++) {
        if(str[i] == '0') {
            ans += (cur-1) * POW10[n-i-1] + suf[i+1] + 1;
        } else if(str[i] >= '0') {
            ans += cur * POW10[n-i-1];
        }
        cur = cur * 10 + str[i] - '0';
    }
    return ans;
}

int main() {
    LL L, R;

    POW10[0] = 1;
    for(int i=1; i<20; i++) POW10[i] = POW10[i-1] * 10;
    while(scanf("%lld%lld", &L, &R) != EOF && !(L==-1 && R==-1)) {
        LL ans = f(R) - f(L-1);
        printf("%lld\n", ans);
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值