UVa 11038 - How Many O's? (组合数学 数位统计)
2014-10-19 17:56
429 查看
UVA - 11038
How Many O's?
Submit Status
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?
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 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.
Piotr Rudnicki
Source
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Counting :: Exercises:
Beginner
Root :: Prominent Problemsetters :: Piotr Rudnicki
Submit Status
题意:
将[L, R]之间的所有数写出来,需要写多少个0?
引入 f(x) 表示0..x的所有数中0的个数
则原问题变为 f(R) - f(L-1)
现在要计算f(x)
直接统计每一位为0的数
在网上看到的一篇题解,写的非常清楚非常详细 (传送门)
虽然是英文,但是很简单
Notation:
denotes an integer
.
That is
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
. Let's find for each position
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:
,
and
, we will have a positive integer
,
which is not greater than b, and the k-th digit of which exists and is equal to zero. There are
such
integers.
If bk = 0, same analysis as above applies, except that when
there
are only
ways to choose the digits to the right of ak.
So in this case there are
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".
How Many O's?
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 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
Submit Status
题意:
将[L, R]之间的所有数写出来,需要写多少个0?
引入 f(x) 表示0..x的所有数中0的个数
则原问题变为 f(R) - f(L-1)
现在要计算f(x)
直接统计每一位为0的数
在网上看到的一篇题解,写的非常清楚非常详细 (传送门)
虽然是英文,但是很简单
Explanation
Notation:denotes an integer
.
That is
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
. Let's find for each position
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:
,
and
, we will have a positive integer
,
which is not greater than b, and the k-th digit of which exists and is equal to zero. There are
such
integers.
If bk = 0, same analysis as above applies, except that when
there
are only
ways to choose the digits to the right of ak.
So in this case there are
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 = 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; }
相关文章推荐
- uva 11038 How Many O's? 组合计数
- UVA11038- How Many O's?(组合数学)
- UVA 11038 How Many O's? (数位统计 )
- UVa:11038 How Many O's?
- UVA - 11038 How Many O's? (计数)
- UVA11038- How Many O's?(组合数学)
- uva 11038 - How Many O's?
- UVa 10061 How many zero's and how many digits ? (任意进制下的阶乘长度和尾0的数目)
- uva 10061: How many zero's and how many digits?
- uva 10061 How many zero's and how many digits ?(在不同进制下分解因子)
- UVa11038 - How Many O's?(数位dp)
- UVa 10061 - How many zero's and how many digits ?
- uva 10061 - How many zero's and how many digits ?
- UVa 10213 How Many Pieces of Land ? (数学&欧拉公式&高精度)
- UVA 10061 How many zero's and how many digits ?(数论)
- UVA - 10061 How many zero's and how many digits ?
- uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401
- uva11038 How Many O's? 数位dp
- UVA 11038 - How Many O's? (数学题)
- UVa 10183 How Many Fibs? (统计斐波那契数个数&高精度)