# Digital statistics DP (338. Counting problem)

Given two integers ∼ a ∼ and ∼ b, find the number of occurrences of 0 ∼ 9 in all numbers between ∼ a ∼ and ∼ b.

For example, if a=1024 and b=1032, the total number of ， 9 ， between ， a ， and ， b ， is as follows:

1024 1025 1026 1027 1028 1029 1030 1031 1032

Among them, 0 ， 10 ， 1 ， 10 ， 2 ， 7 ， 3 ， 3 ， and so on

Input format

The input contains multiple sets of test data.

Each group of test data occupies one line, including two integers ＾ a ＾ and ＾ bb.

When a line ， 0 ， is read in, it indicates that the input is terminated and the line is not processed.

Output format

Each group of data outputs a result, and each result occupies one row.

Each result contains ten numbers separated by spaces. The first number represents the number of occurrences of "0", the second number represents the number of occurrences of "1", and so on.

Data range

0 < a,b < 100000000

Input sample:

```1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0
```

Output example:

```1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247```

Train of thought: y general remark: it's difficult to write math Olympiad in primary school,,,

Introduction: count(n, x) function: calculate the number of X between 1~n (the data range is greater than 0)

According to the idea of prefix sum, calculate the number of occurrences of each number before M (count(m, i)), and subtract the number of occurrences before n - 1 (count(n-1, i)), which is the number of occurrences before (n, m). The essence of this algorithm lies in the count function, Explanation: set n to seven digits abcdefg. Now judge the number of occurrences in each digit on 1~abcdefg,

Let this calculate the number of times 1 appears in the fourth bit, which is equivalent to calculating the number of numbers in the form of xxx1yyy in 1~abcdefg

1) First, when xxx = 000~abc - 1, yyy can take any number, that is, yyy = 0~999, a total of 1000 numbers

So the result is res += abc * 1000

2) When xxx = abc:

2.1 When d < 1, that is, the fourth digit of n is less than the number to be calculated, so xxx1yyy > ABCDEFG, res+= 0

2.2 when d == 1, that is, the fourth digit of n is equal to the number to be calculated. In this way, xxx1yyy is equivalent to abcdyyy. We only need to calculate the number of YYY within the efg range (000~efg), res += efg + 1 (one more 000, need + 1)

2.3 when d > 1, that is, the fourth digit of n is greater than the number to be calculated, xxx1yyy is equivalent to abc(d+1)yyy. No matter what value YYY takes (000 ~ 999), abc1yyy must exist, so res += 1000,

3) When XXX > ABC does not exist

Note: when d is 0, xxx = 001~abc-1 in case 1), so res -= 1000

After the above analysis, we can get the general framework

We need two functions,

Get function: get the value of part n (intercept part of the value of n), such as abc, efg, etc

power10 function: get the function to the power of 10. 10 ^ 3 is required in case 1) and 2.3

The framework is basically completed:

Pseudo code implements the following basic steps

i is the number of digits of n, the first a and the second b

1) Res + = get (number of previous i) * power10 (i - 1)

2)

2.1 res += 0

2.2 res + = get (after I - 1) + 1

2.3 res += power10( i )

Overall code implementation:

```#include <iostream>
#include <algorithm>

using namespace std;

int n, m;

int get(vector<int> num, int l, int r)
{
int res = 0;
for(int i = l; i >= r; i -- )
res = res * 10 + num[i];
return res;
}

int power10(int x)
{
int res = 1;
while(x -- ) res *= 10;
return res;
}

int count(int n, int x)
{
if(!n) return 0;

vector<int> num;

while(n)
{
num.push_back(n%10);
n /= 10;
}

n = num.size();

int res = 0;

for(int i = n - 1 - !x ; i >= 0; i -- )
{
if( i < n - 1)
{
res += get(num, n-1, i+1)*power10(i);
if(!x) res -= power10(i);
}

if(num[i] == x) res += get(num, i-1, 0) + 1;
else if(num[i] > x) res += power10(i);
}

return res;

}

int main()
{
while(cin >> n >> m, n || m)
{
if(n > m) swap(n, m);

for(int i = 0; i <= 9; i ++ )
cout << count(m, i) - count(n-1, i) << " ";
puts("");
}

return 0;
}```

Posted by artisticre on Fri, 20 May 2022 22:49:00 +0300