POJ 3286 How many 0 ' s? (0? )
Time limit:1000ms Memory limit:65536k
"Description" |
"Title 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 ' would he write down? |
A Benedict 16th Monk writes down the M and N and includes all decimal natural numbers of MN, m≤n. How many 0 did he write down between the two? |
"Input" |
Input |
Input consists of a sequence of lines. Each line contains the unsigned 32-bit integers m and N, M≤n. The last line of input have the value of M negative and this line should is processed. |
multiple sets of inputs. Each line has two unsigned 32-bit integers m and n,m≤n. The last line of M is a negative number and therefore should not be executed. |
"Output" |
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. |
An integer for each line of input data output, indicating how many 0 the monk wrote. |
"Sample Input-Enter sample" |
"Sample output-Output sample" |
10 11 100 200 0 500 1234567890 2345678901 0 4294967295 -1-1 |
1 22 92 987654304 3825876150 |
Exercises
If the data range is not considered, this question should be the entry-level digital DP (refer to HDU 2089 here)
However, the data range is unsigned int, unsigned subtraction may be problematic, so go straight to the int64.
This problem in the specific way of thinking is basically consistent, because the data range is large, you need to compress space, not wayward.
After that, the calculation will probably change to:
Calculation 0~n wrote a few 0, that is, the number of bits on the 0 write, 10 bits on 0 ..., hundreds of 0 ... And so on
Each of them is based on the interval and interval length of 0, and the calculation of this one is written several times 0.
Hundred |
[1000, 1099] |
[2000, 2099] |
[3000, 3099] |
[4000, 4099] |
...... |
Ten |
[100, 109] |
[200, 209] |
[300, 309] |
[400, 409] |
...... |
Bit |
[0, 0] |
[10, 10] |
[20, 20] |
[30, 30] |
...... |
Take 100 as an example:
Digit: direct 100/10 +1 = 11 (lazy).
10 bits: 100 split into 1 00
Where 1 indicates the number of carry, the complete interval number 1-0=1, the number of elements remaining in the interval 0–0+1=1.
So the 10-digit number of times =0*10+1 = 1.
Hundred: Reach the highest level, end.
Result: 11+1 = 12.
Take 200 as an example:
Digit: 20 0,200/10 +1 = 21.
10-bit: 2 10, complete interval number 2-1=1, the number of elements in the remaining interval 0-0+1=10.
The remaining 10>9, so more than 9 of the elements can only provide 10 x 0
10-digit number of times =1*10+10 = 11.
Hundred: End.
Result: 21+11 = 32.
The results of other numbers, and so on, can be calculated 0~n write a few 0.
"Code C + +"
1#include <cstdio>2__int64 cmp[ A];3 voidRdy () {4 inti;5 for(cmp[0] = i =1; I < A; ++i) Cmp[i] = cmp[i-1] *Ten;6 }7 __int64 Calculate (__int64 now) {8 if(Now <0)return 0;9 inti =1;Ten__int64 right, left, opt = now/Ten+1; One while(1){ Aleft = Now/(Cmp[i +1]); -right = now% (Cmp[i +1]); - if(Right <Now ) { theopt + = (left-1)*(Cmp[i]); - if(right >= cmp[i]) opt + +Cmp[i]; - ElseOpt + + right +1; -++i; + } - Else Break; + } A returnopt; at } - intMain () { - Rdy (); - __int64 A, b; - while(SCANF ("%i64d%i64d", &a, &b)) { - if(A <0)return 0; inprintf"%i64d\n", calculate (b)-Calculate (A-1)); - } to return 0; +}
POJ 3286 How many 0 ' s? (0? )