2380. Digits Count
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Diana is going to write a list of all positive integers between A and B, inclusive, in base 10 and without any leading zeros. She wants to know how many times each digit is going to be used.
Input
Each test case is given in a single line that contains two integers A and B (1AB108). The last test case is followed by a line containing two zeros.
Output
For each test case output a single line with 10 integers representing the number of times each digit is used when writing all integers between A and B, inclusive, in base 10 and without leading zeros. Write the counter for each digit in increasing order from 0 to 9.
Sample Input
1 9 12 321 5987 6123 12345678 12345679 0 0
Sample Output
0 1 1 1 1 1 1 1 1 1 61 169 163 83 61 61 61 61 61 61 134 58 28 24 23 36 147 24 27 47 0 2 2 2 2 2 2 2 1 1
Problem Source
每周一赛第二场
// Problem#: 2380
// Submission#: 3270428
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <math.h>
int allNum[] = {0, 1, 10, 190, 2890, 38890, 488890, 5888890, 68888890, 788888890, 8888888890};
void cal(int n, int times[]) {
int originalN = n;
for (int i = 0; i <= 9; i++) times[i] = 0;
if (n == 0) {
times[0] = 1;
return;
}
while (n > 9) {
int weishu = (int)log10(1.0 * n) + 1;
int zushu = n / (int)pow(10.0, 1.0 * (weishu - 1));
int zongshu = zushu * (int)pow(10.0, 1.0 * (weishu - 1)) * (weishu - 1);
for (int i = 1; i <= 9; i++) {
times[i] += zongshu / 10;
}
for (int i = 1; i < zushu; i++) {
times[i] += (int)pow(10.0, 1.0 * (weishu - 1));
}
times[zushu] += n % (int)pow(10.0, 1.0 * (weishu - 1)) + 1;
n %= (int)pow(10.0, 1.0 * (weishu - 1));
}
for (int i = 1; i <= n; i++) times[i] += 1;
int originalWeishu = (int)log10(1.0 * originalN) + 1;
int suoyoushu = allNum[originalWeishu];
suoyoushu += (originalN - (int)pow(10.0, 1.0 * (originalWeishu - 1)) + 1) * originalWeishu;
int oneToNineSum = 0;
for (int i = 1; i <= 9; i++) oneToNineSum += times[i];
times[0] = suoyoushu - oneToNineSum;
}
int main() {
while (1) {
int a, b;
scanf("%d%d", &a, &b);
if (a == 0 && b == 0) break;
int aa[15], bb[15];
cal(a - 1, aa);
cal(b, bb);
for (int i = 0; i <= 9; i++) {
if (i) printf(" ");
printf("%d", bb[i] - aa[i]);
}
printf("\n");
}
//getchar();
//getchar();
return 0;
}