You are given a binary string s (recall that a string is binary if each character is either 0 or 1).
Let f(t) be the decimal representation of integer t written in binary form (possibly with leading zeroes). For example f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0 and f(000100) = 4.
The substring s_{l}, s_{l+1}, ... , s_{r} is good if r - l + 1 = f(s_l ... s_r).
For example string s = 1011 has 5 good substrings: s_1 ... s_1 = 1, s_3 ... s_3 = 1, s_4 ... s_4 = 1, s_1 ... s_2 = 10 and s_2 ... s_4 = 011.
Your task is to calculate the number of good substrings of string s.
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of queries.
The only line of each query contains string s (1 ≤ |s| ≤ 2 ⋅ 10^5), consisting of only digits 0 and 1.
It is guaranteed that ∑_{i=1}^{t} |s_i| ≤ 2 ⋅ 10^5.
Output
For each query print one integer — the number of good substrings of string s.
Example
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to count substrings (contiguous) where the length of substring equals the integer value of the binary representation (including leading zeros). That is, for substring s[l..r] (1-indexed), let len = r - l + 1. Let the binary number represented by that substring (with leading zeros allowed) be val = binary_to_int(s[l..r]). The substring is good if len == val.
We need to count over all substrings.
Constraints: total length sum ≤ 2e5, t up to 1000. So O(n sqrt n) or O(n log n) per string may be acceptable but careful.
We need to handle substrings where the binary number could be large. Since len ≤ n ≤ 2e5, the value val could be up to 2^{len} - 1, which could be huge (exponential). However, note that for a substring to be good, we require val = len. Since len is at most ~2e5, val must be ≤ 2e5. But the substring might be long (up to 2e5). But if substring length > something, its binary value (even with leading zeros) is at least the integer formed by its bits, which is at least 0. But for len large, val is at least 0, but could be small if it has many leading zeros? Actually, consider substring with many leading zeros: e.g., "000...001" of length L, where only the last bit is 1. Then its integer value could be as low as 1 (if the only 1 is at last position). But could it equal L? For L > 1, 1 != L, so not good. For L=1, val=1, good. For long substring with many leading zeros, val can be small (like 1,2,4,...). But len is large. So equality only possible when len is small, or when the substring's binary number is moderate and length matches.
Observation: For any substring, if the first character is 0, then the binary number's most significant bit is 0, but we still treat it as having leading zeros; val is computed as integer from the bits (including leading zeros). Actually, careful: f(t) is decimal representation of integer t written in binary form (possibly with leading zeroes). So f("00101") = 5, which is decimal value of binary string "101". That means they interpret the string as a binary number, ignoring leading zeros. So val is the integer represented by the substring when interpreted as a binary number ignoring leading zeros? Wait the problem statement: "Let f(t) be the decimal representation of integer t written in binary form (possibly with leading zeroes). For example f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0 and f(000100) = 4." So f(t) takes the string t and returns the integer value of the binary number it represents, where leading zeros don't affect the value. So f("011") = 3 because binary "11" is 3. So yes, val = integer value of the substring ignoring leading zeros. This is important.
Thus, for substring s[l..r], let x = the integer formed by the bits after the first '1' (i.e., the number ignoring leading zeros). But note: if the substring consists entirely of zeros, then val = 0.
Now condition: length of substring (r-l+1) must equal val.
So we need to count substrings where the integer value (ignoring leading zeros) equals the length.
Let n = |s|.
A naive O(n^2) checking all substrings is too slow.
Idea: Since val = integer value of the binary string after the first 1 (or zero if all zeros). And val ≤ len. But len can be up to n ~ 2e5. val can be large but only up to len, so val <= len <= n. So we only care about substrings where the integer value after the first 1 is at most n. But also note that if the substring starts with '0', then val could be small even if length is large; but val = len would require len = small, contradictory if len is large. So substrings that are long and start with 0 cannot be good because val is small (≤ 2^{len - number of leading zeros} - 1 but maximum can be up to 2^{len} - 1, but with leading zeros the first bit is 0, so the actual number of significant bits is less than len; the integer value is at most 2^{len - leadingZeros} - 1, which is ≤ 2^{len-1} - 1 if first char is 0? Actually if first char is 0, then there is at least one leading zero, so number of significant bits ≤ len-1, so val ≤ 2^{len-1} - 1. For len >= 2, 2^{len-1} - 1 could be much larger than len; but val could still be large if there are many 1s later, but note that the most significant bit position in the number is the position of first 1. The value of a binary number with first 1 at position i (relative to left, 0-indexed) is at least 2^{len-1-i}? Let's define properly.
Let substring from l to r inclusive, length L. Suppose the first '1' occurs at index p (l <= p <= r) if any; if no '1', then val=0. Then the binary number's value is the integer represented by s[p..r] (if p exists). The length of this significant part is L' = r - p + 1. Then val is at least 2^{L' - 1} (if the first bit is 1) and at most 2^{L'} - 1.
Now condition: L == val.
If L' is the number of significant bits, then val >= 2^{L' - 1} (if L' >= 1). For L to equal val, we need L >= 2^{L' - 1}. Also L >= L' (since L includes leading zeros). So L must be at least max(L', 2^{L' - 1}). For large L', 2^{L' - 1} grows exponentially, but L is only linear up to 2e5. So L' cannot be too large. Let's solve: L' such that 2^{L' - 1} <= L <= 2e5. This gives L' <= log2(2e5) + 1 ≈ 18.6, so L' <= 19? Actually 2^18 = 262144, so L' - 1 <= 17? Let's compute: 2^{17}=131072, 2^{18}=262144. Since L <= 2e5, 2^{L'-1} <= 200k => L'-1 <= floor(log2(200k)) ≈ 17.6 => L'-1 <= 17 => L' <= 18. So significant length L' at most 18 (maybe 19 if L can be up to 200k? Actually 2^18 = 262144 > 200k, so if L'=19, then 2^{18}=262144 > max L 200k, so val >= 2^{18} > L, impossible for L==val). So L' <= 18. Also if L'=18, then val >= 2^{17}=131072, and L must be equal to val, so L >=131072, which is possible since max n is 2e5. So L' can be up to 18. If L'=18, then L=val between 131072 and 262143, but L <= 2e5, so val <=200k, so val max 200k, so 2^{18} - 1=262143 >200k, so val may be >200k for some numbers, but if val >200k, then L would be >200k, which is impossible since substring length can't exceed n ≤ 2e5. So only those with val ≤ n are considered.
Thus, for any good substring, the number of significant bits (i.e., after the first 1) is at most 18 (maybe 19 if n is larger? but n <= 200k, so indeed 18). Also note that if the substring is all zeros, then val=0, length L must be 0, impossible since L>=1. So good substrings cannot be all zeros. So the substring must contain at least one '1'.
Thus, we can approach by iterating over each position where a '1' appears, and consider substrings starting at that position (the first character of the significant part) and extending to the right, computing the integer value as we go, and checking if the substring length (including any leading zeros? Wait if we start at the position of the first 1, then the substring from l to r where l is before the first 1 includes leading zeros. But our current iteration considers substrings where the first 1 is at the leftmost considered? Actually we want to count all substrings, including those with leading zeros. But condition depends on val which ignores leading zeros, and length L is total length. If we start from a '1' and consider substrings that start at that '1' (i.e., l = position of first 1), then there are no leading zeros. That's a subset of good substrings: those with no leading zeros. But there might be good substrings that have leading zeros. Example: s = "0110" has good substrings: s1..s1 = "1"? Actually s1=0? Wait "0110" positions: 1:0,2:1,3:1,4:0. Good substrings? They say output 4. Let's compute: good substrings: "1" at position2? Actually they counted in example for "1011": s1..s1=1, s3..s3=1, s4..s4=1, s1..s2=10, s2..s4=011. So substrings starting with 0 can be good if the integer value ignoring leading zeros equals length. Eg "011" of length3, val=3 => good. So substrings with leading zeros are included. So we need to count those as well.
But note that if a substring has leading zeros, its val is determined by the first 1 inside it. So essentially, each good substring corresponds to a pair (p, r) where p is the position of the first '1' in the substring (or none if all zeros, but then not good), and l <= p is the start, and l can be before p, adding leading zeros. Condition: Let L = r - l + 1 = (p - l) + (r - p + 1) = leadingZeros + L', where L' = r - p + 1 (length of significant part). And val = integer value of s[p..r] (binary). Condition: L = val.
So for a given significant part (p, r), we need to have leadingZeros = val - L'. Since leadingZeros = p - l >= 0. So l = p - (val - L') must satisfy l >= 1 and also that there are no 1's between l and p-1 (i.e., the substring from l to p-1 must be all zeros), because p is the first 1. So the substring's prefix up to p-1 must be zeros.
Thus, for each possible significant part (i.e., substring starting at a '1' and ending at r), we can compute val. Then we know that for this (p, r) to be extendable to a good substring, we need L' = r-p+1, and val >= L' (since leadingZeros = val - L' >= 0). Also, we need that we can choose l such that l = p - (val - L') and the positions from l to p-1 are all zeros. And also l >= 1.
Thus, for each (p, r) with p being a '1', we can check if val >= L' (which is true for most because val >= 2^{L'-1} and L' <=18, but val could be less than L'? Possibly if val is small and L' is large? Actually val is at least 2^{L'-1} (if L'>=1), which for L'>=1 is at least 1. For L'>=? Example: L'=5, val min = 16, L'=5, val> L'. Actually 2^{L'-1} >= L' for L' >= 2? Let's test: L'=2: 2^{1}=2 >=2 true. L'=3: 4 >=3 true. L'=4:8>=4 true. L'=5:16>=5 true. So for L'>=2, val >= 2^{L'-1} > L' (since 2^{L'-1} grows fast). For L'=1, val is either 0? No if L'=1, s[p] = '1', val = 1 (since binary "1" =1) or could be 0? If L'=1, s[p] must be '1' because we start at first 1, so it's '1', so val =1. So val = 1, L' =1, so val = L'. So for L'=1, val =1. So val >= L' always, and equality only when L'=1? Actually for L'>1, val > L' because min val is 2^{L'-1} which is > L' for L'>=2. So val - L' is positive for L'>=2. So leadingZeros = val - L' >=1 for L'>=2. For L'=1, leadingZeros = 0 (if val=1, L'=1, leadingZeros=0). So for L'=1, the good substring would start exactly at p (no leading zeros). So essentially for each (p,r) with L'=1 (i.e., r=p), we have a good substring if val=1 = L' -> leadingZeros=0 -> l=p. So that substring itself is good.
For L'>=2, we need to have enough leading zeros before p to cover val - L'. Also, those leading zeros must be present and no other '1' in that range.
Thus, counting good substrings reduces to: For each position p where s[p] = '1', we consider all r >= p such that the binary number from p to r, when interpreted as integer v, satisfies v <= n (since L = v <= n because substring length cannot exceed n; actually v must equal length L, and L <= n, so v <= n). Also we need that v - (r-p+1) <= number of consecutive zeros immediately to the left of p (i.e., we can extend leftwards that many zeros without hitting another 1 or start of string). And also the total length L = v must not exceed n, but since v <= n, and substring from l to r must fit within string: l = p - (v - (r-p+1)) must be >= 1 and the positions from l to p-1 must be zeros. And also, we need that there is no '1' between l and p-1. This is equivalent to requiring that the number of zeros immediately to the left of p (i.e., the length of the zero-run ending at p-1) is at least (v - (r-p+1)). But careful: if there is a '1' before p within that range, then p wouldn't be the first 1. So we need that the entire segment from l to p-1 is all zeros. So we need that the run of zeros directly to the left of p (contiguous zeros ending at p-1) has length at least needed leading zeros. But also if p is the first character of the string (p=1), then left zeros length = 0.
Also note that there might be multiple ways to choose l? For a given (p,r), the condition uniquely determines l = p - (v - L'). Because L must equal v, and L = (p-l) + L' = v => p-l = v - L' => l = p - (v - L'). So it's unique. So each good substring corresponds uniquely to a pair (p, r) where p is the first 1 in that substring, and r is the last index. And it must satisfy that the number of zeros immediately preceding p is at least v - L', and also that l >= 1.
So we can iterate over all p (positions of '1'), and for each, iterate over r up to some limit such that v (the integer value of substring p..r) stays ≤ n (since v must be ≤ n, and also v - L' must be ≤ zeros_left(p) to be valid). But we also need that the length L = v must be ≤ n, but also l = p - (v - L') must be ≥ 1. However, zeros_left(p) is limited, and v - L' ≤ zeros_left(p) automatically ensures l = p - (v - L') >= 1? Not necessarily: if zeros_left(p) is the number of consecutive zeros immediately before p, but p - (v - L') might be less than 1 if the needed left extension goes beyond the start of string. However, if zeros_left(p) is finite, but p - (v - L') < 1 means we need more zeros than available before the string. Actually zeros_left(p) counts zeros only within the string; if p - (v - L') < 1, then l would be ≤ 0, impossible. But if p is small, zeros_left(p) might be less than needed because the start limits. However, zeros_left(p) is defined as number of zeros directly to the left of p up to the previous '1' or start. If we need to extend left beyond index 1, it's impossible. So we need p - (v - L') ≥ 1. That is equivalent to v - L' ≤ p - 1. Since zeros_left(p) ≤ p-1, but zeros_left(p) may be exactly p-1 if the string from 1 to p-1 are all zeros. In general, zeros_left(p) is the length of contiguous zeros ending at p-1; but if there is a '1' before, zeros_left(p) is less than p-1. However, we require that the segment from l to p-1 is all zeros; this implies that the entire segment must be zero. So it's not enough that zeros_left(p) >= needed; we also need that there is no '1' before that zero run. Actually zeros_left(p) by definition is the maximum length of zeros ending at p-1. So if needed <= zeros_left(p), then there exists a l = p - needed such that s[l..p-1] are all zeros (since zeros_left(p) >= needed ensures that the previous needed characters are zeros). However, it's possible that zeros_left(p) >= needed, but p-needed might be less than 1? zeros_left(p) can be at most p-1. If needed = p, zeros_left(p) cannot be p because maximum is p-1. So zeros_left(p) >= needed already implies needed <= p-1? Actually zeros_left(p) <= p-1, so if needed <= zeros_left(p), then needed <= p-1, so l = p - needed >= 1. So the condition zeros_left(p) >= needed ensures both that there are enough zeros and that l is within bounds. But also we must ensure that there is no '1' between l and p-1; zeros_left(p) guarantees that the immediate zeros before p are contiguous; but if needed is less than zeros_left(p), then we are taking a subset of that zero-run, and that subset will also be all zeros. So it's fine.
Thus, for each p with s[p]='1', let leftZeros = number of consecutive zeros immediately before p (i.e., the maximum k such that p-k >= 1 and s[p-k..p-1] are all '0'). Then for each r >= p, let L' = r-p+1, compute v = value(s[p..r]) as integer (binary). If v > n (since n is max length) then we can stop extending r because v will only increase as we add more bits, and v already exceeds max possible length? Actually v must equal L = L' + leadingZeros, but leadingZeros is non-negative, so L >= v. But condition is L = v. If v > n, then L = v > n, which is impossible because substring length cannot exceed n. Also if v > n, even if we could extend left, L would be at least v (since L = v), which exceeds n. So we can stop when v > n. Also we might stop earlier if L' becomes too large, but we already reasoned L' ≤ 18, but that's only because v grows at least exponentially; however, if we extend r, v can become huge quickly. Since n ≤ 2e5, the maximum L' that yields v ≤ n is at most about 18 (since 2^18 = 262144 > 200k, but some 18-bit numbers are ≤ 200k). So indeed, for each p, we only need to consider r up to p+18 or p+19 maybe. But caution: even if v becomes > n, we can stop. But also if v is too big, there's no chance. So we can limit the window to at most 18-20 bits.
But we also need to consider that for a given (p, r) with v <= n, we check if leftZeros >= v - L'. If yes, then the substring with start l = p - (v - L') is good. Additionally, we must ensure that the substring does not include any '1' before p? Actually we already ensure that because we require leftZeros >= needed, which guarantees the segment from l to p-1 is all zeros. But what about the possibility that there is a '1' before l? That doesn't matter because the substring starts at l, and its first 1 is at p, so it's okay if there is a 1 earlier than l (outside the substring). So it's fine.
Thus, algorithm: For each query string s of length n:
Precompute leftZeros array: for each position i (1-indexed), leftZeros[i] = number of consecutive zeros ending at i-1. Specifically, if i == 1, leftZeros[1] = 0. Else if s[i-1] == '0', then leftZeros[i] = leftZeros[i-1] + 1? Actually careful: leftZeros[i] should represent the number of zeros immediately before i. So we can compute by scanning: maintain count of consecutive zeros seen so far. When we see a '1', we can set leftZeros for that position to current zero count? But we need leftZeros for each position i where s[i] = '1'? Also might need for '1' positions only. But we may also need for positions that are '1'? Actually we need leftZeros only for positions p that are '1' (since they are candidate first-1). So we can compute an array zeroRun ending at i? Alternatively, we can compute prevOne array: for each i, the index of previous '1' (or 0 if none). Then zeros before i = i - prevOne[i] - 1? But careful: if there are zeros between prevOne and i, that's correct. However, if i is a '1', then zeros to its left that are contiguous (i.e., immediately before i) are exactly i - prevOne[i] - 1, where prevOne[i] is the index of the most recent '1' before i (or 0). But if there is no previous '1', then zeros left = i-1. That works. However, if the string has zeros and then a '1', then after that '1', zeros appear again, then next '1', zeros left = distance to previous '1' minus 1. This indeed gives the length of the zero-run immediately before i. Example: s = "00101", indices: 1:0,2:0,3:1,4:0,5:1. For i=5 (s[5]=1), prevOne[5] = 3, zeros left = 5-3-1 = 1 (correct, there is one zero at position 4). For i=3, prevOne[3] = 0, zeros left = 3-0-1 = 2 (positions 1-2 zeros). So that works.
But we also need to consider the case where there are multiple zeros and we might extend left beyond the previous '1'? No, because if we extend left too far, we would include that previous '1', which would become the first 1 in the substring, contradicting that p is the first 1. So the maximum left extension is limited by the distance to the previous '1' (or start). So zeros_left(p) is exactly i - prevOne[i] - 1? Actually if we want the substring to start at l, we need all chars from l to p-1 to be zeros. The maximum possible l (minimum start) is such that we don't include any earlier '1'. So the furthest left we can go is just after the previous '1' (or the start). That is exactly: l_min = prevOne[i] + 1 if prevOne[i] exists, else 1. And the zeros available are exactly (p - l_min) = p - (prevOne[i]+1) = (p - prevOne[i] - 1). So indeed, leftZeros = p - prevOne[i] - 1. That's the maximum number of zeros we can have before p without including a previous '1'. So that's the relevant quantity.
Thus, we can compute prevOne for each position (or just for each '1').
Now algorithm:
Initialize ans = 0.
For each position p from 1 to n where s[p] == '1':
Let prev = index of previous '1' before p (or 0 if none). So leftZeros = p - prev - 1.
Initialize val = 0.
For r from p to min(n, p + 20) maybe: Actually we need to go while val <= n. But as we add bits, val doubles and adds bit. Since n ≤ 2e5, val will exceed n quickly after about 18 bits. So we can cap r at p+18? But to be safe, we can go up to p+20 or while val <= n. Let's compute max bits: maximum n=200k ~ 2^17.6, so 18 bits may produce numbers up to 2^18 -1=262143, which may be > n for some but still we need to consider numbers ≤ n. So we can loop r from p to min(n, p+19) because 19 bits could be up to 524287 > n, but some 19-bit numbers might be ≤ 200k? Actually the smallest 19-bit number is 2^18 = 262144 > 200k. So any 19-bit binary number (i.e., length of significant bits 19) is at least 2^18 = 262144 > n. So if L' = 19, val >= 262144 > n, impossible. So we only need L' up to 18. So r max = p+17 (since L' = r-p+1, max 18 -> r = p+17). That is safe for n up to 2e5. But to be safe for n up to 2e5, 2^18=262144 >200k, so yes. For n maybe up to 200000, 2^17=131072, 2^18=262144 > n, so L'=18 possible only if val <=200k, but smallest 18-bit number is 2^17=131072, which is ≤200k, so some 18-bit numbers are allowed. So L' max = 18. So we can loop r from p to min(n, p+17). Actually if p+17 > n, we stop at n. But L' could be less if r near end. So that's fine.
But we must be careful: if n is exactly 200000, and p is near end, r might not reach p+17 because end of string. So we loop as long as r <= n and L' <= 18 (or val <= n). But if we cap at 18 bits, it's okay because beyond that val > n, no good. However, what if n is larger than 200000? But constraints say total sum ≤ 200k, each string length ≤ 200k. So n <= 200k. So safe.
Now for each r in that range:
L' = r - p + 1.
val = val * 2 + (s[r] - '0'). (Starting from 0 at r=p-1? Actually we can compute iteratively.)
If val > n: break (since further extensions will only increase val).
needed = val - L'. // leading zeros needed
if needed <= leftZeros: then ans++.
But careful: When we compute val, it's the integer value of substring s[p..r] as binary (without leading zeros). Since we start at the first 1, val >= 1 for the first iteration (r=p, s[p]='1', val=1). That's fine.
Now, is there any other possibility? What about substrings where there are multiple leading zeros but no '1'? They would be all zeros, val=0, length L must be 0, impossible. So ignore.
Also, what about substrings where the first 1 is at position p, but we could also have leading zeros that extend left beyond the previous '1'? That would include that previous '1', making it the first 1. So not allowed. So our leftZeros is correct.
Thus, ans is count.
Now test on examples.
Example 1: s="0110", n=4.
Indices: 1:'0',2:'1',3:'1',4:'0'.
Compute prevOne for each '1':
p=2: prevOne? previous '1'? none (0), leftZeros = 2-0-1 =1 (position1 is zero). Good.
p=3: prevOne=2, leftZeros=3-2-1=0.
Now iterate:
p=2:
r=2: val=1, L'=1, needed=0, leftZeros=1 >=0 => ans++ (substring from l=2 to 2? l = p - needed = 2, so "1" at pos2). Good.
r=3: extend: val = 12 + s[3]=? s[3]='1', so val=3, L'=2, needed=3-2=1, leftZeros=1 >=1 => ans++ (l = 2-1=1, substring from 1 to 3: "011", val=3, length=3). Good.
r=4: val = 32 + s[4]='0' = 6, L'=3, needed=6-3=3, leftZeros=1 < 3 => not good. Also val=6 > n? n=4, but val=6>4, so break? Actually we break when val > n because further will only increase. So we break. So from p=2 we got 2.
p=3:
r=3: val=1, L'=1, needed=0, leftZeros=0 >=0 => ans++ (substring "1" at pos3). Good.
r=4: val = 1*2 + 0 =2, L'=2, needed=2-2=0, leftZeros=0 >=0 => ans++ (substring from l=3 to 4: "10"? Actually s[3..4]="10", val=2, length=2, good). So ans becomes 4.
That matches output 4.
Example 2: s="0101", n=4.
Indices:1:'0',2:'1',3:'0',4:'1'.
p=2: prevOne=0, leftZeros=2-0-1=1.
r=2: val=1, needed=0 -> ans++ (pos2 "1")
r=3: val = 12+0=2, L'=2, needed=0? 2-2=0, leftZeros=1 >=0 -> ans++ (substring from l=2? Actually l = p - needed =2, so s[2..3]="10", val=2, len=2, good)
r=4: val = 22+1=5, L'=3, needed=5-3=2, leftZeros=1 <2, not good; val=5>4 so break. So from p=2: 2 good.
p=4: prevOne=2, leftZeros=4-2-1=1.
r=4: val=1, needed=0, leftZeros=1>=0 -> ans++ (pos4 "1")
r beyond? r=5 > n stop. So total ans=3. matches output 3.
Example 3: s="00001000", n=8. They output 4. Let's compute.
Indices: 1-4 zeros, 5:'1',6:'0',7:'0',8:'0'.
p=5: prevOne=0, leftZeros = 5-0-1=4 (positions1-4 zeros).
Now iterate r:
r=5: val=1, L'=1, needed=0 -> ans++ (substring pos5 "1")
r=6: val = 12+0=2, L'=2, needed=0? 2-2=0 -> leftZeros=4 >=0 -> ans++ (substring from l=5? Actually l =5, substring "10" (positions5-6), length=2, val=2)
r=7: val=22+0=4, L'=3, needed=4-3=1, leftZeros=4 >=1 -> ans++ (need 1 leading zero: l =5-1=4, substring positions4-7: s[4..7]="0100"? Wait s[4]='0', s[5]='1', s[6]='0', s[7]='0' => "0100"? But val=4, length=4? Actually substring length L = 4 (positions4-7). Check: s[4]=0, s[5]=1, s[6]=0, s[7]=0 => binary "0100" ignoring leading zeros gives 100? Wait binary "0100" value = 4? Yes, "100" is 4. length 4, val 4, good. So ans++.
r=8: val=4*2+0=8, L'=4, needed=8-4=4, leftZeros=4 >=4 -> ans++ (l =5-4=1, substring positions1-8: entire string "00001000"? That's length8, val? binary "1000"=8, good). So from p=5 we got 4.
Any other p? There is only one '1' at position5. So ans=4. matches output.
What about substrings that have val = L, but the first '1' is not at the start of the substring, and there are zeros before p, but there might also be zeros after p? That's fine; we already include r after p.
What about substrings where the binary number includes leading zeros, but the first 1 is not the first character? That is exactly our model.
What about substrings that start with '1' and have length 1? That's covered.
What about substrings where val = L but the needed leading zeros exactly equals leftZeros? Good.
What about substrings where needed leading zeros = 0? Good if leftZeros >=0, always true. So for any (p,r) with val = L' (i.e., needed=0) automatically good, regardless of leftZeros. This occurs when val = L'. When does that happen? For L'=1, val=1, so needed=0. For L'=2, val could be 2? Binary "10" is 2, length2, so needed=0. For L'=3, val could be 3? Binary "011"? That's leading zero? But if we start at the first 1, the substring from p to r must be exactly the bits that produce val. For L'=3, can val=3? Binary "11" is 3 but length2, so not length3. To have val=3 with length3, the binary representation would be "011" (leading zero). But if we start at the first 1, the substring includes the first 1, so we cannot have a leading zero in the substring because p is first 1. So the substring from p to r, by definition, has no leading zeros. So its binary representation has first bit 1, so the number is at least 2^{L'-1}. For L'=3, min val=4 (binary 100). So val cannot be 3. So needed=0 only when L'=1 and val=1, or L'=2 and val=2? Check: For L'=2, min val=2 (binary 10) and max 3. So val=2 gives needed=0. For L'=3, min val=4, so needed at least 1. For L'=4, min val=8, needed at least 4? Actually needed = val - L' >= 8-4=4. So needed is positive for L'>=3. So only L'=1 and 2 can have needed=0. So those substrings are automatically good (if val <= n). That matches examples: "10" works.
But careful: For L'=2, val=3 also possible (binary 11) => needed=1. So not automatic.
Thus algorithm looks sound.
Complexity: For each '1' position (at most n), we iterate at most 18 steps. So total O(n * 18) ~ O(3.6e6) for total sum n = 2e5, which is fine. Also we need to compute prevOne quickly. We can scan once and keep lastOne index.
Implementation details:
For each string s of length n:
Convert to 0-indexed or 1-indexed? Easier to use 0-indexed for coding but careful with formulas. Let's use 0-indexed: positions 0..n-1.
We'll compute prevOne array: for each i, prevOne[i] = index of previous '1' before i, or -1 if none. But we only need for positions where s[i]=='1'. But we can compute lastOne as we go.
Better: While iterating i from 0 to n-1, when we see a '1', we can set p = i, and leftZeros = i - (lastOne + 1) = i - lastOne - 1? Actually lastOne is index of previous '1' (or -1 if none). Then leftZeros = i - lastOne - 1. Because the zeros between lastOne+1 and i-1 are the allowed leading zeros. So leftZeros = i - lastOne - 1. Then update lastOne = i.
Now for each such p, we loop r from p to min(n-1, p+maxBits-1), but maxBits we can compute as up to 18? Since n ≤ 200000, we can set maxLen = 18 (or maybe 20). But to be precise, we break when val > n. But we must also ensure we don't exceed n-1. So:
For p in positions where s[p]=='1':
val = 0
for r in range(p, n):
val = val * 2 + (s[r] - '0')
if val > n: break
Lprime = r - p + 1
needed = val - Lprime
if needed <= leftZeros:
ans += 1
But this could loop up to n for each p, but we argued val will exceed n quickly. However, in worst-case, if n is 200k, and the bits are all zeros after p? But note: p is a '1', so val starts at least 1. As we extend, if the bits are zeros, val doubles each time (since val = val * 2 + bit). Even if bit is 0, val doubles. So val grows exponentially. Starting from 1, after k steps, val >= 2^{k-1} (since first step yields at least 1). Actually if we have a leading 1 then zeros, val = 2^{L'-1} (e.g., "1000..." -> val = 2^{L'-1}). So after 19 bits, val >= 2^{18} = 262144 > 200k. So indeed maximum L' around 18. But what if bits are not zeros? If bits are ones, val grows even faster. So the loop length per p is at most ~18-20. So it's safe.
But we need to ensure we also break when val > n, because there might be cases where val stays <= n for more than 18 bits? For n=200k, max bits that can keep val <= n is floor(log2(n)) + 1? Actually the maximum binary number with L' bits is 2^{L'} - 1. So we need 2^{L'} - 1 <= n -> L' <= floor(log2(n+1)). For n=200000, log2(200001) ~ 17.6, so floor = 17? Actually 2^17 = 131072, 2^18 = 262144 > 200000, so maximum L' such that even the maximum number fits is 17? Because 2^{17} - 1 = 131071 <= 200000, yes. 2^{18} - 1 = 262143 > 200000, so not all 18-bit numbers fit, but some 18-bit numbers could fit if they are less than n. For example, 18-bit number with value 150000 is less than 200000, and 150000 > 2^{17}=131072, so yes some 18-bit numbers are <= n. So L' could be 18. So we need to allow L'=18. The minimal 18-bit number is 2^{17}=131072, so if n >= 131072, then L'=18 is possible. So loop length per p up to 18. But careful: if p is near the end, we may have less than 18 positions. So overall O(n * 18) fine.
Now we must also consider that needed = val - L' could be negative? Is that possible? val >= 1 and L' >= 1, and for L'=1 val=1, val - L'=0. For larger L', val >= 2^{L'-1} > L' for L'>=3, so positive. For L'=2, val could be 2 or 3, so val - L' = 0 or 1, non-negative. So needed >=0. So ok.
Now what about substrings that have more than one candidate p? Our method counts each good substring exactly once because each good substring has a unique first 1. But is it possible that a substring has two 1's? The first 1 is unique. So yes.
Edge Cases:
String with no '1's: Then there are no good substrings because val=0, length >=1 cannot equal 0. So ans=0.
String with many 1's: Works.
Now double-check with some random cases to ensure no missing good substrings.
Consider s = "10". n=2. p=0: leftZeros=0. r=0: val=1 -> ans1 (substr [0,0] "1") r=1: val = 1*2+0=2, L'=2, needed=0 => ans2 (substr [0,1] "10") => total 2. Also p=1? s[1]='0' not a '1', so done. So ans=2. Check manually: Substrings: "1"(len1 val1 good), "0"(len1 val0 not good), "10"(len2 val2 good) -> 2 good. Works.
Consider s = "11". n=2. p=0: leftZeros=0. r=0: val=1 -> ans1. r=1: val = 1*2+1=3, L'=2, needed=1, leftZeros=0 => not good (since needed 1 >0). p=1: leftZeros = 1 - 0 -1? Wait previous one index =0, leftZeros = 1-0-1=0. r=1: val=1 -> ans2. So total 2. Manual: substrings: "1" (pos1), "1" (pos2), "11" (len2 val3 not good). So 2 good. Works.
Consider s = "101". n=3. p=0: leftZeros=0. r0: val=1 -> ans1. r1: val=2 (since 10), L'=2, needed=0 -> ans2 (substr 10). r2: val=5 (101 binary =5), L'=3, needed=2, leftZeros=0 -> not good. p=2 (index2): leftZeros = 2 - 0? Wait previous one is at index0? Actually positions:0:'1',1:'0',2:'1'. For p=2, previous one index =0, leftZeros = 2-0-1=1 (position1 zero). r=2: val=1 -> ans3 (substr "1" at pos3). So total 3. Let's list all substrings: positions (1-based): 1: "1" good, 2:"0" no, 3:"1" good, (1,2):"10" good, (2,3):"01" val=1, length=2 -> 1!=2, not good, (1,3):"101" val=5, length=3 -> not good. So 3 good. Works.
Consider a case where there are leading zeros and a 1, and also zeros after. Already covered.
Consider a tricky case: s = "0010". n=4. Let's manually compute good substrings: positions:1:0,2:0,3:1,4:0.
Substrings:
"0"(len1 val0 no), "0"(2), "1"(3) good, "0"(4) no.
"00"(1-2) val0 no, "01"(2-3): "01" val=1 len2 -> no, "10"(3-4): "10" val=2 len2 -> good? "10" val=2 len2, yes good.
"001"(1-3): "001" val=1 len3 -> no, "010"(2-4): "010" val=2? binary "10" =2, len=3 -> no,
"0010"(1-4): "0010" val=2, len=4 -> no. So good: "1" at pos3, "10" at pos3-4. That's 2.
Our algorithm: p=3 (index2 0-index? Let's index 0: s[0]=0,s[1]=0,s[2]=1,s[3]=0). For p=2 (0-index), prevOne? previous one? none so far? Actually after scanning, when we hit first '1' at index2, lastOne initially -1, leftZeros = 2 - (-1) - 1 = 2. Good.
r=2: val=1, needed=0 -> ans1 (substr [2,2]).
r=3: val = 1*2+0=2, L'=2, needed=0 -> leftZeros=2 >=0 -> ans2 (substr [2,3]? That's s[2..3]="10", l=2 (since needed=0), yes)
r=4? out of range.
Other p? No other '1'. So ans=2. Good.
Now consider s = "00100". n=5. s:0,0,1,0,0. p=2 (0-index). leftZeros=2. r=2: val=1 -> ans1. r=3: val=2, L'=2, needed=0 -> ans2 (10). r=4: val=4 (binary "100"), L'=3, needed=1 -> leftZeros=2 >=1 -> ans3 (need 1 leading zero: l = p-1=1 (0-index?), so substring positions 1..4: s[1..4]="0100"? Actually s[1]=0,s[2]=1,s[3]=0,s[4]=0 => "0100" val=4, len=4, good). r=5? out of range. So ans=3. Check manually? Good.
Now consider s = "0001001000"? Something.
Now consider potential pitfalls: We need to ensure that for each (p,r), the substring we count indeed lies within the string: l = p - needed must be >= 0 (0-index) and we also require that the positions from l to p-1 are zeros. Our condition leftZeros >= needed ensures that, because leftZeros is the count of zeros immediately before p without crossing a '1'. But what if needed is exactly leftZeros, but there is a '1' exactly at l? Actually leftZeros is defined as the number of consecutive zeros ending at p-1. That means positions p - leftZeros ... p-1 are all zeros. If needed = leftZeros, then l = p - needed = p - leftZeros. That is exactly the first index after the previous '1' (or start). So l is either start or after a '1'. If there was a '1' at l? Actually l cannot be a '1' because then leftZeros would be less (since if there was a '1' at l, the zeros before p would start after that '1', so leftZeros would be p - l - 1). So it's fine. So condition ensures all chars from l to p-1 are zeros. Good.
But we also need to ensure that we don't count substrings where the "first 1" is not actually the first 1 because there is a '1' between l and p-1. But we've ensured those are zeros. So it's good.
Now, are there any good substrings where the first 1 is not the first occurrence of 1 in the entire string, but there is a '1' before l? That would be outside the substring, so it's fine. The first 1 in substring is indeed p.
Now consider cases where needed is 0 but leftZeros > 0, we count the substring starting exactly at p (since l=p). That's correct. But what about the possibility of leading zeros being present but we choose not to include them? That's exactly the substring with l=p, which is allowed. Our method counts that as a separate substring for each (p,r) that satisfies condition. So good.
Now check a case where there are multiple possible l for the same p and r? As argued, condition L = val determines l uniquely, so only one possible. So fine.
Now complexity: For each string, we loop over its '1's. Each '1' processes at most 18 r's. Total operations ~ O(18 * number_of_ones). In worst case all characters are '1', number_of_ones = n, so total ~ 3.6 million, fine.
But we must be careful with the break condition: when val > n, we break. However, note that val could become > n early, but later if we add more bits, val may become smaller? No, binary numbers always increase when you add more bits to the right because you are shifting left (multiplying by 2) and adding a bit (0 or 1), so val_new = val2 + bit >= val2 > val for val > 0. For val=0 (not possible since p is '1', val >=1). So monotonic increasing. So once val > n, all longer extensions will be > n, so we can break.
Now implement.
Implementation steps per query:
Read string s.
n = len(s)
ans = 0
lastOne = -1 (0-index, index of previous '1')
for i in range(n):
if s[i] == '1':
leftZeros = i - lastOne - 1
val = 0
for j in range(i, n):
val = val * 2 + (ord(s[j]) - ord('0'))
if val > n:
break
Lprime = j - i + 1
needed = val - Lprime
if needed <= leftZeros:
ans += 1
lastOne = i
Print ans
Edge Cases: when n is small, works.
But we need to be mindful of integer overflow? val could become large (exceed n) but we break. In Python, no overflow issues.
Now we should test with maximum constraints: n=200k, all '1's. Then for each '1', we will loop until val > n. For the first '1' at position 0, val will become: 1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143 >200000? Let's compute: 2^{k} -1 for k bits: 1,3,7,... After 17 bits: 2^17 -1 = 131071; after 18 bits: 2^18 -1 = 262143 >200k. So val will exceed n at 18 bits. So loop length ~18. For later '1's, similar. So total operations ~200k*18=3.6M, fine.
But is there a risk that for some p, val stays <= n for more than 18 steps? For numbers not all ones, they might be smaller? Actually the smallest growth is when bits are zeros: val doubles each time (since val = val2 + 0). Starting from 1, after k-1 zeros, val = 2^{k-1}. So after 18 bits, val = 2^{17} = 131072, still ≤ 200k. After 19 bits, val = 2^{18} = 262144 > 200k. So for zeros pattern, it takes 19 bits to exceed n? Let's recalc: Starting at r=p, L'=1, val=1 = 2^0. For L'=2, if bit=0, val=2 = 2^1. For L'=3, if bits "100"? Actually pattern: first bit 1, then zeros: val = 2^{L'-1}. So for L'=18, val = 2^{17} = 131072 <=200k. For L'=19, val = 2^{18} = 262144 > 200k. So for this zero pattern, we would have to iterate to L'=19 to break? But our max bits earlier we thought 18, but actually L'=19 might be needed for val to exceed n. However, recall we also have condition that val <= n. If val = 131072 at L'=18, that's <= n (200k), so we consider it. At L'=19, val=262144 > n, break. So we need to iterate up to L'=19 possible. But is L'=19 possible? That would be 19 bits, but note that the integer value of 19-bit number with first bit 1 is at least 2^{18} = 262144 > n. So any 19-bit number will exceed n. So we can break when val > n, but we will attempt to compute r for L'=19 only if val after L'=18 is <= n, and then compute for L'=19: val = previous2 + bit, which will be >= 2*prev. Since prev at L'=18 is at least 2^{17}=131072, then val >= 262144 > n, so break at L'=19. So we need to allow j loop to go up to maybe 20, but that's fine. So worst-case per p we might do 19 iterations. Still fine. For n=200k, max L' that yields val <= n is 18, but we might iterate 19 to detect break. So 19 iterations per p.
Now consider if n is 200000, and the bits after p are such that val remains small? But as argued, minimum val for a given L' is 2^{L'-1}, so grows exponentially. So L' cannot be >19. So we are safe.
Thus O(total length * 20) is safe.
Now check the case where p is near the end: j loop will stop when j reaches n-1, possibly before val exceeds n. But that's okay.
Now we need to confirm that we haven't missed any good substrings where the first '1' is not the first character and we have leading zeros but also there are zeros after p that might affect the integer value? That's already accounted in val.
Now think about substrings where the first '1' is not at p? Wait p is the first '1' in substring. So we are enumerating all substrings by enumerating all possible (first 1 position, last position). That covers all substrings that contain at least one '1'. Substrings with no '1' are not good because val=0, length>=1, so not counted.
So algorithm appears correct.
But wait: Could there be a substring where the first '1' appears at p, but the integer value of s[p..r] is not equal to the length of substring? That's the condition we test. Good.
Now I'd like to double-check with some random brute force for small strings to ensure no off-by-one or misinterpretation of leftZeros.
Let's implement a quick mental test with a string: s = "01010". n=5.
Positions: 1:0,2:1,3:0,4:1,5:0.
We'll compute manually good substrings:
List all substrings (1-indexed):
(1,1):0 ->0 not good
(2,2):1 -> good
(3,3):0 -> no
(4,4):1 -> good
(5,5):0 -> no
(1,2):01 val=1 len2 -> not good
(2,3):10 val=2 len2 -> good
(3,4):01 val=1 len2 -> no
(4,5):10 val=2 len2 -> good
(1,3):010 val=2 len3 -> no
(2,4):101 val=5 len3 -> no (5!=3)
(3,5):010 val=2 len3 -> no
(1,4):0101 val=5 len4 -> no
(2,5):1010 val=10 len4 -> no
(1,5):01010 val=10 len5 -> no
So good: (2,2), (4,4), (2,3), (4,5) => total 4.
Our algorithm:
Indices 0-based: [0]=0,1=1,2=0,3=1,4=0.
lastOne=-1.
i=1 (s[1]='1'): leftZeros = 1 - (-1) - 1 = 1. (zero at index0)
r=1: val=1, L'=1, needed=0 => ans1 (substr [1,1])
r=2: val = 12+0=2, L'=2, needed=0 => leftZeros>=0 => ans2 (substr [1,2]="10")
r=3: val = 22+1=5, L'=3, needed=2, leftZeros=1 <2 => no; val=5<=5? n=5, val=5, still <=n. But needed too high.
r=4: val = 52+0=10, L'=4, needed=6, leftZeros=1 no, val=10>5 break? val=10>5, break after checking? We break when val>n, but we already computed r=3? Actually after r=3, val=5 still <=5, we continue to r=4: compute val=10, which is >5, so we break before counting? In loop, we compute val then check if val>n break after possibly counting? But we should break after computing val>n without counting because condition fails anyway? But we need to break after checking? Actually if val>n, it cannot be good because length would need to be >n which is impossible. So we can break after we see val>n. But careful: In loop we compute val = val2 + bit, then if val > n: break (so not counting). So at r=4, val=10>5, break. So fine.
So from i=1 we got 2.
i=3 (s[3]='1'): lastOne now =1. leftZeros = 3 - 1 - 1 = 1 (zero at index2)
r=3: val=1, needed=0 => ans3 (substr [3,3])
r=4: val=1*2+0=2, L'=2, needed=0 => ans4 (substr [3,4]="10")
r=5? out of range. So ans=4. Good.
Now consider a more tricky case: s = "00110". n=5.
Indices:1:0,2:0,3:1,4:1,5:0.
Manual good substrings? Let's list quickly:
Single: pos3=1 good, pos4=1 good.
Length2: (3,4): "11" val=3 len2 -> not good (3!=2)
(2,3): "01" val=1 len2 no
(4,5): "10" val=2 len2 good? 10 is 2, len2 -> good. So (4,5) good.
Length3: (3,5): "110" val=6 len3 no
(2,4): "011" val=3 len3 good? "011" =3, len3 -> good. (1,3): "001" val=1 len3 no
Length4: (2,5): "0110" val=6 len4 no
(1,4): "0011" val=3 len4 no
Length5: whole "00110" val=6 len5 no
So good: (3,3),(4,4),(4,5),(2,4) -> 4.
Now algorithm:
0-based: [0]=0,[1]=0,[2]=1,[3]=1,[4]=0.
lastOne=-1.
i=2: leftZeros = 2 - (-1) -1 = 2 (zeros at indices0,1)
r=2: val=1 -> ans1 (substr [2,2])
r=3: val = 12+1=3, L'=2, needed=1, leftZeros=2 >=1 => ans2 (substr start = p - needed = 2-1=1 => indices1..3: s[1..3]="011"? Actually 0-index: 1->0,2->1,3->1 => "011" val=3 len=3? Wait we computed L'=2 (r=3, so L'=2?), careful: r=3, p=2, so L' = r-p+1 = 2. So substring from p to r is "11" (positions2,3) which is binary 3, length L'=2, needed=1 -> we count substring with start l = p - needed = 2-1=1, so substring indices 1..3 length=3, value ignoring leading zeros (starting at index2?) Actually the substring from 1 to 3: s[1]=0, s[2]=1, s[3]=1 => "011" val=3, length=3. So it's good. So ans2 corresponds to (2,4) in 1-index? 1-index: positions2-4? Actually indices1..3 in 0-index correspond to positions2-4 (since 0-index+1). That's (2,4) which we found good. Good.
r=4: val = 32+0=6, L'=3, needed=3, leftZeros=2 <3, not good; val=6 <=5? n=5, 6>5? Actually n=5, val=6 >5, but we check after computing val? We compute val=6, then if val>n break. So break. So from i=2 we got 2.
Now i=3: lastOne=2, leftZeros = 3 - 2 -1 = 0.
r=3: val=1 -> ans3 (substr [3,3])
r=4: val=1*2+0=2, L'=2, needed=0 -> ans4 (substr [3,4]="10")
r=5? out. So total 4. Good.
Now consider string with many zeros between 1's: s = "100001". n=6.
Indices:1:1,2:0,3:0,4:0,5:0,6:1.
Good substrings?
List:
Single: pos1=1 good, pos6=1 good.
Length2: (1,2): "10" val=2 len2 good, (5,6): "01" val=1 len2 no, (2,3):"00" val=0 no, etc.
Length3: (1,3): "100" val=4 len3 -> no, (4,6): "001" val=1 len3 no, (2,4): "000" no, (3,5): "000" no.
Length4: (1,4): "1000" val=8 len4 no, (3,6): "0001" val=1 len4 no.
Length5: (1,5): "10000" val=16 len5 no, (2,6): "00001" val=1 len5 no.
Length6: whole "100001" val=33? Actually binary 100001 = 33, len6 no.
Also maybe (1,? ) etc.
What about (2,6)? Already considered val=1 len5 not good.
Maybe (1,2) good, (6,6) good.
Also (1,? ) Could there be a substring starting at 1 with leading zeros? No leading zeros because start at 1 is 1.
What about substring (2,5)? "0000" val0 no.
(2,3)? "00" no.
(3,4)? no.
(4,5)? no.
(5,6)? "01" no.
(1,3) no.
So total good: pos1, pos6, (1,2) => 3? But also (??) check (2,6) we said val=1 len5 no. (3,6) val=1 len4 no. (4,6) val=1 len3 no. (5,6) val=1 len2 no. So 3.
Our algorithm:
0-based: [0]=1,[1]=0,[2]=0,[3]=0,[4]=0,[5]=1.
lastOne=-1.
i=0: leftZeros = 0 - (-1) -1 = 0.
r=0: val=1 -> ans1 (substr [0,0])
r=1: val = 12+0=2, L'=2, needed=0 -> ans2 (substr [0,1]="10")
r=2: val=22+0=4, L'=3, needed=1, leftZeros=0 <1 -> not good.
r=3: val=4*2+0=8, L'=4, needed=4, leftZeros=0 no, val=8 >6? n=6, break? val=8>6 so break after computing? Actually after r=2, val=4 <=6, continue. At r=3, val becomes 8 >6, break. So from i=0 we got 2.
i=5: lastOne=0, leftZeros = 5 - 0 -1 = 4 (zeros indices1-4)
r=5: val=1 -> ans3 (substr [5,5])
r=6? out. So ans=3. Good.
Now consider string where there is a long zero run before a '1', and the needed leading zeros exactly match leftZeros: works.
Now consider potential double-counting: Could the same substring be counted from two different p? Suppose substring has first 1 at p, but also could be considered with a different first 1? No, because first 1 is unique.
Now edge: substring that starts with '1' and has no leading zeros: counted by p = start index.
Now what about substring where the first 1 is at p, but also there is a '1' before p that is exactly at l? That would mean l = previous '1', but then that previous '1' would be inside substring, making it the first 1, contradiction. So not possible.
Thus, algorithm is sound.
Now we must ensure we use 0-index or 1-index consistently. In code we'll use 0-index.
Now check the condition needed <= leftZeros: leftZeros computed as i - lastOne - 1. But careful: For i=0 and lastOne=-1, leftZeros = 0 - (-1) - 1 = 0, correct.
Now confirm with a case where the string begins with zeros and then 1: leftZeros should count zeros before that 1. For i=2 (0-index) in "001", leftZeros = 2 - (-1) -1 = 2? Actually "001": indices 0:0,1:0,2:1. lastOne before index2 is -1, so leftZeros = 2 - (-1) - 1 = 2. That's correct, zeros at indices0,1.
Now what about subsequent 1's: Example "0101": indices:0:0,1:1,2:0,3:1. For i=3, lastOne=1, leftZeros = 3-1-1=1 (zero at index2). Good.
Now we need to make sure that for each p, we correctly handle r loop and val computation. Since we compute val iteratively, we must be careful: val starts at 0 before the loop, then for each r, update val = val*2 + (s[r]-'0'). That's correct.
Now we also need to break when val > n. However, there is a subtlety: The condition for goodness also includes that the total length L = val. If val > n, L = val > n, impossible because substring cannot have length greater than n. So break.
Now what about if val is huge but leftZeros might also be huge? But L would be > n, not possible. So break.
Now we need to ensure that we don't miss any substrings where val <= n but needed > leftZeros, but maybe there exists a different l that still yields the same length? No, l is determined.
Now let's test with a random brute for small n to be completely sure. But given reasoning and examples, it's likely correct.
Now one more nuance: The integer value is computed ignoring leading zeros, but the substring may have zeros after the first 1. That's fine.
Now note that the definition of f(t) says "decimal representation of integer t written in binary form (possibly with leading zeroes)". They gave f(000) = 0. So for a substring of all zeros, f=0. So we correctly ignore those because len >=1 cannot be 0.
Now, could there be a substring where the integer value equals length, but the substring contains multiple '1's and the first '1' is not at the start, and the leading zeros count is such that needed = val - L' is exactly the number of zeros before p. That's our condition.
Now I think it's correct.
Now implement solve() to read t, loop, for each string compute ans as above.
But wait: There is a catch: The substring length L must be exactly val, but we have used L = (p-l) + L' and set l = p - (val - L'). However, we must also ensure that the substring does not extend beyond the string? That's already covered because r is within string, and l computed is >= 0 and <= p, and since we require leftZeros >= needed, it ensures l >= 0? Actually leftZeros = i - lastOne - 1, which is at most i (since lastOne >= -1). But needed could be less than or equal to leftZeros, which ensures l = i - needed >= i - leftZeros = lastOne+1 >= 0. So l is valid index. So substring [l, r] lies within string.
But what about the possibility that l < 0? That would require needed > i, but leftZeros <= i, so if needed > leftZeros, we don't count. So safe.
Now, we also need to consider that the substring's length L = (r-l+1) must equal val. We derived l = p - (val - L') because L = (p-l) + L' = val => p-l = val - L' => l = p - (val - L'). So that's correct.
Now what about the requirement that the substring's first 1 is indeed at p. This is satisfied because we enforce that all positions from l to p-1 are zeros. Our condition leftZeros >= needed ensures that we can pick l such that those positions are zeros. However, we must also ensure that there is no '1' between l and p-1. Since leftZeros is the length of contiguous zeros immediately before p, and needed <= leftZeros means that from p - needed to p-1 is a subset of that zero-run, so no ones. Good.
But what if needed = 0? Then l = p, no leading zeros, so no issue.
Now one more nuance: The definition of good substring requires r-l+1 = f(s_l..s_r). The function f ignores leading zeros. So for a substring with first 1 at p, f(s_l..s_r) = integer value of s_p..s_r. That's what we used. So correct.
Now we also need to consider that the substring could have its first 1 at p, but there could be zeros before p but also there might be another '1' before p but outside the range? No, if there is a '1' before p and within the substring, that would be included because substring starts at l. If there is a '1' before p but l > that '1', then it's not in substring. But our method's leftZeros is computed based on distance to previous '1' (lastOne). If we set l = p - needed, and needed <= leftZeros, then l >= lastOne+1, meaning the previous '1' is at index lastOne, which is less than l? Actually lastOne is the index of the most recent '1' before p. Since leftZeros = p - lastOne - 1, we have p - needed >= p - leftZeros = lastOne + 1. So l >= lastOne + 1, which is strictly greater than lastOne. So the previous '1' is not included. So it's fine.
Now, is there any possibility that there is a '1' before lastOne? That doesn't matter because the substring doesn't include it. So good.
Now, we should also consider that the substring might have multiple '1's after p, that's fine.
Now, we can also consider optimizing by precomputing nextZero? Not needed.
Now let's consider potential off-by-one in the loop: For each p, we need to start val = 0. For r = p, we do val = 0*2 + s[p] => val = 1 if s[p]=='1'. That's correct.
Now, we need to ensure that we don't miss substrings where the substring has first 1 at p, but the integer value computed is such that needed is larger than leftZeros, but maybe there is a different choice of p? For instance, could there be a substring where we misinterpret which '1' is the first? No.
Now, I think we are done.
But I recall that some solutions for this problem (Codeforces problem "Good Substrings" maybe from Round #? Actually this problem appears to be Codeforces 1730B? Not sure) Often the solution is to iterate over possible lengths of significant part (up to 18) and use prefix sums or something. But our approach is similar to editorial: For each '1', consider up to 18 next bits.
Now, one more check: We used leftZeros = i - lastOne - 1. But suppose there are zeros before p but there is a '1' not immediately before but further left? The distance to lastOne is exactly the start of the zero run. So leftZeros is the maximum number of zeros we can prepend without including a '1'. That's correct.
Now, test a tricky case: s = "0100100"? Let's brute with small code mentally maybe not needed.
But I'd like to test an edge: where needed = leftZeros, and leftZeros = 0? That's okay.
Now, let's consider a case where the substring's length L = val, but the integer value is large, and the needed leading zeros is exactly leftZeros, but the zeros left of p are not contiguous all the way to the start because there is a '1' earlier? But leftZeros is defined as the number of consecutive zeros ending at p-1; if needed = leftZeros, then we need to include all those zeros. That would make l = p - leftZeros = lastOne + 1. So the substring starts right after the previous '1'. That's valid as long as that previous '1' is not inside the substring. So it's fine.
Now, what if needed < leftZeros? Then we can choose a smaller l (i.e., we don't need to use all zeros). That's also fine because the zeros we include are a suffix of the zero-run, which are all zeros.
Thus condition is exactly needed <= leftZeros.
Now, any other constraints? The substring must be within the string; l computed may be less than 0? Already handled.
Now, we must also ensure that the substring does not extend beyond the string? r is within string, and l computed is within string, so fine.
Now, test a scenario where needed > leftZeros but maybe we could include some of the zeros before the previous '1'? That would include the previous '1', making it the first 1, changing p. So that substring would be counted under a different p (the previous '1'). Indeed, if we try to extend left beyond lastOne+1, then the first '1' becomes that earlier one. So our enumeration under the earlier '1' would capture it, provided that substring with that earlier first 1 satisfies condition. So no double counting.
Thus, we have a partition of all substrings containing at least one '1' based on the position of the first '1'.
Now, what about substrings that have the first '1' at p, but also there is another '1' later, but we could also consider a substring that starts after p but before that later '1'? That would have first '1' at that later one, not p.
So indeed, each substring with at least one '1' has a unique first 1.
Therefore, algorithm is correct.
Now, let's ensure we are not missing substrings where the first 1 is at p, but there are zeros after p that are not included in the significant part? No, the significant part is always from p to r. So zeros after p are included in the substring as part of the binary number? Yes, they are part of the binary string; they affect the value.
Now, check if there could be a substring where the binary number's value when ignoring leading zeros equals length, but the substring includes trailing zeros after the last 1? That's fine; the value includes them because they are part of the binary representation. For example "10" value 2 includes trailing zero.
Now, all good.
Now time to implement.
Potential pitfalls: Using Python, ensure we don't have off-by-one in loops.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
t = int(data[0])
out = []
idx = 1
for _ in range(t):
s = data[idx]; idx+=1
n = len(s)
ans = 0
last_one = -1
for i,ch in enumerate(s):
if ch == '1':
left_zeros = i - last_one - 1
val = 0
# iterate r from i to end, break when val > n
for j in range(i, n):
val = (val << 1) | (ord(s[j]) - 48) # bit shift or val*2 + int
if val > n:
break
Lprime = j - i + 1
needed = val - Lprime
if needed <= left_zeros:
ans += 1
last_one = i
out.append(str(ans))
sys.stdout.write("\n".join(out))
Now, is there any scenario where val > n but the substring could still be good if we extend left? No, because L = val, so length would be > n impossible.
Now test with given examples.
But wait, we didn't handle the case where the substring contains only zeros? Already excluded.
Now, test with n=200000 all ones. The inner loop will run about 18 per i, total 3.6M, which is fine in Python? 3.6 million operations is fine (<0.1 sec). But we also have the outer loop scanning n, and for each '1' we do inner loop. So overall for sum n=200k, operations ~ 3.6M, which is fine. However, we must consider that in worst-case all characters are '1', the inner loop may do up to 18 iterations, but each iteration includes a multiplication and condition checks, which is fine.
But we need to be careful about Python's speed for large input. Since total sum is 200k, 3.6M operations is definitely okay.
Now, is there any more optimization? Not needed.
But there might be an off-by-one: left_zeros = i - last_one - 1. For i=0 and last_one=-1, left_zeros = 0 - (-1) - 1 = 0. Correct. For i after a previous one, e.g., last_one=0, i=2: left_zeros = 2-0-1=1. Works.
Now, what about the situation where there is a '1' and then zeros, and then another '1'? The left_zeros for the second '1' will be number of zeros between them. That's correct.
Now, consider a scenario: s = "1 0 1 0 0 1"? Works.
Now, we should also think about potential integer overflow in other languages but Python fine.
Now, I recall a Codeforces problem "1462E1" maybe not. Actually I think this problem is CF #1462 B? Not sure.
Now let's test with some random small strings manually by writing a brute force in mind maybe not needed.
But I want to ensure we don't miss substrings where the first 1 is not at the start and the integer value is large but needed leading zeros exactly equals leftZeros, but there are also zeros after p that affect val. We count those as we extend r.
Now, what about substrings that have first 1 at p, and val = L, but the substring may have leading zeros such that the first 1 is not the first character, but also there could be zeros after p that cause val to be large but length also large, maybe val - L' could be negative? As argued, it's non-negative. So ok.
Now, think about a scenario where the binary value includes leading zeros? But we ignore them; the first 1 is the start of the value. So the value is determined by bits from that first 1 onward. So if there are zeros between the first 1 and the end, they are part of the value. So it's fine.
Now, confirm with another example: s = "0010100" (n=7). Let's manually compute good substrings? But trust.
Now, one nuance: The problem's definition of f(t) says "decimal representation of integer t written in binary form (possibly with leading zeroes)". For example f(000) = 0. So indeed, they treat the string as a binary number, ignoring leading zeros. So our interpretation is correct.
Now, is there any possibility that the integer value of a substring could be interpreted differently if there are leading zeros and the substring is all zeros? Then f=0. We ignore.
Now, implement.
But we need to be careful: The sum of lengths is 200k, and t up to 1000. So reading input line by line is fine.
Now, I want to double-check the loop bounds: for j in range(i, n): we break if val > n. Since n is at most 200k, val can become large quickly. But consider the worst-case scenario where n is very small, say n=1. Then for i=0, val=1, which is <=1, we check, then j increments to 1? Actually range(i, n) goes from i to n-1 inclusive, so if n=1, i=0, range(0,1) includes j=0 only. So fine.
Now, if n=2, i=0, range(0,2) includes j=0,1. At j=0: val=1 <=2, check. j=1: val becomes 1*2+bit (depending). If bit=0, val=2 <=2, check; if bit=1, val=3 >2, break after computing? Actually we compute val = 2+1=3, then check if val > n: break, so we do not check condition for j=1. That's correct because if val=3, L would be at least 3 (since length = (p-l) + L' and we haven't considered l yet, but val = 3 > n=2, L would be 3 >2 impossible). So break.
Now, what about val exactly equals n? That's allowed.
Now, check: In our condition needed <= left_zeros, we didn't check that the resulting substring length L = val does not exceed n? But val <= n because we break when val > n. So it's fine. Also if val <= n but needed > left_zeros, not good. So that's it.
Now, can there be a scenario where val <= n but L = val would exceed n because of leading zeros? Actually L = val, so if val <= n, it's okay because the substring's length is val, which must be <= n. However, we must also ensure that the substring from l to r fits within the string. Since r is within the string and l computed is within string, the total length is exactly val, and we have val <= n, so it will fit as long as l >= 0. That is guaranteed by needed <= left_zeros (as argued). So fine.
Now, another nuance: Could there be a substring where val <= n, needed <= left_zeros, but the substring would extend beyond the string because r is fixed but l might be negative? Actually l = i - needed; since needed <= left_zeros and left_zeros <= i (since i - last_one -1 <= i), we have l >= i - i =0? Actually left_zeros can be up to i, but it's possible that left_zeros = i (if there are no previous ones and i zeros before? But i is the index, if s[0..i-1] are zeros and s[i]='1', then last_one = -1, left_zeros = i - (-1) - 1 = i. So left_zeros = i. So needed <= i ensures l = i - needed >= 0. So ok.
Now, if left_zeros = i, needed could be i, then l=0, valid.
Thus, condition is sufficient.
Now, we must also consider that the substring may include characters beyond r? No.
Thus algorithm is solid.
Now, let's also consider potential alternative interpretations: The problem might have been about substrings where the length equals the integer represented by the substring including leading zeros? But the examples clarify: f(011)=3, not 3? Actually "011" binary with leading zero is "11" which is 3. So they ignore leading zeros. Also f(000)=0. So our interpretation is correct.
Now, I recall some solutions use the fact that the number of good substrings is at most n * log n, which matches our approach.
Now, let's think about potential pitfalls in coding: We need to convert s characters to ints quickly. Using ord(s[j]) - 48 is fine. Or use int(s[j]) but that's slower. We'll use ord.
Now, we might want to use bit shifting: val = (val << 1) | (c). That's fine.
Now, we must be careful about the order: For each i, we reset val=0, then for j from i to n-1, we update val. Since val is recalculated from scratch, that's fine.
Now, we should also consider that we might double count? Already argued not.
Now, test with string of all zeros: Then no '1' encountered, ans remains 0. Correct.
Now, test with string "0": ans=0. Good.
Now, test with string "1": ans=1. Good.
Now, test with string "00": ans=0. Good.
Now, test with string "01": n=2. p=1 (index1) leftZeros = 1 - (-1) -1 = 1? Actually index1? Wait s[0]='0', s[1]='1'. For i=1, last_one initially -1, leftZeros = 1 - (-1) - 1 = 1. r=1: val=1, needed=0 => ans1 (substr [1,1] "1") r=2? out. So ans=1. But what about substring "01"? length2, val=1, not good. So ans=1, correct.
Now, test with string "10": we did ans=2.
Now, test with string "11": ans=2.
Now, test with string "1010": n=4. Let's compute manually: good substrings? Positions:1:1,2:0,3:1,4:0.
Substrings: "1"(1) good; "0"(2) no; "1"(3) good; "0"(4) no; "10"(1-2) val2 len2 good; "01"(2-3) val1 len2 no; "10"(3-4) val2 len2 good; "101"(1-3) val5 len3 no; "010"(2-4) val2 len3 no; "1010"(1-4) val10 len4 no. So good: 4? Actually we have (1,1),(3,3),(1,2),(3,4) => 4. Our algorithm:
i=0: leftZeros=0. r0: val1 -> ans1; r1: val2 -> ans2; r2: val5 -> L'=3, needed=2, leftZeros=0 -> no; val5>4? n=4, val5>4 break? Actually val5 >4, break after r2? At r2, val=5 >4, so break before counting? We compute val=5, then check if val>n: break. So we do not check r2 condition. So from i=0 we got 2.
i=2: last_one=0, leftZeros = 2-0-1=1. r2: val=1 -> ans3; r3: val=1*2+0=2 -> L'=2, needed=0 -> leftZeros>=0 -> ans4; r4 out. So ans=4. Good.
Now, test a longer string: s = "001001". n=6. Let's compute manually? Possibly good substrings: p=2? Actually positions 1:0,2:0,3:1,4:0,5:0,6:1.
Our algorithm:
i=2 (0-index?) Actually indices:0:0,1:0,2:1,3:0,4:0,5:1.
last_one=-1 -> i=2: leftZeros=2-(-1)-1=2.
r=2: val=1 -> ans1
r=3: val=2 (10) L'=2 needed=0 -> ans2 (substr [2,3]="10"? Actually indices2-3: "10"? s[2]=1, s[3]=0 -> "10" val2 len2 good)
r=4: val=22+0=4, L'=3 needed=1 leftZeros=2>=1 -> ans3 (l=2-1=1, substring indices1..4: s[1]=0,s[2]=1,s[3]=0,s[4]=0 => "0100" val4 len4? Actually length=4, val=4, good)
r=5: val=42+0? Actually s[5]=1? Wait index5 is 1, but r=5? Sequence: r=4 gave val=4, next r=5: val=4*2 + s[5]=8+1=9, L'=4, needed=5? val9 -4 =5, leftZeros=2<5, not good; val=9>6? n=6, break? val=9>6, break. So from i=2: ans=3.
i=5: last_one=2, leftZeros =5-2-1=2 (indices3,4 zeros)
r=5: val=1 -> ans4
r=6? out. So total ans=4.
Now list good substrings manually:
"1" at pos3.
"10" at pos3-4 (positions3-4? Actually indices2-3 correspond to positions3-4? Let's map: index2->pos3, index3->pos4. So "10" good.
"1" at pos6 (index5) good.
Maybe also "100"? Let's check substring "100" at positions3-5? indices2-4: s[2]=1,3=0,4=0 => "100" val=4, length=3 -> not good. "001"? etc. So 4 good. Good.
Now consider a case where needed = leftZeros and leftZeros > 0, but the substring might also include some zeros after p that cause val to be exactly length. Already covered.
Now, one more nuance: The condition needed <= leftZeros ensures that l is within string and zeros are present. However, we also need to ensure that the substring we count does not have any '1' before p? That's satisfied.
Now, could there be a substring where the first '1' is at p, but there is a '1' between l and p that is zero? Actually if there is a '1', then it's not zero. So we ensure no '1'.
Now, I think we can implement.
But wait, I recall a known Codeforces problem: "Good Substrings" from Codeforces Round #1462 (Div. 1)? Actually problem 1398C? No, there is a problem "Good Substrings" where you count substrings with numeric value equal to length, and solution involves iterating over 1's and checking up to 20 characters. That's exactly this.
Now, potential issue: In the condition needed = val - L', we treat L' = r-p+1. However, we should confirm that the length of the substring L is indeed (p-l) + L'. Since l = p - needed, then L = (p - (p - needed)) + L' = needed + L' = val. So correct.
Now, could there be a scenario where needed = val - L' is negative? As argued, val >= L'? Let's check: val = integer value of binary string starting with 1 of length L'. The minimum value is 2^{L'-1}. For L'=1, min=1 => val - L' = 0. For L'=2, min=2 => val - L' = 0. So val >= L' always? Let's test L'=2, val could be 3 => 3-2=1 >0. For L'=3, min=4 => 4-3=1 >0. So val - L' >= 0 always. So needed is non-negative.
Now, we must also consider the case where the substring's integer value is 0 (all zeros). But then first 1 doesn't exist, and our loop over '1's wouldn't capture it. Could a substring of all zeros be good? Then val=0, length L must be 0, impossible. So not good.
Now, we are done.
Now, to be thorough, we should test with some random strings of length up to maybe 10 and compare with brute force. Since we are reasoning, it's okay.
Now, regarding input reading: The first line is t, then t lines each contain a string. There might be empty lines? Usually not.
Now, output each answer on new line.
Now, we should consider the possibility that the sum of lengths is 200k, and t=1000, many small strings. Our algorithm per string is O(len * maxbits), which is fine.
Now, finalize solution.
But I just realized something: In our inner loop, we break when val > n. However, suppose val becomes > n at some r, but we might have already counted some substrings for larger r? No, we break, which is correct.
But could there be a scenario where val <= n at r, but then for next r, val becomes > n, we break. That's correct.
Now, is it possible that for some p, val becomes > n, but later for a much larger r, val could become <= n again? No, because val is strictly increasing as we extend r to the right (since we are appending bits, the binary number increases). Multiplying by 2 and adding 0 or 1 yields val' >= 2*val > val if val>0. So monotonic. So break is safe.
Now, one more nuance: We are using n = len(s). But the maximum allowed substring length is n, and val must be ≤ n. So we break when val > n. However, what if n is very small, say n=1, then for p=0, val=1 <=1, we count. For r=1 doesn't exist. Good.
Now, we must ensure we treat the string as binary number with possible leading zeros correctly. In our computation of val, we start from the first 1, so no leading zeros in the considered bits. That's correct because the value is determined by the bits from the first 1 onward. So we are good.
Now, implement solve function accordingly.
Potential optimization: Use precomputed array of nextOne to skip? Not needed.
Now, code.
Now, I'll think about potential pitfalls in Python due to large recursion? No.
Now, test with provided examples.
Now, maybe there is an even simpler method: For each position i, we can consider substrings starting at i, but we chose per first 1 approach, which is fine.
Now, before finalizing, let's search memory: I recall a Codeforces problem 1730B might be "Good Substrings" but I'm not sure. However, many participants used this method.
Now, I'll produce final answer.
But I want to double-check with a tricky case: s = "1000000000" (one followed by many zeros). n=10.
Our algorithm: p=0, leftZeros=0.
r=0: val=1 -> ans1
r=1: val=2 (10) -> L'=2 needed=0 -> ans2 (substr [0,1]="10")
r=2: val=4 (100) -> L'=3 needed=1, leftZeros=0 -> not good, val=4<=10 continue
r=3: val=8 -> L'=4 needed=4 -> not good
r=4: val=16 -> L'=5 needed=11 -> not good, val=16>10? Actually val=16>10, break at r=4? At r=4, val becomes 16 >10, break. So ans=2.
But are there any other good substrings? Consider substring starting at position 2? That would have first 1 at position 0? Actually if we take substring from index 1 to index something that includes the leading 1? Let's think: substrings that contain the '1' at position 0 but with leading zeros? Actually there are no leading zeros because the first character is 1. So only substrings starting at 0 are possible with first 1 at 0. But also substrings that start after 0, e.g., from index 1 to something, but those substrings contain no '1's because after index0 all zeros. So they are all zeros, not good. So total good =2. That seems plausible: "1" and "10". Check: "100" length3 val4 not good; "1000" length4 val8 not good; etc. So ans=2 correct.
Now consider s = "0001000000" (n=10, 1 at position4). p=3 (0-index). leftZeros = 3 - (-1) -1 = 3 (since no previous 1).
Compute:
r=3: val=1 -> ans1 (substr "1")
r=4: val=2 (10) L'=2 needed=0 -> ans2 (substr "10" at positions 3-4)
r=5: val=4 (100) L'=3 needed=1 leftZeros=3>=1 -> ans3 (l=2, substr indices2-5: s[2]=0,s[3]=1,s[4]=0,s[5]=0 => "0100" val=4 len4 good)
r=6: val=8 (1000) L'=4 needed=4 leftZeros=3<4 -> not good; val=8<=10 continue
r=7: val=16 -> L'=5 needed=11, leftZeros=3 no; val=16>10 break? Actually val=16>10 break. So ans=3.
But maybe there is also substring starting earlier? For instance, could there be a substring from index 1 to 6? That would have first 1 at index3, leading zeros count = 2 (indices1-2), length = 6, val=8? Actually substring indices1..6: s[1]=0,s[2]=0,s[3]=1,s[4]=0,s[5]=0,s[6]=0 => "001000" val=8, length6 not good. From index0 to 6: length7 val=8? "0001000" val=8, length7 not good. So only those we counted. So ans=3 correct.
Now consider s = "101001" (n=6). Let's compute manually maybe: indices0:1,1:0,2:1,3:0,4:0,5:1.
We'll run algorithm:
p=0: leftZeros=0.
r=0: val=1 -> ans1
r=1: val=2 -> L'=2 needed=0 -> ans2 (10)
r=2: val=5 (101) -> L'=3 needed=2 leftZeros=0 -> not good; val=5 <=6 continue
r=3: val=10 (1010) -> L'=4 needed=6 leftZeros=0 -> no; val=10>6 break? Actually 10>6 break.
p=2: last_one=0, leftZeros = 2-0-1=1 (zero at index1)
r=2: val=1 -> ans3
r=3: val=2 (10) L'=2 needed=0 -> ans4 (substr "10" indices2-3? Actually s[2]=1,s[3]=0 => "10" length2 val2 good)
r=4: val=4 (100) L'=3 needed=1 leftZeros=1>=1 -> ans5 (l=2-1=1, substring indices1..4: s[1]=0,s[2]=1,s[3]=0,s[4]=0 => "0100" val=4 len4 good)
r=5: val=9? (1001) Actually val=4*2+1=9, L'=4 needed=5, leftZeros=1<5; val=9>6 break? 9>6 break.
p=5: last_one=2, leftZeros = 5-2-1=2 (indices3,4 zeros)
r=5: val=1 -> ans6
r=6? out.
Total ans=6.
Now, list good substrings manually? Let's enumerate all substrings with at least one 1 and check if length equals value.
We'll list all substrings of s="101001" (positions1..6).
I'll list systematically maybe in mind or quick: Use brute mental? Let's attempt.
Indices (1-indexed):
1:1 -> good
2:0 -> no
3:1 -> good
4:0 -> no
5:0 -> no
6:1 -> good
Length2:
1-2:10 -> good (2)
2-3:01 -> val1 len2 no
3-4:10 -> good (2)
4-5:00 -> no
5-6:01 -> val1 len2 no
Length3:
1-3:101 -> val5 len3 no
2-4:010 -> val2 len3 no? 010 binary =2, len3, not good
3-5:100 -> val4 len3 no
4-6:001 -> val1 len3 no
Length4:
1-4:1010 -> val10 len4 no
2-5:0100 -> val4 len4 -> good? "0100" val=4, length=4 -> yes good
3-6:1001 -> val9 len4 no
Length5:
1-5:10100 -> val20 len5 no
2-6:01001 -> val9? "1001" =9, len5 no
Length6:
1-6:101001 -> val? binary 101001=41? Actually 101001_2 = 32+8+1=41, len6 no.
Also maybe some other good? Check (1,2) we counted, (3,4) counted, (2,5)? That's length4 "1000"? Actually 2-5: positions2-5: 0,1,0,0 =0100 val4 len4 good, we have that. (1,1),(3,3),(6,6) counted. Also (2,4)? That's 010? length3 val2 no. (4,6) 001 length3 val1 no. So good substrings: (1),(3),(6),(1,2),(3,4),(2,5) -> that's 6. So matches our ans.
Thus algorithm seems consistent.
Now, check if there is any good substring that our method might miss because the first 1 is not at the earliest possible? No.
Now, I'm confident.
But wait: There is a subtle nuance: When we compute leftZeros as i - last_one - 1, we are assuming that the zeros immediately before i are contiguous and there is no '1' in between. That's correct because last_one is the most recent '1'. However, we might need to consider that the substring could start after last_one but before i, and include some of those zeros. That's allowed as long as it doesn't include the last_one. Our leftZeros is the maximum number of zeros we can include without including a '1'. So it's correct.
Now, think about a scenario: Suppose we have s = "101". For p=2 (index2, the last '1'), leftZeros = 2 - 0 - 1 = 1 (zero at index1). That's correct. Substrings with first 1 at index2 can start at index2 (needed=0) or index1 (needed=1?). For needed=1, would correspond to substring from index1 to 2? That's "01"? But that substring's first 1 is at index2? Actually substring indices1..2: s[1]=0, s[2]=1, first 1 is at index2, yes. So condition needed=1 => val - L' =? For r=2, val=1, L'=1, needed=0. For r maybe larger? If we consider r=2 only, val=1, needed=0 only. So cannot have needed=1. For r=3? r=3 (index3) doesn't exist. So only needed=0. So leftZeros=1 but needed=0, so counted. So ok.
Now, for p=0, leftZeros=0, etc.
Now, let's consider a substring with first 1 at p and with leading zeros, but there is also a zero immediately after p that might cause val to be such that needed is exactly the number of zeros before p but also the substring could be extended left to include some zeros that are not part of the contiguous zero run? But they are part of the contiguous zero run; if needed is less than leftZeros, we can choose l accordingly, always staying within the zero run. So it's fine.
Now, maybe we should also consider the possibility that the substring could start before the previous '1'? That would make the first 1 the previous '1', not p. So it would be counted under that previous '1'. So not missed.
Thus algorithm is correct.
Now, about complexity: For each i where s[i]=='1', inner loop runs up to O(log n) iterations, so overall O(n log n). With n sum 2e5, it's fine.
Now, let's implement.
But before final code, we should consider that in Python, using for j in range(i, n): and break when val > n may cause many iterations if n is large but val grows slowly? As argued, val grows at least doubling each step, so at most ~18-20 steps. So it's okay.
However, we must be careful: What if n is up to 200,000 and the bits after p are all zeros? Then val doubles each step, so after 18 steps val = 2^{17} = 131072, after 19 steps = 262144 > n. So at most 19 steps. If bits are ones, growth is even faster. So max steps per p is about 19. So it's fine.
Now, for each p, we recompute val from scratch, which is O(L') operations, but that's fine.
Now, we might want to break early also if L' becomes large? Already handled by val > n break.
Now, we must also consider that the string length could be up to 200k, but we have to answer t up to 1000 queries. Sum lengths is 200k, so many strings may be small. Our code loops over each character and then for each '1' does up to 20 steps. So total steps across all queries <= sum_lengths * 20 = 4e6, which is fine.
Now, finalize.
But I recall a similar problem where the solution also needed to consider that the value might exceed the length early and then later come back? Not possible because val strictly increases with longer substring.
Now, I'm done.
Now, I'll produce final answer.
But we should also consider possible memory issues: none.
Now, code as described.
Now, we might want to micro-opt: Use local variables for speed, maybe use list of ints for s.
But it's okay.
Now, implement solve().
Now, test with provided sample input.
Sample Input:
4
0110
0101
00001000
0001000
Our algorithm should output 4,3,4,3.
Now, produce final answer.
Now, write explanation for solution.
I'll include comments.
Now, final code:
import sys
def main():
data = sys.stdin.read().split()
t = int(data[0])
out = []
pos = 1
for _ in range(t):
s = data[pos]; pos+=1
n = len(s)
ans = 0
last_one = -1
for i,ch in enumerate(s):
if ch == '1':
left_zeros = i - last_one - 1
val = 0
for j in range(i, n):
val = (val << 1) | (ord(s[j]) - 48)
if val > n:
break
Lprime = j - i + 1
needed = val - Lprime
if needed <= left_zeros:
ans += 1
last_one = i
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
main()
Now, we could also convert s to a list of ints once: s = [int(c) for c in s] to avoid ord each time. But ord is fine.
Now, final solution is ready.
Now, ensure that we handle possible leading/trailing spaces. Using split() reads all tokens, which will separate lines properly.
Now, done.
Now, produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
t = int(data[0])
out = []
idx = 1
for _ in range(t):
s = data[idx]
idx += 1
n = len(s)
ans = 0
last_one = -1 # index of the previous '1' (0‑based)
code
for i, ch in enumerate(s):
if ch == '1':
# number of consecutive zeros immediately before i
left_zeros = i - last_one - 1
val = 0
# extend the substring to the right, at most ~18 steps
for j in range(i, n):
val = (val << 1) | (ord(s[j]) - 48) # binary value of s[i..j]
if val > n: # length would exceed the whole string
break
Lprime = j - i + 1 # length of the significant part
need = val - Lprime # required leading zeros
if need <= left_zeros:
ans += 1
last_one = i
out.append(str(ans))
sys.stdout.write("\n".join(out))