Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
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 number of substrings (contiguous intervals [l, r]) such that within that substring there exists a triple of indices forming an arithmetic progression of length 3 (i.e., x, x+k, x+2k) with the same character (0 or 1). In other words, the substring contains a "3-term arithmetic progression" with equal symbols.
We are given binary string s length up to 300k. Need to count intervals that contain at least one such triple.
Let n = |s|.
We can think about complement: count total intervals (n*(n+1)/2) minus number of intervals that contain NO such triple. Then answer = total - bad.
But counting intervals with no "good triple" might be challenging. However note condition: existence of x,k with 1 <= x,k <= n, l <= x < x+2k <= r, and s_x = s_{x+k}=s_{x+2k}. This is essentially that the substring contains any three equally spaced positions with same bit.
Observation: For a binary string, by Van der Waerden type? Actually, it's known that any binary string of length 5 must contain such a progression? Not exactly. Let's think: It's known that any 5-bit binary string contains an arithmetic progression of length 3? That's false: For example "01010" of length 5: positions 1,2,3,4,5. Check possible x,k: for k=1: check triples consecutive: need three equal in a row. Not present. k=2: positions (1,3,5) => 0,0,0? Actually "01010": positions 1=0,3=0,5=0 -> yes! So that's a progression! So length 5 always? Check "00110"? Let's systematically: For any 5 binary bits, there is an arithmetic progression of length 3? Positions: 1,2,3,4,5. Possible triples: (1,2,3) k=1; (2,3,4); (3,4,5); (1,3,5) k=2. So we need to avoid both patterns. Is it possible to avoid all? Let's try to construct: To avoid consecutive triples of equal bits, we must alternate bits? But if we alternate 01010, then (1,3,5) are all 0, that's a progression. If we do 00101? Check: s = 0,0,1,0,1. Triples: (1,2,3): 0,0,1 not all equal; (2,3,4): 0,1,0; (3,4,5): 1,0,1; (1,3,5): 0,1,1 not equal. So no triple? Check (1,2,3) no; (2,3,4) no; (3,4,5) no; (1,3,5) 0,1,1 not all equal. So that's a length 5 with no such triple. So not forced at length 5. What about length 6? Possibly all strings of length > something must contain? Actually it's known that the van der Waerden number W(2,3)=9? Wait, van der Waerden number W(r,k) is smallest N such that any r-coloring of {1,...,N} contains a monochromatic arithmetic progression of length k. For 2 colors and length 3, W(2,3)=9. That means any binary coloring of length 9 contains a monochromatic arithmetic progression of length 3. But note: our progression is with indices from 1..n, but we only consider triples x, x+k, x+2k such that x+2k <= n. That's exactly arithmetic progression of length 3 within [1,n]. So the van der Waerden number says: For n >= 9, any binary string of length n must contain a monochromatic arithmetic progression of length 3. But careful: van der Waerden number W(2,3) is known to be 9. That means for any 2-coloring of {1,2,...,9}, there is a monochromatic arithmetic progression of length 3. So any string of length 9 must contain such triple. But note: we consider substrings [l,r] of the original string. So if an interval length >= 9, it must contain a triple? Not exactly: The van der Waerden number applies to any set of integers of size >=9 with the natural order. The interval [l, r] of length L = r-l+1 corresponds to indices {1,...,L} after relabeling. So if L >= 9, then any binary string of length L must contain a monochromatic arithmetic progression of length 3. Therefore any substring of length >=9 is automatically "good". So the "bad" intervals (with no triple) can only have length <= 8. Wait, is that true? van der Waerden number says: any 2-coloring of {1,...,9} contains a monochromatic AP of length 3. So if we take a substring of length 9 (i.e., indices l..l+8), it corresponds to positions 1..9 after shifting. So it must contain a triple. So any substring of length >=9 is good. So the only substrings that could be bad are those with length <= 8. But careful: van der Waerden number is about the whole set {1,...,N}. So yes, any 2-coloring of an arithmetic progression of N consecutive integers contains a monochromatic AP of length 3 if N >= 9. So indeed, any interval of length 9 or more is automatically good. That greatly simplifies the problem! Because then we only need to count intervals of length <=8 that are good, plus all intervals of length >=9 are good. So total good intervals = total intervals - number of bad intervals of length <=8.
But is van der Waerden number exactly 9? Yes, W(2,3) = 9. I recall that the known values: W(2,3)=9, W(2,4)=35, etc. So indeed.
But need to confirm: The condition is that there exists some x,k with l ≤ x < x+2k ≤ r. That is an AP of length 3 with difference k > 0. The van der Waerden number covers all k (including k=1,2,...). So yes.
Thus any substring of length at least 9 is good.
Now we only need to count bad intervals of length at most 8. But careful: Are there any intervals of length <9 that might still be good? Yes, many are good; we need to subtract only the ones that are bad (i.e., contain no triple). So we can count all intervals total, then subtract those that are bad. Bad intervals can only be of length ≤8 (since length ≥9 always good). So we can enumerate all intervals with length ≤8, check if they contain a triple, and count those that don't. Then answer = total - bad.
Total intervals = n*(n+1)/2.
Now we need to count number of intervals [l, r] (1-indexed) with length L = r-l+1 ≤ 8 and such that there is no triple (x, x+k, x+2k) within [l,r] (with indices in [l,r]) with equal bits.
Since n up to 300k, enumerating all intervals with length ≤8 is O(8n) which is fine. But we must be careful: For each interval we need to check existence of triple. Could be O(L^3) naive, but L ≤8, so maximum 8^3=512 per interval, but there are up to 8n ≈ 2.4M intervals, total operations up to ~1.2B which might be borderline but perhaps okay in C++? But we can optimize: Precompute something? But given constraints, 2.4M * maybe up to ~ (8 choose combos for triples) maybe 100 checks each, would be ~240M operations, which might be okay in optimized C++ but in Python maybe too slow. We need to implement in Python? The problem likely expects efficient solution; but we can use the van der Waerden insight to reduce to O(n * constant). For each possible starting l, we can slide windows up to length 8 and check. But we still need to check triples efficiently.
We can also note that length ≤8, we can precompute all possible binary strings of length up to 8 and precompute whether they contain a triple. Then for each interval, we can convert its substring to a bitmask and look up. But there are only 2^8=256 possible strings of length exactly 8, but we have variable lengths (1 to 8). So we can precompute for all strings up to length 8: but we would need to map substring to an integer quickly. We can precompute prefix bits perhaps use bitmask for each position? Since length ≤8, we can extract substring and compute its integer representation quickly using bit operations if we precompute something. But simpler: just for each interval, we can check all triples by loops; it's small. n=300k, total intervals of length <=8: For each start l, there are min(8, n-l+1) intervals. So total intervals = sum_{i=1}^n min(8, n-i+1) ≈ 8n - 28 (since sum from 1 to 8? Actually for n large, first n-8+1 starts have 8 intervals, last 7 have decreasing. So total ≈ 8*(n-7) + (8+7+...+1) = 8n - 56 + 36 = 8n -20? Let's compute exactly: For n >=8, total intervals with length <=8 = 8n - 28? Let's derive: Number of intervals length exactly L: n-L+1. Sum L=1 to 8: sum_{L=1}^{8} (n-L+1) = 8n + sum_{L=1}^{8} (1-L) = 8n + sum_{L=1}^{8} (1) - sum_{L=1}^{8} L = 8n + 8 - (8*9/2) = 8n + 8 - 36 = 8n - 28. So total ~ 8n -28. For n=300k, that's about 2.4 million intervals. Checking each interval: worst-case we need to examine all triples (x, x+k, x+2k) within that interval. For a length L, number of possible (x,k) with x+2k ≤ r. For fixed L, number of triples is about L^2/2? Actually count: For each start x from 1 to L, for each k such that x+2k ≤ L, i.e., k ≤ (L-x)/2. So total number = sum_{x=1}^{L} floor((L-x)/2) ≈ L^2/4. For L=8, max number = sum_{x=1}^{8} floor((8-x)/2). Let's compute: x=1: floor(7/2)=3; x=2: floor(6/2)=3; x=3: floor(5/2)=2; x=4: floor(4/2)=2; x=5: floor(3/2)=1; x=6: floor(2/2)=1; x=7: floor(1/2)=0; x=8: floor(0/2)=0. Total = 3+3+2+2+1+1=12. So at most 12 triples to check per interval. That's tiny. So total operations ~ (2.4e6 * 12) ≈ 28.8e6 checks, each check just comparing three bits. That's absolutely fine in Python. So we can just brute-force.
But wait: There's also the possibility that some intervals of length >8 could be bad? No, van der Waerden says any 2-coloring of 9 numbers has a monochromatic AP of length 3. So length >=9 implies good. So the only bad intervals are length <=8. So we just count those.
Thus solution: read string s of length n. total = n*(n+1)//2. bad = 0. For each l from 0 to n-1 (0-indexed), for length L from 1 to min(8, n-l): r = l+L-1. Check if substring s[l:r+1] contains a triple. If not, bad += 1.
But careful: Need to be sure about correctness: Is van der Waerden number W(2,3) = 9? Let's confirm via known results: The van der Waerden number W(r,k) is the smallest N such that any r-coloring of {1,...,N} contains a monochromatic arithmetic progression of length k. For r=2, k=3, indeed W(2,3)=9. This can be verified: There is a 2-coloring of {1,...,8} with no monochromatic 3-term AP: e.g., 0 0 1 1 0 1 0 1? Actually known: The longest binary string without a monochromatic AP(3) is of length 8. One example: 00100111? Let's find: It is known that the maximal length is 8. Classic coloring: 0 0 1 1 0 1 0 1 (or 0 0 1 0 1 1 0 1). So indeed length 9 forces a triple. So our reasoning holds.
Edge cases: n < 9. Then all intervals length <= n <=8, so we need to count bad intervals (those with no triple) and subtract from total. So the enumeration over all intervals up to length 8 covers everything. So we can just do enumeration for all intervals regardless, but we must be careful that for n > 8 we also consider intervals of length >8? Actually we will only iterate lengths 1..8, so we will not count intervals length >8. But for n > 8, those length >8 intervals are all good, so they should be counted as good. Our total includes them, and we subtract bad (only intervals length <=8 that are bad). So that works.
Thus algorithm:
Input s, n = len(s).
total = n*(n+1)//2.
bad = 0.
For i in range(n):
for L in range(1, min(9, n - i + 1)? Actually careful: i is start index (0-index). max length = min(8, n-i) ??? If i is start, maximum length is n - i. So we want lengths 1..min(8, n-i). But we need length up to 8 inclusive. So L from 1 to min(8, n-i). Because n-i is number of chars from i to end, so length = L, where L max = n-i. So we loop L = 1 to min(8, n-i). For each L, compute r = i+L-1. Check if substring from i to r contains a triple.
Check triple: For x from i to r-? Actually indices within substring: we can use absolute indices. For all possible x (start index) from i to r, for k >=1 such that x+2k <= r, check if s[x] == s[x+k] == s[x+2k]. If found, then good; else after checking all, it's bad.
So for each interval, if no triple found, increment bad.
Finally answer = total - bad.
Complexities: O(n * 8 * (max triple checks per length ~12)) = O(96 n) ≈ 28.8M for n=300k, which is fine.
But is there any subtlety about the bounds: k can be any positive integer, but we restrict to those such that x+2k <= r. That's exactly what we do.
Double-check examples:
Example1: s=010101, n=6. Total=21. Need answer=3. So bad should be 21-3=18. So there should be 18 bad intervals. Let's manually compute? Possibly. We'll test with algorithm later.
Example2: s=11001100, n=8. Total=36. Answer=0, so bad=36. That means all intervals are bad (i.e., no triple in any substring). Since length <=8, all intervals are length <=8 (since n=8) and none contain triple. So the entire string must be triple-free. We can test: 11001100: Check triples: For the whole string (length 8), does it have a triple? According to van der Waerden, length 8 can be triple-free. So it's plausible. So answer 0. So algorithm would count all intervals as bad, subtract from total, get 0.
Edge: n=9, any string must have a triple in the whole string, but there might be substrings of length <=8 that are bad. So answer = total - bad, where bad counts only those short intervals without triple. But is there any substring of length 9 that could be bad? No, by van der Waerden, any length 9 substring is good. So algorithm includes those as good automatically.
Thus algorithm correct.
But wait: Are we sure about the condition "1 ≤ x, k ≤ n" and "l ≤ x < x + 2k ≤ r"? The definition of x and k is that they are integers, with constraints 1 ≤ x,k ≤ n. However, note that x,k are between 1 and n inclusive, not necessarily that x+k and x+2k are ≤ n? Actually they also require l ≤ x < x+2k ≤ r, which implies x+2k ≤ r ≤ n, so automatically x+2k ≤ n, and similarly x+k ≤ n. So effectively we only consider triples fully inside the substring and also satisfying x,k within 1..n, but that's automatically satisfied if indices are within 1..n. Since we are only checking within [l,r] which is subset of [1,n], we are fine.
Now need to implement efficiently. Use 0-indexing in Python: indices 0..n-1. But careful with condition: s[x] == s[x+k] == s[x+2k]. x from l..r-2? Actually need x+2k <= r. So we need to iterate k from 1 to (r-x)//2. So for each start x in [i, r], for k in range(1, (r-x)//2 + 1): if s[x] == s[x+k] == s[x+2*k]: then found.
We could also early break upon finding a triple. That's fine.
But note: For a given interval, we are checking all triples. Since intervals are small, it's okay.
Alternatively, we can precompute all triples (x,k) that satisfy equality. Then for each interval, we need to check if there exists such triple entirely within it. That could be faster using prefix sums or segment tree? But not needed.
Given that n=300k, but total intervals to check is ~2.4M, each with up to ~12 triple checks, it's fine.
Let's implement.
But we need to be careful about Python speed: 2.4M * up to 12 loops = 28.8M iterations, plus inner loops overhead. That is okay for Python within 2 seconds? Possibly borderline but likely fine. However, we can optimize a bit: For each start i, we can iterate length L up to 8, but we can break earlier if we find that a longer interval would be good? Not needed.
We can also combine the triple check with incremental building: as we extend the interval one character at a time, we can check new triples that include the new character. That might reduce constant factor. But simpler is fine.
Let's approximate: 2.4M intervals; each interval check may involve up to ~ (L^2)/4 triple checks. For L=8, it's 12, for L=7 it's about 9, for L=6 it's about 6, etc. So average maybe 6-7. So total triple checks ~ 2.4M*7 = 16.8M. That's well within Python's capacity if coded efficiently (maybe 0.2-0.3 seconds per 10M simple operations). Should be okay.
But we need to also consider the outer loops: for i in range(n): for L in 1..min(8, n-i): That's about 8n iterations. So total loop iterations ~ 2.4M. Inside each, we do a nested loop over x and k. But we can possibly precompute for each interval the status quickly by using a precomputed table of all substrings of length up to 8? That could reduce inner loops to just a table lookup. There are only 2^8 = 256 strings of length exactly 8, but we have variable lengths. We can precompute for every possible binary string of length up to 8 (by enumerating all bitmasks of length 1..8) whether they contain a triple. Then for each interval we need to compute its integer representation quickly. Since we are scanning consecutive intervals, we can compute a rolling bitmask. For example, for each start i, we can maintain a mask of up to 8 bits as we extend L. Starting with mask = 0, for L=1..min(8, n-i): mask = (mask << 1) | (int(s[i+L-1])). Then use precomputed boolean array good_mask[mask][L]? But careful: For length L, the mask should represent exactly L bits, but if we shift left and add new bits, the mask may have leading zeros from earlier positions? Actually if we treat mask as bits from oldest to newest: Suppose we start with mask=0, then for L=1: mask = (0<<1) | bit = bit. That's fine. For L=2: mask = (bit1<<1) | bit2. So the mask for length L is exactly the integer whose binary representation corresponds to the substring bits. However, the integer representation depends on the order: Most significant bit corresponds to the first character. That's okay, because we can map to precomputed status for that exact bit pattern of that length. But we need to store for each possible mask and length L whether it contains a triple. Since mask values can go up to 2^8 -1 =255. For each length L from 1 to 8, we can have mask in [0, 2^L -1]. We can precompute an array good[L][mask] (or just a dictionary). The total number of entries is sum_{L=1}^{8} 2^L = 510. That's trivial.
Then for each interval, we compute mask and L, look up precomputed status, and if not good, increment bad. This avoids any triple checking per interval. That would be even faster: O(n * 8) with just bit operations and table lookup.
Let's do that.
Implementation steps:
Input s as string. n = len(s). Convert s to list of ints bits = [1 if ch=='1' else 0 for ch in s].
Precompute an array is_bad[L][mask]? Actually we need to know if the substring (represented by mask of length L) contains any triple (i.e., is "good" or "bad"). We'll compute a boolean has_triple[mask, L]. For all L 1..8 and mask 0..2^L-1, we compute if there exists x,k with 0 <= x < L, k>=1, x+2k < L, and bits (extracted from mask) at positions x, x+k, x+2k equal. Bits: For position j (0-index within substring), the bit is (mask >> (L-1 - j)) & 1 if we consider MSB as first char. But easier: we can generate the string of length L from mask. Since L <=8, we can simply loop over x and k as before.
We'll precompute an array bad[1..8][mask] = True if no triple (i.e., bad interval). Or we can compute good. We'll store good[mask] for each L? Actually we need separate for each length because the mask representation depends on L (leading zeros are part of pattern). But we can compute for each L and mask.
Simplify: We'll create a dictionary or list of lists: good[L][mask] = bool (True if contains triple). Then for an interval of length L, we compute mask and check if not good[L][mask] then it's bad.
Compute good[L][mask] as:
For each mask from 0 to (1<<L)-1:
Extract bits: arr = [(mask >> (L-1-i)) & 1 for i in range(L)] or just by shifting.
For x in range(L):
for k in range(1, (L-1-x)//2 + 1):
if arr[x] == arr[x+k] == arr[x+2*k]: then good=True; break.
store.
We'll do this offline before processing the string.
Then iterate over start i from 0 to n-1:
mask = 0
max_len = min(8, n-i)
for L in range(1, max_len+1):
# add next char
mask = (mask << 1) | bits[i+L-1]
if not good[L][mask]:
bad += 1
Note: When we shift left and add, mask will contain exactly L bits, because we only have added L bits and shifted accordingly. For L=1: mask = 0<<1 | bit = bit. For L=2: mask = (bit1<<1) | bit2. So the mask's bits are: most significant is the first character (i), then second, etc. That matches the extraction in precomputation where we assumed MSB is first character. Good.
But note: In our precomputation we used mask >> (L-1 - i) to get bit at position i. That's consistent if we treat mask's MSB as index 0. However, if we construct mask by shifting left and appending, then the first character becomes the most significant bit. Let's test: Suppose substring "01": bits: first=0, second=1. Our construction: start mask=0; L=1: mask = (0<<1)|0 =0; L=2: mask = (0<<1)|1 =1. Binary of 1 is '01'? Actually 1 in binary is '1' but we need 2 bits: '01' corresponds to decimal 1. However, the MSB is 0, LSB is 1. Our precomputation extraction: for L=2, mask=1, bits: i=0: (mask >> (2-1-0)= (mask>>1)= 0; i=1: (mask>>0)=1. So bits = [0,1] correct. So it's fine.
But careful: For L=3, substring "101": bits: 1,0,1. Construction: start mask=0; L=1: add first '1' -> mask=1; L=2: shift left (1<<1=2) + next 0 -> mask=2 (binary 10); L=3: shift left (2<<1=4) + next 1 -> mask=5 (binary 101). Works.
So mask representation is consistent.
Now one nuance: The precomputation good[L][mask] is defined for mask representing exactly L bits. But when we compute mask incrementally, mask always contains exactly L bits because we never exceed that length. However, if we shift left and add, the mask might have more bits if we keep shifting beyond L? But we only shift exactly L-1 times, so the highest set bit can be at position L-1, but mask value can be up to 2^L -1, which fits within 8 bits. So fine.
Thus algorithm:
Precompute good as a list of size 9 (index L). For L from 1 to 8: create list of size 1<<L, initialize False.
For L in 1..8:
For mask in range(1<<L):
arr = [(mask >> (L-1-i)) & 1 for i in range(L)]
# check triples
found = False
for x in range(L):
max_k = (L-1-x)//2
for k in range(1, max_k+1):
if arr[x] == arr[x+k] == arr[x+2*k]:
found = True
break
if found: break
good[L][mask] = found
But note: For L < 3, there are no possible triples (since need at least 3 positions). So good[1][mask]=False, good[2][mask]=False. So all length 1 and 2 substrings are bad (no triple). That's correct: they cannot contain a triple. So they contribute to bad count.
Now iterate:
bad = 0
bits = [1 if c=='1' else 0 for c in s]
n = len(bits)
for i in range(n):
mask = 0
max_len = min(8, n-i)
for offset in range(max_len):
mask = (mask << 1) | bits[i+offset]
L = offset+1
if not good[L][mask]:
bad += 1
After loop, answer = total - bad.
Check example:
Example 1: s="010101", n=6. We'll compute manually maybe via code later.
We should also verify that intervals of length exactly 9 are not counted as bad because we don't process them; but they are all good so they are not subtracted. So answer = total - bad, where bad only from length<=8. That should match expected.
But is there any length 8 interval that could be good? Yes, if it contains a triple. Those intervals are not subtracted (since they are good). So they remain in answer.
Now we should test on examples.
Example 2: s="11001100", n=8. According to van der Waerden, there exists an 8-length string without triple. Let's verify manually: 1 1 0 0 1 1 0 0. Check triples: Consecutive? 1,1,1? No. 1,0,1? etc. Also AP with k=2: positions 1,3,5: 1,0,1 not equal; 2,4,6: 1,0,1; 3,5,7: 0,1,0; 4,6,8: 0,1,0. k=3: 1,4,7: 1,0,0? no; 2,5,8:1,1,0? no. k=4: 1,5,9 out. So indeed no triple. So all substrings of this string of length up to 8? But wait, not all substrings of this string are necessarily triple-free; some substrings might contain triples even if the whole string doesn't. For example, substring "110" maybe? Actually "110": check triples: only possible triple with k=1: indices 1,2,3 -> 1,1,0 not all equal; with k=??? only length 3, k=1 only. So no. Substring "1100"? check k=1: (1,2,3)=1,1,0; (2,3,4)=1,0,0; k=2: (1,3,5) but 5 out of range. So no. Substring "1001"? length 4: check k=1: (1,2,3)=1,0,0; (2,3,4)=0,0,1; k=2: (1,3,5) out. So no. Could there be any substring that contains a triple? Possibly "0011"? check: (1,2,3)=0,0,1; (2,3,4)=0,1,1; k=2: (1,3,5) out. So no. "0110"? similar. So all substrings length <=8 of this string might also be triple-free? But is it possible that some substring of a triple-free string contains a triple? Yes, if the substring is selected differently; but if the whole string is triple-free, all its substrings are also triple-free, because triple would also be a triple in the whole string. So indeed, if the whole string has no triple, then no substring has a triple. So all intervals are bad. So answer 0.
Now example 1: s="010101". n=6, total=21. Let's compute which intervals are good: According to note: only (1,6), (2,6), (1,5). So three good intervals. So bad = 18. Let's see if we can verify manually? Possibly.
Now test algorithm: We need to compute good[L][mask] and then for each interval check if contains triple.
But we need to ensure that our detection of triples matches the condition exactly: s_x = s_{x+k} = s_{x+2k} with l ≤ x < x+2k ≤ r. So we need to check all such triples. Our nested loops will do that.
Now precompute.
Potential edge: For L=8, our mask extraction uses bits from MSB to LSB, which is correct if we treat first character as most significant. The triple detection uses positions relative to substring start (0-index). That's fine.
Now let's test with example 1 manually using code mental or small script? I'll simulate in mind.
But to be thorough, I'll write a small Python snippet in head? Instead, I'll trust algorithm.
But before finalizing, we must consider whether the van der Waerden number result holds for any arithmetic progression of length 3 with common difference positive integer (including k=0? no, k>=1). Yes, it includes all differences.
So indeed any 9 consecutive positions must contain such a triple. Therefore intervals of length >=9 always contain a triple. That's the key.
Thus solution is correct.
Now verify if there is any subtlety: The van der Waerden number says that any 2-coloring of {1,...,9} has a monochromatic AP of length 3. However, does it consider only AP with positive integer difference? Yes. So it's exactly our condition. Therefore any substring of length 9 is good.
But wait: The van der Waerden number is for the set {1,...,N}. In our substring, the positions are not necessarily 1..L? They are contiguous indices from l to r, which are not necessarily 1..L but we can map them to 1..L by subtracting l-1. So yes.
Thus solution stands.
Now one more nuance: The problem statement says: "1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r". So x and k are bounded by n, but also k must be at least 1. That's fine.
Now we implement.
Edge Cases:
n=1: total=1, intervals of length 1: no triple, so bad=1, answer=0.
n=2: total=3, intervals: both singles and whole. All no triple, bad=3, answer=0.
n=3: total=6, intervals: singles and doubles (no triple), and triple whole: may or may not have triple? For length 3, triple exists if all three equal (since only possible triple with k=1). So if s = "000" or "111", then the whole interval [1,3] is good, so answer=1. Our algorithm: For each interval length <=8 (all intervals because n=3 <=8). We'll check each interval. For whole interval length 3, we'll compute good[3][mask]. Our precomputation would mark that mask as good if all bits equal. So we will not count it as bad. So bad will be count of intervals without triple: the 2 singles and 2 doubles? Actually number of intervals total=6. Good intervals = those containing a triple. In length 3, only possibility is if it contains triple; maybe some smaller intervals also? For length 2 or 1, cannot contain triple. So good intervals = possibly just the whole if it contains triple, else 0. So answer = total - bad. Works.
Now performance: Precomputation: up to 510 masks, each checking up to ~12 triples, negligible.
Iteration: for i in range(n) and for offset up to 7 (or 8-1). So total inner loop up to 8n. For each we do shift and OR and lookup. That's about 2.4M operations, trivial.
Thus answer is computed.
But is it always true that any interval of length 9 is good? Let's verify with a potential counterexample: Could there be a binary string of length 9 without a monochromatic arithmetic progression of length 3? According to known results, no. So it's safe.
But I recall that van der Waerden numbers are for the set {1,...,N} where the AP must be exactly equally spaced but not necessarily contiguous? It is equally spaced. So it matches.
Thus final answer.
But we must be careful: In our triple detection, we consider triples only where x, x+k, x+2k are distinct positions; k can be any positive integer. That includes k such that x+2k may skip over positions, but still within the interval. So the precomputation for length L must check all such possibilities. And we do.
Now code.
We'll define:
good = [None] * 9 # index 0 unused, for L from 1 to 8.
for L in range(1,9):
good[L] = [False] * (1<<L)
for mask in range(1<<L):
# extract bits as list
bits = [ (mask >> (L-1-i)) & 1 for i in range(L) ]
# check triples
found = False
for x in range(L):
max_k = (L-1-x)//2
for k in range(1, max_k+1):
if bits[x] == bits[x+k] == bits[x+2*k]:
found = True
break
if found:
break
good[L][mask] = found
Now process s:
s = input().strip()
n = len(s)
bits = [1 if ch == '1' else 0 for ch in s]
total = n*(n+1)//2
bad = 0
for i in range(n):
mask = 0
max_len = min(8, n-i)
for offset in range(max_len):
mask = (mask << 1) | bits[i+offset]
L = offset+1
if not good[L][mask]:
bad += 1
print(total - bad)
But wait, we need to consider that intervals of length 9+ are all good. However, we are only subtracting bad intervals of length up to 8. But what about intervals of length exactly 9? They are not processed, so not subtracted. They are included in total, and since they are good, they contribute to answer. That's fine.
Now test with examples.
Example1: s="010101". n=6. total=21.
We need to compute bad. Let's manually compute all intervals length <=8 (which is all intervals because n=6) that are bad (no triple). Let's list all intervals and check if they contain triple.
We'll compute using programmatic reasoning.
But maybe we can trust. However to double-check, let's manually enumerate intervals of length up to 6.
Indices 1..6: 0 1 0 1 0 1.
List all intervals [l,r] (1-indexed) and check if they contain a triple.
First, note that triple condition: three equally spaced positions with same bit.
The whole string 1-6: positions: 1=0,2=1,3=0,4=1,5=0,6=1. Check all possible triples:
Now count bad intervals: Let's list all intervals (21 total):
Length 1: 6 intervals -> all bad. count=6.
Length 2: intervals: [1,2],[2,3],[3,4],[4,5],[5,6] -> 5 intervals, all bad. total bad=11.
Length 3: intervals: [1,3],[2,4],[3,5],[4,6] -> 4 intervals. According above, all bad? Actually we said [1,3] bad, [2,4] bad, [3,5] bad, [4,6] bad. So 4 bad, total=15.
Length 4: intervals: [1,4],[2,5],[3,6] -> 3 intervals. All bad? [1,4] bad, [2,5] bad, [3,6] bad. So 3 bad, total=18.
Length 5: intervals: [1,5],[2,6] -> 2 intervals. But we said [1,5] good, [2,6] good. So both good. So no additional bad.
Length 6: interval [1,6] -> good.
Thus total bad = 6+5+4+3 = 18. Good intervals = total - bad = 21-18=3. That matches example. So algorithm would produce 18 bad, answer 3.
Now check Example2: s="11001100". n=8 total=36. We need to confirm that all intervals are bad. Since whole string has no triple, and any substring also has no triple, so all intervals are bad. So bad=36, answer=0.
Our algorithm would compute for each start i, max_len = min(8, n-i). For each length L, we compute mask, and check good[L][mask]. Since the whole string has no triple, but we need to verify that every substring also has no triple. But is it possible that a substring of a triple-free string contains a triple? As argued, if the whole string is triple-free, then any substring is also triple-free because any triple within a substring is also a triple within the whole string. So yes, all substrings are triple-free. So all masks for substrings of this string would have good[L][mask]=False. Therefore bad will count all intervals. However, note that we only process lengths up to 8, but total intervals includes lengths 1..8, so we count all intervals. So bad = 36. Answer 0.
Thus algorithm matches.
But we must consider case when n>8: e.g., n=9, some substring of length 9 will always be good, but our bad count only includes intervals length ≤8. So answer = total - bad, which includes all length ≥9 intervals as good.
Now one more nuance: Could there be an interval of length 8 that is good? Yes, if it contains a triple. That would be subtracted? Actually we subtract only if it's bad. So it remains in total as good. That's correct.
Now check edge: Suppose n=9, and the whole string contains a triple, but some substrings of length ≤8 might be bad. That's fine.
Now confirm van der Waerden's number holds for any 2-coloring of 9 numbers. Let's double-check quickly: Known example of length 8 without triple: 00110011? Actually common examples: 0 0 1 1 0 1 0 1 (or its complement) is length 8. For length 9, impossible. So yes.
But is there any nuance about the positions being contiguous and the AP condition uses indices that are within the interval but not necessarily starting at 1? No.
Thus solution is correct.
Now about time: Python loops of 300k * 8 = 2.4M iterations, each doing mask update and lookup. That's fast.
Potential improvements: We could break out early if we find that for a given start i, once we find a good interval of length L, all longer intervals (for that start) will also be good? Not necessarily: Adding more characters could turn a bad interval into good. But the condition is "at least one triple". If a shorter interval is already good, then any super-interval (containing it) is also good because the triple remains inside. So for a fixed start i, as we increase L, once we find that the interval [i, i+L-1] is good, then all larger intervals with same start (longer) are also good. So we could break the inner loop after the first good interval, because we only care about counting bad intervals. For a given start i, we need to count all lengths L (1..max_len) that are bad. If we find that at length L0 the interval is good, then for all L > L0, the interval will also be good because it contains the same triple? Actually careful: If the triple lies entirely within [i, i+L0-1], then for any larger L, the same triple is still inside, so the interval is good. So indeed, once we find a good interval of length L0 for a given start, all longer intervals from that start are good. So we can break the inner loop when we first encounter a good interval. That reduces the number of intervals we process, especially for strings that have early triples. However, worst-case scenario (string with no triples at all up to length 8) will still process all lengths for each start. That's okay. But it might reduce average.
But we must be careful: For a given start, if the interval of length L0 is good, we break, and we do NOT count any longer intervals as bad (which is correct because they are good). Also we do not need to count them as anything; they are included in total automatically. So we can break.
Implementation: For each start i, mask=0, for offset from 0 to max_len-1:
mask = (mask<<1) | bits[i+offset]
L = offset+1
if not good[L][mask]:
bad += 1
else:
# this interval is good, and all larger L for this start will also be good, so break.
break
But caution: Could there be a situation where an interval of length L is good but a longer interval from the same start might NOT be good? No, because the triple that makes it good lies within the first L positions. So it's still present in longer interval. So indeed longer intervals are good. So break is safe.
Thus we can add break after finding a good interval for that start.
But we need to be careful: For start i, if the first good appears at length L0, we break after processing L0. However we have already processed L0 (and maybe counted it as not bad). Actually we just checked good[L][mask]; if it is good, we shouldn't count it as bad, and then we break. That's fine.
Now what about intervals of length less than L0? They are processed and may be bad. Good.
So with break, the inner loop may be shorter for many starts.
This optimization is not necessary but may improve speed a bit.
Now we also need to be careful: For start i where max_len < 9, we break only if we encounter good; else we go through all lengths.
Now consider an example: s="0"*9 (all zeros). For any start i, any interval of length 3 is good because it contains three zeros consecutively? Actually for start i=0, length 3: "000" good. So we will break after L=3. So for each start, we process at most 3 lengths. That reduces operations.
Worst-case: string that is triple-free for all lengths up to 8. For such string, for each start we will process all possible lengths (up to 8). So still O(8n). Acceptable.
Thus we can implement break.
Now re-check the condition for break: If good[L][mask] is True, then this interval [i, i+L-1] is good. Since any extension of this interval to the right (increasing L) still contains that triple, they are all good. So break.
Now about the precomputation: Use list of lists.
Potential pitfalls:
For L from 1 to 8, good[L] array length 2^L. Access good[L][mask] where mask is within 0..2^L-1.
When we break, we need to ensure that we don't miss any intervals that could be bad after being good? Not applicable.
Now one more nuance: The van der Waerden number ensures any interval of length >=9 contains a triple. But what about intervals of length exactly 9? They are automatically good. But our loop for start i only goes up to length 8 for that start. For start i where n-i >=9, we don't process length 9. That's fine because we assume they are good (no need to subtract). So break is for lengths <=8 only. So we still need to process up to length 8. For start i where n-i >=9, max_len = 8, so we process lengths 1..8. If any of these is good, we break; else if none are good up to length 8, then all intervals with that start and length <=8 are bad. But the intervals with length 9+ for that start are good. That's accounted by total.
Now we need to ensure that for a start i, if all lengths 1..8 are bad, we then have intervals length 9,10,... which are good. So we don't need to break because we exhausted max_len.
Thus algorithm is solid.
Now we should test with some random strings to verify correctness.
But before that, note that the problem might have n up to 300,000 and we need to output a possibly large number (up to about 45e10) which fits in 64-bit integer. Python int is arbitrary precision, fine.
Now we implement.
I'll write code accordingly.
But before finalizing, let's test with some small strings by mental or quick reasoning.
Test n=3, s="000". total=6. Precompute: good[3][mask] for mask representing "000" = True. Let's compute: mask for "000" (L=3) = 0. good[3][0] should be True? Because bits [0,0,0] all equal, triple exists. In our precomputation, we set good[3][mask] to True if found triple. So it should be True. So for start i=0, we process lengths:
L=1: mask=0 -> good[1][0] is False (since length 1 no triple) -> bad++ (now bad=1)
L=2: mask = (0<<1)|0 =0 -> good[2][0] False -> bad++ (bad=2)
L=3: mask = (0<<1)|0? Actually need to compute correctly: after L=2 mask=0, then L=3: mask = (0<<1)|0 =0? Wait we need to add the third bit: bits[0+2] = 0. So mask = (0<<1) | 0 = 0. So mask=0. good[3][0] = True, so not bad, and we break.
So bad = 2. total=6 => answer=4. But earlier we reasoned that only the whole interval [1,3] is good, so answer should be 1, not 4. There is discrepancy: Our count of bad intervals gave 2, implying good intervals = 4. Let's double-check: For s="000", intervals: [1,1], [2,2], [3,3] (3), [1,2], [2,3] (2), [1,3] (1). That's 6 total. Which are good? Those containing a triple: [1,3] contains triple (k=1, positions 1,2,3 all 0). Also, does [1,2] contain a triple? No, because need three positions. [2,3] no. So only 1 good interval. So answer should be 1, not 4. Our algorithm gave answer = total - bad = 6 - 2 = 4, which is wrong.
What went wrong? Let's enumerate our bad count: We counted two bad intervals for start i=0: lengths 1 and 2 are bad. That's correct: intervals [1,1] and [1,2] are bad. But we also have start i=1 and i=2. We only processed start i=0. But we need to process all starts. Did we miss them? In our loop, we iterate for i in range(n): i=0,1,2. So we should process all starts. But in my mental simulation, I only considered i=0. For i=1 (start at index 1), similar: L=1: bit=0 -> bad; L=2: bits[1,2] = 00 -> mask? But we need to compute: i=1, bits[1]=0, bits[2]=0. For L=1: mask=0 -> bad; L=2: mask = (0<<1)|0=0 -> good[2][0] False -> bad; L=3? max_len = min(8, n-i) = min(8,2)=2, so only lengths 1 and 2. So both bad. So from i=1, we add 2 bad intervals: [2,2] and [2,3]. That's correct: both are bad. For i=2 (start index 2), max_len=1, L=1: mask=0 -> bad; add [3,3] which is bad. So total bad from all starts: i=0: 2 (length1,2) ; i=1:2 ; i=2:1 -> total bad = 5. Then answer = total - bad = 6 - 5 = 1. That's correct. I mistakenly only counted i=0. So algorithm gives correct answer.
Now test with s="010101", n=6. Our earlier manual gave bad=18, answer=3. Let's compute algorithmically: For each start, we will count bad intervals up to length min(8, n-i). We'll compute quickly:
Indices 0..5: bits: 0,1,0,1,0,1.
Start i=0: max_len=6. Process lengths:
L=1: mask=0 -> good[1][0]=False -> bad.
L=2: mask = (0<<1)|1 = 1 -> good[2][1]? For length2, always False -> bad.
L=3: mask = (1<<1)|0 = 2? Actually after L=2 mask=1 (binary 01). For L=3: mask = (1<<1)|0 = 2 (binary 010). So bits: 0,1,0. good[3][2]? Check triples: (0,1,2) positions: 0,1,0 not all equal; k=1 only. So no triple => good=False -> bad.
L=4: mask = (2<<1)|1 = 5 (binary 0101). L=4: Check triples: Need to see if substring "0101" contains triple. We earlier determined [1,4] is bad. So good[4][5] should be False -> bad.
L=5: mask = (5<<1)|0 = 10 (binary 01010). L=5: substring "01010". We earlier said [1,5] is good because (1,3,5) are 0,0,0. So good[5][10] should be True. So for L=5, good=True => not bad, and we break. So for start i=0, we counted bad for lengths 1,2,3,4 (4 bad). Good for L=5 and beyond, but break.
Start i=1: bits starting at index1: 1,0,1,0,1. max_len=5.
L=1: mask=1 -> good[1][1]=False -> bad.
L=2: mask = (1<<1)|0 = 2 (binary 10) -> good[2][2]=False -> bad.
L=3: mask = (2<<1)|1 = 5 (binary 101) -> substring "101". Check triples: only possible k=1: 1,0,1 not all equal -> no triple. So good=False -> bad.
L=4: mask = (5<<1)|0 = 10 (binary 1010) -> substring "1010". Check triples: k=1: (1,2,3)=1,0,1; (2,3,4)=0,1,0; k=2: (1,3,5) out. So no triple -> good=False -> bad.
L=5: mask = (10<<1)|1 = 21 (binary 10101) -> substring "10101". Check triples: k=1: (1,2,3)=1,0,1; (2,3,4)=0,1,0; (3,4,5)=1,0,1; k=2: (1,3,5)=1,1,1? Actually positions 1=1,3=1,5=1 -> all 1? In "10101": indices 1->1, 3->1, 5->1 -> yes triple. So good=True -> break. So for start i=1, bad for lengths 1,2,3,4 (4 bad). (Note: L=5 is good, but break.)
Start i=2: bits: 0,1,0,1. max_len=4.
L=1: mask=0 -> bad.
L=2: mask = (0<<1)|1 = 1 -> bad.
L=3: mask = (1<<1)|0 = 2 -> substring "010". Check: k=1: 0,1,0 -> no triple -> good=False -> bad.
L=4: mask = (2<<1)|1 = 5 -> substring "0101". Check: no triple -> good=False -> bad.
Since all lengths are bad, we process all 4 lengths, no break. So bad count for i=2: 4.
Now sum bad: i0:4, i1:4, i2:4, i3:3, i4:2, i5:1 = total bad = 4+4+4+3+2+1 = 18. Good. So algorithm works.
Thus the solution is correct.
Now we can implement.
Potential pitfalls:
The precomputation good[L][mask] uses mask bits representation as described. Ensure that for L < 8, the mask values are within 0..2^L-1. When we do mask = (mask << 1) | bit, we always get a number with exactly L bits, because we are shifting left and adding a bit. However, for L=8, mask may be up to 255. That's fine.
But caution: For start i where we break early because we found a good interval, we must not forget to still process longer intervals? But we break because all longer are good, so they are not bad. So it's fine.
Edge: Could there be an interval of length 8 that is good, but the precomputed good[8][mask] = True? Yes.
For L=8, we might break after length 5 or something; we don't need to process further.
Now about the precomputation: We'll compute good as a list of lists. But perhaps we can compute on-the-fly using a memoization? But precomputing is easy.
Implementation details:
We'll define good = [None] * 9.
for L in range(1,9):
size = 1 << L
arr = [False] * size
for mask in range(size):
# generate bits list
bits = []
for j in range(L):
# extract from MSB to LSB: (mask >> (L-1-j)) & 1
bits.append((mask >> (L-1-j)) & 1)
# check triples
ok = False
for x in range(L):
max_k = (L-1-x)//2
for k in range(1, max_k+1):
if bits[x] == bits[x+k] == bits[x+2*k]:
ok = True
break
if ok:
break
arr[mask] = ok
good[L] = arr
But generating bits list for each mask might be a bit slow for precomputation (510 masks) but it's fine.
Alternatively, we can precompute using bit operations without list, but easier to use list.
Now note: For L=1, max_k=0 so loop doesn't run, ok remains False, good[1][mask] = False for all. Good.
For L=2, also False.
Now we can also store good as a flat dictionary keyed by (L,mask). But list is fine.
Now complexity of precomputation: For L=8, 256 masks, each checking up to 12 triples. So at most 3000 operations, trivial.
Now main loop.
We must ensure that we use Python's integers as mask. Since we shift left, mask may become large for lengths > 8? But we only go up to 8, so mask <= 255.
Now potential issues: For start i, after we break, we need to go to next i. So we need a break statement inside the inner loop when good is found.
Now final output.
Let's test with sample input.
But we must also consider that the problem input may have newline after string. We'll use input().strip().
Now implement.
I'll also test with a random string length 9 that is like triple-free for length 8 but forced triple at length 9. For instance, known length 8 triple-free string: 00110011? Actually check: 0 0 1 1 0 0 1 1? Does that have triple? Check consecutive: 0,0,1? Not all equal. k=2: (1,3,5): 0,1,0; (2,4,6):0,1,0; (3,5,7):1,0,1; (4,6,8):1,0,1. No. k=3: (1,4,7):0,1,1? no; (2,5,8):0,0,1? no. k=4: (1,5,9) out. So triple-free. So any string of length 8 that is triple-free. Then extending to length 9 must create triple. So for a string of length 9 that is extension of such triple-free prefix? But the whole string of length 9 must contain triple. So any substring of length 9 is good. So our algorithm would count intervals: for starts i with n-i >=9, we process lengths 1..8. Many may be bad, but all length 9+ intervals are good.
Now total intervals = n*(n+1)/2. For n=9, total=45. Suppose the whole string is such that all length <=8 substrings are bad? Is that possible? Could a length 9 string have all its substrings of length <=8 bad? That would mean that for every interval of length <=8, there is no triple. But the whole string length 9 must contain a triple (by van der Waerden). So some interval of length 9 is good (the whole string). Also intervals of length 9 that are not the whole string? For n=9, there are two intervals of length 9? Actually only one: the whole string. Intervals of length 8 are many. Could all length 8 substrings be bad? Possibly if the string is triple-free of length 8, but the whole string of length 9 forces a triple; that triple must be within some 9-length interval; but it might be exactly the whole string. So the length 8 substrings might still be triple-free. For example, take a known length 8 triple-free string like 00110011, and add a 9th character that creates a triple. For instance, if we add 0 at the end: 001100110. Does that create a triple? Let's test: Check triples in 001100110. We need to see if there is any triple anywhere. Possibly there is. But could the length 8 substrings (which are subsets) still be triple-free? Possibly some might become good if they include the new character? The length 8 substrings that include the new character are positions 2-9, 1-8? Actually for n=9, intervals of length 8: [1,8] and [2,9]. [1,8] is the original triple-free string, so bad. [2,9] includes the new character. Could [2,9] contain a triple? Possibly if the added character creates a triple with some existing ones. But not necessarily; maybe the string 0 0 1 1 0 0 1 1 0 (append 0). Check [2,9] = 0 1 1 0 0 1 1 0? Actually original: indices: 1:0,2:0,3:1,4:1,5:0,6:0,7:1,8:1,9:0. [2,9]: 0,1,1,0,0,1,1,0. Check for triple: maybe there is one? Let's check systematically: k=1: look for three consecutive equal bits: sequences: 0,1,1 not; 1,1,0 not; 1,0,0 not; 0,0,1 not; 0,1,1 not; 1,1,0 not. No. k=2: positions: (2,4,6):0,0,0? indices2=0,4=1? Wait: (2,4,6): index2=0, index4=1, index6=0 -> 0,1,0 not all equal. (3,5,7):3=1,5=0,7=1 -> 1,0,1; (4,6,8):4=1,6=0,8=1 -> 1,0,1; (5,7,9):5=0,7=1,9=0 -> 0,1,0. No. k=3: (2,5,8):2=0,5=0,8=1 -> 0,0,1; (3,6,9):3=1,6=0,9=0 -> 1,0,0; (4,7,10) out. k=4: (2,6,10) out. So no triple. So both length 8 substrings are bad. So for this n=9 string, all intervals of length <=8 are bad (since the only length 8 intervals are both bad, and all shorter intervals are subintervals of those, thus also bad? Not necessarily; but subintervals of a triple-free string are also triple-free, so indeed all intervals of length <=8 are bad). So total bad intervals would be all intervals of length <=8. Number of intervals length <=8: as earlier sum_{L=1}^{8} (n-L+1) = 8n - 28 = 8*9 - 28 = 72 - 28 = 44. So bad = 44. Total intervals = 45. Answer = 1 (only the whole string). So answer would be 1. That seems plausible. Our algorithm would compute bad as 44 and answer 1. So it's fine.
Now check that our loop would count correctly: For start i=0 (first char 0), max_len = min(8,9)=8. We process lengths until we find a good interval or reach 8. Since the interval of length 8 (positions 1-8) is bad, we will process all 8 lengths, each will be bad (since we already know they're all bad). So we add 8 bad from i=0. For i=1, max_len = min(8,8)=8. We process all lengths: lengths 1..8 for substring starting at index 1 (positions 2-9). This interval of length 8 (positions 2-9) is also bad (as we checked). So all 8 lengths are bad. So i=1 adds 8 bad. For i=2, max_len = min(8,7)=7. All substrings of this start are substrings of the bad length-8 intervals, thus also bad. So we will process all lengths and add counts accordingly. Summation yields 44. Good.
Thus algorithm works.
Now, but there is a nuance: We assumed that if an interval of length L is good, all longer intervals with same start are good. That is true because the triple is within the first L positions. However, what about intervals with same start but extending to the right? That's exactly what we consider: L increases, the interval [i, i+L-1] contains [i, i+L0-1] if L >= L0. So yes.
Thus break is safe.
Now we might also consider early break for each start when we find a good interval. This will skip processing longer lengths for that start. That's correct.
Now confirm with start i where max_len < L0? If max_len is less than L0 (i.e., we never reach length where triple appears), then we process all lengths and don't break. That's fine.
Thus final answer.
Now we can implement and test with given examples.
But before finalizing, we should double-check that our precomputation for good[L][mask] matches our mask construction. Let's test for L=3 mask=5 (binary 101) we said good=False. But we should compute manually: bits: (mask>>2)&1 = 1? Actually for L=3, MSB is bit2: (mask>>2)&1. mask=5 binary 101. So bits: pos0 (MSB) = 1, pos1 = 0, pos2 = 1. So bits = [1,0,1]. Triple? Only possible k=1: bits[0],bits[1],bits[2] = 1,0,1 not all equal. So good=False. So correct.
For L=5 mask=10 (binary 01010): L=5, mask=10 decimal, binary 01010 (5 bits). Extract: bits: pos0: (mask>>4)&1 = 0, pos1: (mask>>3)&1 = 1, pos2: (mask>>2)&1 = 0, pos3: (mask>>1)&1 = 1, pos4: (mask>>0)&1 = 0. So bits = [0,1,0,1,0]. Check triples: (0,2,4) -> 0,0,0 -> all equal, so good=True. So precomputation should mark good[5][10] = True. Good.
Now for L=5 mask=21 (binary 10101): bits [1,0,1,0,1]. Triple (0,2,4) -> 1,1,1 -> good=True. So correct.
Now ensure that for L=8, we correctly compute. All good.
Now final code.
But maybe we can micro-opt: Instead of precomputing good for each L and mask by extracting bits into list each time, we can compute using bit operations directly. But it's fine.
Alternatively, we can compute good on the fly for each interval but use the precomputed table. The table is easier.
Now ensure that we handle large n up to 300k and output possibly large number.
Now we can produce final answer.
One more check: Could there be any triple with k=0? No, because k must be positive and also condition x < x+2k ensures k>0.
Now answer's maximum: n=300k, total intervals ≈ 45e9 (300000*300001/2 ≈ 45,000,150,000). That's about 4.5e10, fits in 64-bit (2^63-1 ≈9e18). So Python int fine.
Now test sample inputs:
Input: 010101 -> Output 3. Our algorithm gives.
Input: 11001100 -> Output 0.
Now run mental on second sample: s=11001100, n=8. Our algorithm will process all starts i=0..7, each max_len = n-i, all intervals are bad. Let's compute: For i=0, max_len=8, all lengths? We'll need to see if any length yields good. The whole string (L=8) is bad, as we argued. For L=5? Might there be a triple in some substring? If the whole string is triple-free, all substrings are triple-free. So all masks will be good=False. So each start will process all lengths (since never good) and count all. For i=0: 8 bad, i=1:7? Actually max_len = n-i: i=0 ->8, i=1->7, i=2->6, i=3->5, i=4->4, i=5->3, i=6->2, i=7->1. Sum = 8+7+6+5+4+3+2+1 = 36. So bad=36, total=36, answer=0. Works.
Now we can implement.
Potential optimization: Use Python's list of bool or ints; fine.
Now we produce final solution.
But wait, is there any scenario where an interval of length >=9 might still be bad due to constraints on k? Because k must be ≤ n? But k is bounded by n, but since interval length >=9, we consider triples within the interval; k can be as large as (r-x)/2, but that's always ≤ n anyway because r ≤ n. So no issue.
Thus final answer.
Now additional verification: Could there be any triple that is not captured by our precomputation because we only consider intervals up to length 8? No, because for longer intervals we consider them automatically good. That's based on van der Waerden. So correct.
But we must be absolutely sure about van der Waerden's theorem applying to any 2-coloring of 1..N. Yes.
One nuance: The theorem ensures existence of a monochromatic arithmetic progression of length 3, but does it require the progression to be exactly 3 terms, with common difference >0, and the terms must be distinct and in increasing order. That matches our condition.
Thus answer.
Now we can write the code.
But before finalizing, consider alternative: Could there be a triple with k such that x+2k ≤ r but x+2k may exceed n? No because r ≤ n.
Thus all good.
Now I'll write the final solution explanation.
But I'll also consider potential pitfalls: The string length can be up to 300k, and our loops are O(n*8). However, Python's loops may be slower if we do many Python-level operations. But 2.4M loops with simple integer operations and list lookups is fine (likely < 0.5 seconds). Even if we do 2.4M * maybe a few operations, it's fine.
To be safe, we can incorporate the break optimization to reduce average.
Now code:
import sys
def precompute():
good = [None] * 9
for L in range(1,9):
size = 1 << L
arr = [False] * size
for mask in range(size):
# extract bits
bits = []
for i in range(L):
bits.append((mask >> (L-1-i)) & 1)
# check triples
found = False
for x in range(L):
max_k = (L-1-x) // 2
for k in range(1, max_k+1):
if bits[x] == bits[x+k] == bits[x+2*k]:
found = True
break
if found:
break
arr[mask] = found
good[L] = arr
return good
def solve():
s = sys.stdin.readline().strip()
n = len(s)
total = n * (n+1) // 2
bits = [1 if ch == '1' else 0 for ch in s]
good = precompute()
bad = 0
for i in range(n):
mask = 0
max_len = min(8, n - i)
for offset in range(max_len):
mask = (mask << 1) | bits[i+offset]
L = offset + 1
if not good[L][mask]:
bad += 1
else:
# this interval good, so all longer intervals with same start are good
break
print(total - bad)
if name == "main":
solve()
Now let's test quickly with sample.
We'll run mental for sample1 as above: works.
Now sample2: works.
Now potential bug: In precompute for L=1, mask=0 or 1, arr[mask] = False. That's correct.
Now check for L=2: arr always False. That's correct.
Now check for L=3: we must ensure good[3][0] (000) is True, and good[3][7] (111) is True, and others false. Let's test: mask=0 bits 0,0,0 -> found triple because bits[0]==bits[1]==bits[2]? That's with k=1. Our loop: x from 0 to 2; x=0, max_k = (2-0)//2 =1, so k=1 check bits[0]==bits[1]==bits[2]? That checks positions 0,1,2. So yes, found. So arr[0]=True. Similarly for mask=7 (111) bits all 1 -> found. Good.
Now for L=4: We need to check e.g., mask for "0011"? Actually we should trust.
Now edge: For L=8, we might have mask representing string that contains triple; our detection will mark good.
Now note: In precompute, we use nested loops. For L=8, there are 256 masks, each with up to 12 triple checks. So total checks ~ 3000. Fine.
Now about speed: The main loop's inner loop uses good[L][mask] which is a list lookup. That's O(1).
Now break optimization: When we break, we skip remaining lengths for that start. That can reduce operations for many starts if the string has early triples.
Now we need to consider the case where the interval of length L is good, we break. But what if there is a longer interval with same start that does NOT contain that triple because the triple we found might involve positions that go beyond the interval? No, if we found a triple within the interval of length L, that triple's indices are all ≤ i+L-1. So they are inside any interval that extends beyond i+L-1. So it remains a triple. So it's still good.
Thus break is correct.
Now final code.
But we might need to handle input string with newline and maybe spaces? The problem says first line contains the string s. So it's a single line.
Now we output integer.
Now test with n=300k random string. The main loop will run ~300k iterations for i, and each i may run up to 8 inner iterations. That's ~2.4M inner loops. Each inner loop does mask update, L computation, list lookup, maybe an if and break. That's fine.
Thus final answer.
But we should also consider that using Python's bit list for good precomputation might be memory efficient: good[1] size 2, good[2] size 4,... total 510 entries. Fine.
Now we should produce final answer.
However, before finalizing, I want to double-check van der Waerden number: W(2,3)=9. Is there any nuance about arithmetic progression of length 3 with difference > 0? Yes. So any 2-coloring of 1..9 contains a monochromatic AP of length 3. It's a known result. So we are safe.
But there is a nuance: In our problem, we consider substrings which are contiguous sequences of indices. However, the van der Waerden number is about any set of integers that are consecutive. So it's exactly the same. So length 9 forces a triple.
Thus answer.
Now we can finalize.
But I want to make sure that there is no hidden condition that k must be ≤ n? But that's automatically satisfied since x+2k ≤ r ≤ n, so k ≤ (n-1)/2. But in van der Waerden, the AP can have any difference, including large ones, but within 1..N. So it's the same.
Now let's consider if the van der Waerden number actually is 9 for "any" two coloring? I recall the known proof: The maximal length of a binary string without a monochromatic arithmetic progression of length 3 is 8. So yes.
Thus answer.
Now I'll provide explanation in final answer.
But the problem might be from some contest and the intended solution uses this van der Waerden property. It's typical.
Now note: Some might argue that we could also solve by directly counting "good" intervals using two pointers: For each left, find minimal right such that interval contains a triple, then all larger right are good. That's also possible: For each l, find smallest r >= l such that substring [l,r] contains triple, then number of good intervals with left=l is n - r + 1 (if such r exists). If no such r exists (i.e., no triple even up to n), then count 0 for that l. But we need to compute minimal r efficiently. Could be done by scanning and maintaining some data structure. However, with van der Waerden, we know that minimal r - l + 1 is at most 9, so we can just brute for each l up to l+8. That gives the same O(9n) approach. Actually we can directly count good intervals: For each l, find the smallest r (if any) where triple appears within [l,r], with length up to 9. Then if found, all intervals [l, r'] for r' >= r are good. So number of good intervals with left l = n - r + 1. Sum over l. That's another way. This avoids counting total and subtracting bad, but both are equivalent.
Let's explore: For each l, we can check substrings starting at l with length from 1 to min(9, n-l+1) until we find a triple. If we find at length L, then that interval is good and all larger r are good. So contribution from this l = n - (l+L-1) + 1 = n - l - L + 2. If we never find a triple even after checking up to length 8? Actually if we check up to length 9, but we know length 9 must contain triple, so we will always find a triple by length 9 if n-l+1 >=9. So for l such that n-l+1 >=9, we will find some L <=9 (maybe smaller). For l such that remaining length <9, we need to check up to the end; if no triple, contribution 0. So we can compute answer as sum over l of max(0, n - (l + first_good_len - 1) + 1). Where first_good_len is minimal length that yields a triple, or INF if none.
That also yields O(9n). This may be simpler to compute directly answer without total and bad. Let's test: For s="010101", n=6. For l=1, check lengths: L=1..5 until triple found. We found triple at L=5. So first_good_len=5, then contribution = n - (1+5-1) +1 = 6 -5 +1? Actually compute: r = l+L-1 = 1+5-1=5, then good intervals with left=1: all r>=5, i.e., r=5,6 => 2 intervals? But we earlier counted good intervals: (1,5) and (1,6) => 2 intervals with left=1. Good. For l=2, first_good_len=5 (since L=5 gives triple). r=2+5-1=6, so good intervals with left=2: r>=6 => only (2,6) => 1 interval. For l=3, check lengths 1..4 (since n-l+1=4), none have triple. So contribution 0. l=4: lengths 1..3, none ->0. l=5: lengths 1..2, none ->0. l=6: length1, none ->0. Sum = 2+1 =3. That's answer. So correct.
For s="11001100", n=8. For each l, we check up to remaining length (which is <=8). Since no triple ever, contributions all 0. So answer 0.
Thus we can also compute answer directly by scanning each start l and finding the first length L (1..min(9, n-l+1)) where substring has triple, then add n - (l+L-1) to answer? Actually careful: number of r such that r >= r_min = (l+L-1) and r ≤ n is n - (l+L-1) + 1 = n - l - L + 2. So we can sum that.
But note: For l where remaining length >=9, we are guaranteed to find a triple at some L ≤ 9. But it might be that the triple appears at L=9 exactly if earlier lengths don't have triple. So we need to check lengths 1..9. That's still constant.
Thus we could implement as:
ans = 0
for l in range(n):
found = False
for L in range(1, min(9, n-l)+1): # L is length
# check if substring s[l:l+L] contains triple.
if contains_triple(s[l:l+L]): # but we can use mask method
found = True
ans += n - (l + L - 1) # number of r from l+L-1 to n-1 inclusive? Actually need +1? Let's derive: r_min = l+L-1 (0-index). Number of r from r_min to n-1 inclusive = n-1 - r_min + 1 = n - r_min = n - (l+L-1). Wait using 0-index: l from 0 to n-1, r from l to n-1. If we find first good at r_min = l+L-1 (0-index), then all r >= r_min are good. Number of such r = n-1 - r_min + 1 = n - r_min = n - (l+L-1). That's the count. For l=0, L=5 gave r_min=4, n=6 => count = 6-4 =2. Good. So ans += n - (l+L-1). Then break.
# If not found (i.e., remaining length <9 and no triple), contribute 0.
So we can compute ans directly without total and bad. This is maybe even simpler: we just sum contributions.
But we need to be able to check contains_triple quickly for each substring length up to 9. Use the same precomputation and rolling mask. For each l, we compute mask incrementally for lengths, break when good.
Thus we can compute ans directly.
We could also incorporate early break: as soon as we find triple, add contribution and break.
This avoids the need to compute total and bad separately. Might be simpler and also avoids potential overflow? Not needed.
Both are fine. I'll stick with total - bad because it's easier to reason: all length >=9 are good; count bad of length <=8; subtract.
But we need to ensure we count bad intervals correctly: We used break when interval is good to stop counting further lengths for that start because they are good. But careful: In our bad counting approach, we increment bad only for intervals that are not good. When we break upon encountering a good interval, we skip counting further lengths. That is correct because further lengths are not bad. So the loop is as described.
But there is a nuance: In the total - bad approach, we rely on the fact that all intervals length >=9 are good. But if for a given start i, we break early (say at length L0 < 9), we are not processing lengths > L0 up to 8. However, those lengths > L0 but ≤8 are still intervals length ≤8; but they are good because they contain the triple found at length L0. So they should not be counted as bad. So skipping them is fine. So our bad count is correct.
But we must ensure that for starts where we don't break (i.e., no good interval found up to length max_len), we have processed all lengths up to max_len (which is ≤8). Those are all bad. So correct.
Now we also need to guarantee that intervals of length exactly 9 are automatically good. They are not part of bad count because we never subtract them. So total - bad includes them. So correct.
Thus we can compute total - bad.
But let's verify with a scenario where a start has max_len = 8 and we find good at L=5, so we break. Then for that start, intervals lengths 6,7,8 are not processed; they are good, so not bad. So total includes them, and we subtract only bad intervals (lengths 1-4). That's correct.
Now we must also consider starts where max_len < 9 but we don't find a good interval; then all lengths for that start are bad. That's correct.
Thus algorithm works.
Now ensure that for starts where n-i >=9, we set max_len = 8, because we only need to check up to 8. But what if the first good interval occurs at length 9? In that case, for that start, we will process lengths 1..8 and find all bad, not break (since no good among them). Then we will have counted all 8 lengths as bad. But the intervals of length 9 for that start are good, and they are not subtracted. So the total includes them. That's correct: The bad count from this start is 8, and the good intervals for this start are lengths 9,10,..., n-i+1. So total good includes them.
Now, is it possible that for some start, the first good interval occurs at length less than or equal to 8? Yes, then we break earlier.
Thus all good.
Now, could there be a scenario where the interval of length 9 is not good? No, by van der Waerden. So for start with remaining length >=9, there is always a good interval at some length ≤9. But it could be exactly 9 if lengths 1-8 are all bad. So then we processed all 8 as bad, and the good ones are from length 9 onward. That's accounted.
Thus correctness stands.
Now, note: Our max_len is min(8, n-i). That's correct because if n-i >=9, we only need to check up to length 8. If n-i <9, we check all lengths (since all lengths ≤8 anyway). So that's correct.
Now, we must verify that our good table includes length up to 8. So we can check up to length 8.
Now for lengths 9 we don't need table, because we consider them automatically good.
Thus final solution.
Now we can write the final answer with explanation.
But before final, let's test with a custom small case to verify correctness.
Case: n=3, s="010". total=6. Let's enumerate intervals: [1,1],[2,2],[3,3] bad; [1,2] (01) bad; [2,3] (10) bad; [1,3] (010) check triple? only k=1: 0,1,0 not all equal. So bad. So all bad, answer=0. Our algorithm: i=0: lengths 1..3? max_len=3. Process:
L=1: mask=0 -> good[1][0]=False -> bad.
L=2: mask = (0<<1)|1 =1 -> good[2][1]=False -> bad.
L=3: mask = (1<<1)|0 =2? Wait compute: after L=2 mask=1 (binary 01). L=3: mask = (1<<1)|0 =2 (binary 010). good[3][2]? Check triple: 0,1,0 -> no triple -> False -> bad. So i=0 adds 3 bad.
i=1: max_len=2. L=1: mask=1? bits[1]=1 -> mask=1 -> bad. L=2: mask = (1<<1)|0 =2 -> good[2][2]=False? length2 always false -> bad. So 2 bad.
i=2: max_len=1. L=1: mask=0? bits[2]=0 -> mask=0 -> bad. So 1 bad.
Total bad = 3+2+1=6, answer=0. correct.
Case: n=3, s="000". total=6. Good intervals: only [1,3] good. So answer=1. We computed earlier: i=0: L=1 bad, L=2 bad, L=3 good -> break after L=3? Actually at L=3 good, we break and do not count L=3 as bad. So i=0 bad count 2. i=1: lengths 1,2 (since max_len=2): both bad? Check: substring from index1: "00"? L=1 bad, L=2 bad? "00" no triple -> bad. So i=1 bad=2. i=2: length1 bad ->1. Total bad = 2+2+1=5, answer=1. correct.
Case: n=4, s="0010". Let's manually. total=10. Determine which intervals contain triple. Write bits: 0,0,1,0. Check all intervals:
length1: 4 intervals bad.
length2: [1,2]=00 -> no triple? need 3 positions, so bad. [2,3]=01 bad, [3,4]=10 bad -> 3 bad.
length3: [1,3]=001 -> check k=1: 0,0,1 not all equal -> no; only k=1, so bad. [2,4]=010 -> k=1: 0,1,0 -> bad. So 2 bad.
length4: [1,4]=0010 -> check k=1: (1,2,3)=0,0,1; (2,3,4)=0,1,0; k=2: (1,3,5) out. So no triple -> bad? Actually length4 but could have triple with k=2? (1,3,5) out. So no triple. So entire string is triple-free. So all intervals bad. So answer=0. Our algorithm: since n=4<9, we process all intervals up to length 4. We'll compute masks and table. Need to check if any interval is good. The whole string length 4, does our precomputation say good[4][mask]? mask for "0010" is bits: 0,0,1,0 -> binary 0010 = decimal 2. L=4. Check triples: possible (x,k) with x+2k<4: x=0: k=1 -> bits0,1,2 =0,0,1; k=2 -> bits0,2,4 out (since 4 >=4). x=1: k=1 -> bits1,2,3 =0,1,0. So no triple. So good[4][2] = False. So all intervals are bad. So bad count = sum_{L=1}^{4} (n-L+1) = 4+3+2+1=10. Answer=0. correct.
Now case: n=5, s="00100". Check manually: total=15. Determine good intervals. Bits: 0,0,1,0,0. Check if whole [1,5] has triple: possible k=1: (1,2,3)=0,0,1; (2,3,4)=0,1,0; (3,4,5)=1,0,0; k=2: (1,3,5)=0,1,0; (2,4,6) out. So no triple. length 4 intervals: [1,4]=0010 (we saw no triple), [2,5]=0100? bits:0,1,0,0 check k=1: (2,3,4)=0,1,0; (3,4,5)=1,0,0; k=2: (2,4,6) out. So no. length3 intervals: [1,3]=001 -> no triple (001); [2,4]=010 -> no; [3,5]=100 -> no. So all intervals bad? length5 also bad. So answer=0. Our algorithm would count bad = 5+4+3+2+1=15, answer=0. Works.
Now case: n=5, s="00111". Bits:0,0,1,1,1. Check whole: [1,5]: triples? k=1: (3,4,5)=1,1,1 -> yes, so good. Also maybe others. So good intervals include any interval containing positions 3-5? Let's compute minimal r for each l.
We'll compute our algorithm: For l=0 (index0), we check lengths: L=1: "0" bad; L=2: "00" bad; L=3: "001" no triple? Check 0,0,1 -> not all equal; so bad; L=4: "0011": check k=1: (0,1,2)=0,0,1; (1,2,3)=0,1,1; k=2: (0,2,4) -> indices 0,2,4: 0,1,1? Actually bits: 0,0,1,1,? L=4 only indices 0-3: bits:0,0,1,1. So (0,2,4) out. So no triple -> bad. L=5: "00111": check k=1: (2,3,4)=1,1,1 -> triple. So first good at L=5. So for l=0, contribution to answer in direct method would be n - (l+L-1) = 5 - (0+5-1)=5-4=1? Actually number of r >=4 (0-index) i.e., r=4 only? Wait r_min = 4 (0-index corresponds to position 5). So intervals with left=1: r=5 only? Actually left=1, r can be 5. That's one interval. But we also have left=1, r=5 is good. Also maybe r=4? left=1, r=4 (indices 1-4) we already determined bad. So yes only r=5. So contribution 1.
l=1 (index1): bits starting at 1: 0,1,1,1. Check lengths: L=1: "0" bad; L=2: "01" bad; L=3: "011": check k=1: 0,1,1 no; so bad; L=4: "0111": check k=1: (1,2,3) indices relative: 1,2,3: 1,1,1? Actually bits: index1=0, index2=1, index3=1, index4=1 (0-index). So positions relative to substring: 0:0,1:1,2:1,3:1. Check triples: x=0,k=1: 0,1,1 no; x=1,k=1: 1,1,1? positions 1,2,3: all 1 -> yes! So triple at L=4 (since x=1 (relative) corresponds to index2 in original, but triple is within substring). So first good L=4, r_min = l+L-1 = 1+4-1=4. So intervals with left=2 (index1+1=2) are r >=4 => r=4,5? Actually r index 4 and 5 (since n=5). But careful: 0-index: l=1 -> original left=2; r_min =4 -> original r=5? Wait: indices: original positions: l=1 means second character, r_min =4 means fifth character (since 0-index). So r values: 4 and maybe 5? n-1=4, so only r=4 available. Because n=5, indices 0..4. So only r=4. So contribution = n - r_min = 5 - 4 = 1? Actually number of r from r_min to n-1 inclusive = 5-4 =1. So one interval: [2,5] (original). But also maybe r=5? No index 5 out of range. So good intervals with left=2: only [2,5]. So contribution 1.
l=2 (index2): bits:1,1,1. L=1: "1" bad; L=2: "11" bad; L=3: "111": triple -> good at L=3. r_min = 2+3-1=4. n - r_min =5-4=1, so interval [3,5] (original) good. So contribution 1.
l=3 (index3): bits:1,1. L=1: "1" bad; L=2: "11" bad (no triple). So no good. Contribution 0.
Total good intervals = 1+1+1=3. Let's list all good intervals manually: [1,5], [2,5], [3,5]? Actually we found [1,5] (left=1,right=5), [2,5], [3,5]. Are there any others? [2,4]? we saw bad. [3,4]? bad. [4,5]? no triple. So answer 3.
Now compute using total - bad method: total=15. Need to count bad intervals (no triple). We can compute by enumeration: all intervals except those three are bad => 12 bad. Let's see if our algorithm yields bad=12.
Compute using our method: For each i:
i=0: max_len=5. Process lengths until good:
L=1: mask=0 -> not good => bad++ (1)
L=2: mask from 0<<1|0=0? Actually bits: i=0: bits[0]=0, bits[1]=0 -> after L=2 mask=0? Wait careful: bits[0]=0, bits[1]=0. Starting mask=0, L=1: mask=0, bad. L=2: mask = (0<<1)|0 = 0 -> good[2][0] false => bad++ (2). L=3: bits[2]=1, mask = (0<<1)|1? Wait we need to track: After L=2, mask=0. For L=3, we shift left and add bits[2]=1: mask = (0<<1)|1 = 1. That is mask=1 for L=3. But earlier we had substring "001". Our precomputed good[3][1]? mask=1 binary 001 for L=3? Actually L=3 mask=1 means bits: (mask>>2)=0, (mask>>1)&1=0, (mask>>0)&1=1 => bits 0,0,1. That's correct. good[3][1]? check triple: 0,0,1 not all equal -> false -> bad++ (3). L=4: bits[3]=1, mask = (1<<1)|1 = 3 (binary 0011). good[4][3]? Check: bits 0,0,1,1. No triple -> false -> bad++ (4). L=5: bits[4]=1, mask = (3<<1)|1 = 7 (binary 00111). good[5][7]? Check if contains triple: Should be true because we found triple. So good[5][7] true -> not bad, break. So i=0 bad count = 4.
i=4: start index4, bits[4]=1, max_len=1.
L=1: mask=1 -> bad++ (1). So i=4 bad = 1.
Now sum bad: i0=4, i1=3, i2=2, i3=2, i4=1 => total bad = 12. total=15, answer=3. Matches.
Thus algorithm works.
Now final code.
Potential improvement: In precomputation, we could compute good for all L up to 8 using a more efficient method, but not needed.
Now let's consider if there is any scenario where length 9 intervals might not be good due to constraints on k? The van der Waerden theorem assumes the set is {1,2,...,N}. Our substring of length L corresponds to positions 1..L after shifting. So it applies directly. So it's good.
Now we should output answer.
Thus final answer.
But I want to double-check the maximum n=300k, and the total intervals number up to about 45e9, which fits in 64-bit. Python can handle.
Now about reading input: The string may be up to 300k characters, using input().strip() is fine.
Now we can write final solution with precomputation and loop.
But is there any catch about memory for good? No.
Now I think it's ready.
Now I'll produce final answer.
But before final, let's double-check that the problem statement doesn't have any twist: "there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}." This is exactly as we used.
Now note: The indices are 1-indexed in the problem, but we used 0-index. That's fine.
Now final solution.
We might also note that we could avoid precomputation and directly check triples for each interval of length up to 8. That would be simpler: For each start, for L from 1..min(8, n-i), extract substring s[i:i+L] and check triples by loops. Since substring extraction in Python creates new string, that could be expensive due to copying (O(L) each time). Better to avoid substring copies. So using mask and precomputed table is more efficient. But we can also check triples by directly indexing into s for each triple without creating substring. For each interval, we can check all triples by loops over x and k using original indices and access s. That avoids substring extraction and uses constant-time indexing. That might be even simpler to code and still fast. Since L ≤ 8, checking at most 12 triples, and we can directly use s as a string and compare characters. However, string indexing in Python is O(1) and character comparison is fast. But we would still need to compute the bounds correctly.
Let's estimate: For each interval, we loop over x in [l, r] and k. That's about 12 comparisons per interval. 2.4M intervals * 12 = 28.8M comparisons. That's acceptable. But we also have overhead of nested loops in Python (for x in range(l,r+1): for k...). That may be slower than using precomputed table because Python's loops are slower. The table method reduces inner loops to just a table lookup and mask update, which is very fast. The precomputation method is more efficient.
Nevertheless, both likely pass. But to be safe with large input, I'll implement the mask + table approach.
But note: In the mask approach, we need to convert characters to int bits. That's a O(n) step anyway.
Now we must be careful: When we compute mask = (mask << 1) | bits[i+offset], we are using ints. That's fine.
Now I'll produce the final code.
Potential issue: If the string is long and we use Python's int for mask, shifting left is fast.
Now final solution with explanation.
I'll also include comments to explain the van der Waerden property.
Now let's produce final answer. ```python
import sys
def precompute():
good = [None] * 9 # index 0 unused, 1..8 are used
for L in range(1, 9):
size = 1 << L
arr = [False] * size
for mask in range(size):
# extract bits: position 0 is the most significant (first character)
bits = [(mask >> (L - 1 - i)) & 1 for i in range(L)]
found = False
for x in range(L):
max_k = (L - 1 - x) // 2
for k in range(1, max_k + 1):
if bits[x] == bits[x + k] == bits[x + 2 * k]:
found = True
break
if found:
break
arr[mask] = found
good[L] = arr
return good
def solve() -> None:
s = sys.stdin.readline().strip()
n = len(s)
# total number of intervals [l, r]
total = n * (n + 1) // 2
code
# convert to list of integers 0/1 for fast bit operations
bits = [1 if ch == '1' else 0 for ch in s]
good = precompute()
bad = 0 # intervals of length ≤ 8 with no triple
for i in range(n):
mask = 0
max_len = min(8, n - i) # we only need lengths 1..8
for offset in range(max_len):
mask = (mask << 1) | bits[i + offset]
L = offset + 1
if not good[L][mask]:
bad += 1
else:
# this interval already contains a triple,
# all larger intervals with the same left border contain it as well.
break
# every interval of length ≥ 9 is automatically good (van der Waerden, W(2,3)=9)
answer = total - bad
print(answer)