Search for a substring of a number with a specific property

I am assigned a string consisting only of numbers from 0 to 9. I want to calculate how many substrings of them have a power of 2.

For example, for substation 2560616, substring 256 and 16 are 2. I need to calculate how many such substrings are in any given substring.

Note that the substring is very large, so brute force cannot work. Therefore, I basically want to solve two problems.

  • How to efficiently count all substrings that have power 2
  • How to efficiently calculate if a substring is power 2

I think there might be a DP approach, but I'm not sure about that.

+4
source share
1 answer

2 :

  • , .
  • 2, .
    • . .
    • node node, . , .
    • . (2.), .
    • node .
  • (2.) , p > 10 ^ 5

.

. , 2, :

  • .
  • .
    • .
    • ( (2.)).
    • node node, .
    • node , 1.
    • (2.) , node .
    • , (2.)
  • . (2.) .

" ", , .

0

Source: https://habr.com/ru/post/1543594/


All Articles