The counting problemTime
limit:MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 2282Appoint Description:System Crawler (2015-04-15)
Description
Given 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 = 1024x768 and B = 1032, the list would be
1024 1025 1026 1027 1028 1029 1030 1031 1032
There is ten 0 ' in the list, ten 1 ' s, seven 2 ' s, three 3 ' s, and etc.
Input
The input consists of up to lines. Each line contains numbers a and b where 0 < A, b < 100000000. The input is terminated by a line ' 0 0 ', which are not considered as part of the input.
Output
For each pair of input, the output a line containing ten numbers separated by the single spaces. The first number is the number of occurrences of the digit 0, and the second is the number of the 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 1061 6 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 1 04 87 93 97 97 142 196398 1375 398 398 405 499 499 495 488 471294 1256 296 296 296 296 287 286 286 247
Test instructions
Count the number of 0-9 in each position from a to B.
Analysis:
From this problem more embodies the charm of mathematics ...
Either way, find an F (n), count from 1 to N, and then F (b)-F (A-1) results.
Recursion is the establishment of the F (k) and F (10*k+x) relationship, that is, for a large number, first processing to the end of 0, and then thought to be starting from 0 10 10 count to this number. For example, F (2984) starts from 2981 to 2984 (2,9,8 each 4 times, 1,2,3,4 once) and then F (2980) can be recursive with F (298) (a little better with F (297) in real time). Note that there is a need to create a "multiplier", starting at 1, each time a layer is pushed on the * 10, and then accumulated to 0-9 of the record.
The direct look is like this, because this number n has been determined, so in the first position, the number of digits J can be calculated, it is the first digit F and the number of the next digit T is determined (f is multiplied by 10 i-1, rather than I, because the equivalent of you to remove the first bit). There are several details to consider: The size comparison of the number J and N in the number of digits I, 0 (first number); The first and the last one are discussed separately (when the number is only one person).
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); #define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1int res_a[10];int res_b[10];void get_res (int s, int res[],int t=1)//{int i; int d,p; if (S <= 0) return; d=s%10; P=S/10; for (I=1; i<=d; i++) res[i]+=t; while (P > 0) {res[p% 10]+= (d+1) *t; P=P/10; } for (i=0; i<=9; i + +) res[i]+= (S/10) *t; t*=10; Get_res ((S/10) -1,res,t); return;} int main () {int a A, B; int i,j; while (~SCANF ("%d%d", &a,&b)) {if (!a&&!b) break; memset (res_a,0,sizeof (res_a)); memset (res_b,0,sizeof (res_b)); if (a>=b) swap (A, b); A--; Get_res (B,res_b); Get_res (a,res_a); for (i=0; i<9; i++) printf ("%d", res_b[i]-res_a[i]); printf ("%d\n", res_b[9]-res_a[9]); } return 0;}
POJ 2282-the counting problem (Combinatorial math _ interval count)