American group Codem Digital

Source: Internet
Author: User

Time limit: 1 seconds

Space limit: 32768K
Given two integers l and R, all the approximate numbers of X are written down for all x that satisfies 1≤l≤x≤r≤10^9. For each number written down, keep only the highest digit of that digital. 1~9 the number of occurrences of each digit.
Input Description:

One row, two integers L and R (1≤l≤r≤10^9).

Output Description:

Output 9 rows.

Line I, the number of output digital I appears.

Solution:

Consider [left,right] = [1,right]-[1,left-1], converting the problem to [1,right] and [1,left-1] two sub-interval problems. Just solve the [1,n] interval problem. For a number D, it is d,2d,3d, ... The approximate number of occurrences in the interval [1, n] is n/d (rounded down). Consider the enumeration of the digital p (that is, the highest bit value) and the number of digits Q of approximate d, then D belongs to [p10^ (Q-1), (p+1) 10^ (q-1)). For example, an approximate d of the highest bit of 1 appears in [10,20], [100,200], and so on, and a maximum of 4 of the approximate d appears in [4,5], [40,50], [400,500] and other intervals. > The advantage of dividing the interval is that the highest bit of the interval median is the same and convenient for statistics. There is no need to solve the maximum of the approximate position. > Since d is 10^9, the maximum value of Q is 10, which is calculated by the above formula.

Referring to the above figure, the number of P is calculated as

The length of the enumeration D is Q,q value 1~10, and the 10^ (q-1) value is saved to the POW

Vector <int> M_pow = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
ll Getsum (ll N, ll m) {
    if (n = = 0| | m = = 0) return 0;
    m = min (n, m);
    LL ret = 0;
    LL t = 0;
    LL CNT;
    for (t = 1; t<=m&& (t*t) <=n; t++) {
        ret+=n/t;
    }
    for (LL k=n/t; k>=n/m; k--) {
        cnt = min (M, n/k)-n/(k+1);
        RET + = Cnt*k;
    }
    return ret;
}

The following is an analysis of how sum (n,m) is computed. If calculated directly, the complexity is O (n). The New Ket network system will still determine the timeout. Optimization of the algorithm should be done, which is also the crux of the problem.

From: https://www.nowcoder.com/test/question/done?tid=8961357&qid=104902#summary

#include <bits/stdc++.h> using namespace std;
typedef long Long LL; The length of the enumeration D is Q,q value 1~10, and the 10^ (q-1) value is saved to the pow vector <int> M_pow = {
1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
    ll Getsum (ll N, ll m) {if (n = = 0| | m = = 0) return 0;
    m = min (n, m);
    LL ret = 0;
    LL t = 0;
    LL CNT;
    for (t = 1; t<=m&& (t*t) <=n; t++) {ret+=n/t;
        } for (LL k=n/t; k>=n/m; k--) {cnt = min (M, n/k)-n/(k+1);
    RET + = Cnt*k;
} return ret; }//calculation [1,num] within the approximate condition void work (ll num, vector<int> &res) {for (ll p=1; p<10; p++) {for (ll q=1; q< = 10;
            q++) {if ((p*m_pow[q-1]-1) >num) {break;
            } else{Res[p]+=getsum (Num,min ((p+1) *m_pow[q-1]-1,num))-getsum (num,p*m_pow[q-1]-1);
    }}}} int main () {LL L, R;
    scanf ("%lld%lld", &l,&r);
    Vector <int> Res1 (10);
    Vector <int> Res2 (10); WOrk (L-1, res1);
    Work (R, res2);
    for (int i=1; i<=9; i++) {printf ("%lld\n", Res2[i]-res1[i]);
} 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.