For example, count the number of 2 in 4123.
By-bit statistics, first calculate everyone. There are 413 cases where the number of single places is 2, because you can have 0 ~ 412, but no number on the right ,,,
Then there are ten, ten, and ten are two types: 41*10 + 1 *. When the left side is from 0 ~ 40 o'clock, and the right side can be from 0 ~ 9, and when the left side is 41, the right side can only be from 0 ~ 3
Then there are hundreds of digits, and hundreds of digits in four or 100 categories, that is, from 0 ~ 3, from 0 ~ 99
A thousand bits have 1*1000, and there are no numbers on the left, and 0 to the right ~ 999 ,,,,
The above is the calculation 1 ~ 9, is special when 0 is calculated, because, except for the number 0, and 0 cannot start ,,,
We can see that ~ When the number is 9, it is divided into two parts to multiply, so that the processing of 0 is very simple, just multiply the left half part-1 ,,,,
Poj 3286: Enter M, N, and calculate the number of occurrences of 0 in all numbers between [M, N.
# Include <cstdio> # include <algorithm> using namespace STD; # define lint _ int64lint B [12] = {1, 10,100,100 0, 10000,100 000, 100000000,100, 10000000000,100 limit 00000}; lint count (lint N) {lint left, M, sum = 0; For (INT I = 1; I <12; I ++) {left = N/B [I]-1; sum + = left * B [I-1]; M = (N % B [I]-N % B [I-1]) /B [I-1]; // obtain the specific number from the first to the highest I bit if (M> 0) sum + = B [I-1]; else if (M = 0) sum + = n % B [I-1] + 1; if (n <B [I]) break;} return sum;} int main () {lint M, N; while (scanf ("% i64d % i64d", & M, & N) & (n> = 0 )) printf ("% i64d \ n", count (N)-count m-1); Return 0 ;}
Poj 2282: Enter M, N, and calculate the number of occurrences of all numbers between [M, N.
# Include <cstdio> # include <algorithm> using namespace STD; # define lint _ int64int B [9] = {1, 10,100,100 0, 10000,100 000, 1000000,100 00000, 100000000 }; lint count (int n, int ID) {lint left, M, sum = 0; For (INT I = 1; I <9; I ++) {left = N/B [I]-(ID = 0); sum + = left * B [I-1]; M = (N % B [I]-N % B [I-1])/B [I-1]; // obtain the specific number from the second to the second I bit if (M> ID) sum + = B [I-1]; else if (M = ID) sum + = n % B [I-1] + 1; if (n <B [I]) break;} return sum;} int main () {int X, Y; while (scanf ("% d", & X, & Y) & (X | y) {If (x> Y) Swap (x, y ); for (INT I = 0; I <= 9; I ++) printf ("% i64d", count (Y, I)-count (x-1, I )); printf ("\ n");} return 0 ;}