hackerscode's blog

By hackerscode, history, 4 years ago, In English

This approach is in O(n) i tried to find better approcah // https://www.geeksforgeeks.org/find-the-occurrences-of-y-in-the-range-of-x/

but I cannt find any approch please suggest me..

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
4 years ago, # |
Rev. 6   Vote: I like it +3 Vote: I do not like it

Let $$$f_{x,y}$$$ be the quantity of digits $$$y$$$ among numbers $$$[0, x]$$$.
Then to answer the query $$$l, r, d$$$ is simply $$$f_{r,d} - f_{l-1,d}$$$.
Preprocissing is $$$O(n * log_{10}n)$$$, and each query if $$$O(1)$$$.
So the total complexity is $$$O(n * log_{10}n + q)$$$.

Code
  • »
    »
    4 years ago, # ^ |
    Rev. 4   Vote: I like it +3 Vote: I do not like it

    By the way, I think there's a more efficient way to solve this problem:
    at first read https://codeforces.com/blog/entry/77096 if you aren't familiar with digit dp.
    After this we have a $$$dp_{i,j,flag} - $$$ quantity of numbers of length $$$i$$$ with $$$j$$$ digits $$$x$$$, and flag tells us whether our prefix of length $$$i$$$ is equal to prefix of length $$$i$$$ of number $$$n$$$ (you can read about it more in the blog I mentioned above).

    C++ code

    Time complexity is $$$O(log_{10}n)$$$

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it
You can try dp-digit