Poj2282: The counting problem (Digital DP)

Source: Internet
Author: User

Description

Given two integers A and B, we write the numbers between A and B, inclusive, in a list. your task is to calculate the number of occurrences of each digit. for example, if a = 1024 and B = 1032, the list will be
1024 1025 1026 1027 1028 1029 1030 1031

There are ten 0's in the list, ten 1's, seven 2's, three 3's, and etc.

Input

The input consists of up to 500 lines. each line contains two numbers a and B where 0 <a, B <100000000. the input is terminated by a line '0 0', which is not considered as part of the input.

Output

For each pair of input, output a line containing ten numbers separated by single spaces. the first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 1044 497346 5421199 17481496 14031004 5031714 1901317 8541976 4941001 19600 0

Sample output

1 2 1 1 1 1 1 1 1 185 185 185 185 190 96 96 96 95 9340 40 40 93 136 82 40 40 40 40115 666 215 215 214 205 205 154 105 10616 113 19 20 114 20 20 19 19 16107 105 100 101 101 197 200 200 200 200413 1133 503 503 503 502 502 417 402 412196 512 186 104 87 93 97 97 142 196398 1375 398 398 405 499 499 495 488 471294 1256 296 296 296 296 287 286 286 247
 
Question: 0 ~ Number of 9
Idea: DP [I] [J], which indicates that there are several J numbers with the length of I.
 
 
# Include <stdio. h> # include <string. h >#include <algorithm> using namespace STD ;__ int64 DP [10] [10] ;__ int64 POW (_ int64 A ,__ int64 B) {_ int64 ans = 1, I; for (I = 1; I <= B; I ++) ans * = A; return ans;} void set () {memset (DP, 0, sizeof (DP); For (_ int64 I = 1; I <= 9; I ++) {for (_ int64 J = 0; j <= 9; j ++) DP [I] [J] = DP [I-1] [J] * 10 + POW (10, I-1); }__ int64 solve (_ int64 n ,__ int64 POS) {_ int64 s [15], Len = 0, I, TEM; whil E (n) {s [++ Len] = n % 10; N/= 10;} s [0] = 0; _ int64 ans = 0, CNT = 0; for (I = Len; I> = 1; I --) {If (Pos = 0 & I = Len) // identify the case of 0 {ans + = DP [I-1] [POS] * (s [I]-1); TEM = POW (10, I-1)-1; ans + = solve (TEM, 0);} else // not 0, then the number of the following s [I] times to add ans + = DP [I-1] [POS] * s [I]; ans + = CNT * POW (10, I-1) * s [I]; // calculate the number of times if (POS) {If (s [I]> POS) that the preceding number is the same as the required number) ans + = POW (10, I-1); // s [I]> TMP, then s [I] = TMP, add the hedgehog POW (10, I-1) times if (s [I] = POS) CNT ++ ;} Else if (I! = Len) {If (s [I]> POS) ans + = POW (10, I-1); If (s [I] = POS) CNT ++ ;}} return ans + CNT;} int main () {set (); _ int64 L, R, I; while (~ Scanf ("% i64d % i64d", & L, & R), L + r) {If (L> r) Swap (L, R ); printf ("% i64d", solve (R, 0)-solve (L-1, 0); for (I = 1; I <= 9; I ++) printf ("% i64d", solve (R, I)-solve (L-1, I); printf ("\ n") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.