[CodeForces] The Number Of Good Substrings



Key idea:

1. for any substring s, if we can do not consider padding prefix 0s, its decimal value is always >= its length.

2. Appending a binary digit means multiply a number by 2 (and plus 1 if appending 1). This will double the required substring length in order to make its length equal to its decimal value.

3. N <= 2 * 10^5, so if we ignore leading 0s, we do not need to consider substrings with length >= 19, because 2^18 > 2 * 10^5. 


Solution:  Fix the highest bit 1 of a substring, then append 1 more bit to get a new value v, if the current length is the same with v, add 1 to ans. Otherwise check if there are v - len preceeding 0s. To speed this up, precompute a zero prefix sum array. For each fixed highest bit 1, we append at most 18 bits. Do this for each 1 in s.


The runtime is O(N + N * 18)


posted @ 2022-01-17 13:44  Review->Improve  阅读(18)  评论(0编辑  收藏  举报