[Statistics] number count

Source: Internet
Author: User
[Problem description] Given two positive integers A and B, calculate how many times each digit (DIGIT) appears in all integers in [a, B. [Input format] the count. In input file contains only one row of two integers, A and B. The meaning is described above. [Output format] the count. Out output file contains 10 integers in a row, indicating the number of times that 0-9 appears in [a, B. [Input sample] 1 99 [output sample] 9 20 20 20 20 20 20 20 20 20 20 [data scale] 30% of the data, a <= B <= 106; in 100% of the data, a <= B <= 1012.

The simple method does not need to be said anymore, that is, to enumerate the intervals and then accumulate them directly.

Of course, the simple algorithm obviously times out.

Through observation, we can find that for all I digits, numbers 1 to 9 appear as I · 10 ^ (I-1); number 0 appears as I · 10 ^ (I-1) (contains leading 0)/or 9i · 10 ^ (I-2) (does not contain leading 0), then the statistics become very simple.

For detailed statistics, see program notes.
Accode:

# Include <cstdio> # include <cstdlib> # include <algorithm> # include <string> typedef long int64; int64 x10 [20] = {0, 1 }; int64 CNT [10]; int64 a, B; char a [20], B [20]; int la, Lb; inline int64 getint () {int64 res = 0; char TMP; while (! Isdigit (TMP = getchar (); Do res = (RES <3) + (RES <1) + TMP-'0 '; while (isdigit (TMP = getchar (); Return res;} int main () {freopen ("count. in "," r ", stdin); freopen (" count. out "," W ", stdout); A = getint (); ++ (B = getint (); For (INT I = 1; x10 [I] <= B; ++ I) x10 [I + 1] = (x10 [I] <3) + (x10 [I] <1 ); sprintf (a, "% i64d", a); sprintf (B, "% i64d", B); // converts A and B to a string. Strrev (a); strrev (B); // reverse string a and B. -- (La = strlen (a); -- (Lb = strlen (B); // obtain the length of the two strings. For (INT I = 1; I <LB + 1; ++ I) CNT [0] + = x10 [I-1] * (I-1) * 9; // calculate the value of 0 in the low position. For (Int J = 1; j <10; ++ J) CNT [J] + = x10 [LB] * Lb; // count the numbers between 1 and 9 in the low position. For (INT I = LB; I>-1; -- I) {for (Int J = I = LB; // starts from 1 when it is the highest bit, otherwise, it starts from 0. J <B [I]-'0'; ++ J) {CNT [J] + = x10 [I + 1]; // last (I-1) the number of bits starting with J is 10 ^ I. For (int K = 0; k <10; ++ K) CNT [k] + = x10 [I] * I; // The number of low-level numbers is free and can be directly accumulated.} CNT [B [I]-'0'] + = B % x10 [I + 1]; // you can obtain so many high-level numbers.} For (INT I = 1; I <La + 1; ++ I) CNT [0]-= x10 [I-1] * (I-1) * 9; for (Int J = 1; j <10; ++ J) CNT [J]-= x10 [la] * la; For (INT I = La; I>-1; -- I) {for (Int J = I = La; j <A [I]-'0'; ++ J) {CNT [J]-= x10 [I + 1]; for (int K = 0; k <10; ++ K) CNT [k]-= x10 [I] * I;} CNT [A [I]-'0']-= A % x10 [I + 1];} for (Int J = 0; j <10; ++ J) printf ("% i64d", CNT [J]); 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.