Блог пользователя hackerscode

Автор hackerscode, история, 4 года назад, По-английски

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..

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 6   Проголосовать: нравится +3 Проголосовать: не нравится

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 года назад, # ^ |
    Rev. 4   Проголосовать: нравится +3 Проголосовать: не нравится

    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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
You can try dp-digit