A Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7.
The Little Elephant calls some string T of the length M balanced if there exists at least one integer X (1 ≤ X ≤ M) such that the number of digits 4 in the substring T[1, X - 1] is equal to the number of digits 7 in the substring T[X, M]. For example, the string S = 7477447 is balanced since S[1, 4] = 7477 has 1 digit 4 and S[5, 7] = 447 has 1 digit 7. On the other hand, one can verify that the string S = 7 is not balanced.
The Little Elephant has the string S of the length N. He wants to know the number of such pairs of integers (L; R) that 1 ≤ L ≤ R ≤ N and the substring S[L, R] is balanced. Help him to find this number.
Notes.
Let S be some lucky string. Then
|S| denotes the length of the string S;
S[i] (1 ≤ i ≤ |S|) denotes the i^th character of S (the numeration of characters starts from 1);
S[L, R] (1 ≤ L ≤ R ≤ |S|) denotes the string with the following sequence of characters: S[L], S[L + 1], ..., S[R], and is called a substring of S. For L > R we mean by S[L, R] an empty string.
Input
The first line of the input file contains a single integer T, the number of test cases. Each of the following T lines contains one string, the string S for the corresponding test case. The input file does not contain any whitespaces.
Output
For each test case output a single line containing the answer for this test case.
Constraints
1 ≤ T ≤ 10
1 ≤ |S| ≤ 100000
S consists only of the lucky digits 4 and 7.
Example
Input:
4
47
74
477
4747477
Output:
2
2
3
23
Explanation
In the first test case balance substrings are S[1, 1] = 4 and S[1, 2] = 47.
In the second test case balance substrings are S[2, 2] = 4 and S[1, 2] = 74.
Unfortunately, we can't provide you with the explanations of the third and the fourth test cases. You should figure it out by yourself. Please, don't ask about this in comments.
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 solve: Count number of substrings (contiguous) of a given string S of length N (up to 100k) consisting only of '4' and '7', such that the substring is "balanced": there exists an index X (1 ≤ X ≤ M, where M is length of substring) such that number of '4's in prefix T[1, X-1] equals number of '7's in suffix T[X, M]. That is: let A = count of '4's in positions 1..X-1, B = count of '7's in positions X..M. Then A = B.
Interpretation: For a given substring S[L, R] (call it T), we want existence of X (with L ≤ L+X-1 ≤ R, but X is relative position 1..M) such that #4 in T[1..X-1] = #7 in T[X..M].
Alternative: For the substring, consider the split point (relative X) within the substring. Let prefix (excluding the X-th character) and suffix (including X). Actually T[1,X-1] and T[X,M]; so X can be from 1 to M inclusive. At X=1, T[1,0] empty => A=0, B= #7 in T[1..M] i.e., number of 7's in whole substring. Balanced if 0 = total 7s? That would be total 7s =0, i.e., substring all 4s. At X=M+1? Not allowed since X≤M. So at X=M, T[1,M-1] and T[M,M]; then A = #4 in all but last char; B = #7 in last char only => balanced if last char is 7 and #4 in first M-1 equals 1? Actually B is #7 in T[M,M], so it's 1 if S[L+M-1]='7' else 0. So condition.
We need to count substrings that have at least one such split.
We need efficient counting O(N log N) or O(N) per test case, N up to 100k, T up to 10 => total 1e6.
We need to derive conditions for a substring to be balanced.
Let the substring T be given. Define:
Let total number of 4s: c4, total number of 7s: c7. M = c4 + c7.
We want existence of X such that #4 in prefix of length X-1 equals #7 in suffix from X to M.
Let prefix 4 count = a, suffix 7 count = b, and they satisfy a = b.
Note that total 4s = a + (number of 4s in suffix). Suffix consists of positions X..M. Let suffix 4 count = c4_suf, and suffix 7 count = b. So total c4 = a + c4_suf, and total c7 = (number of 7s in prefix) + b. But prefix length X-1 has a 4s and some 7s: let d = number of 7s in prefix. Then c7 = d + b.
We know that a = b, so b = a.
Thus c4 = a + c4_suf => a = c4 - c4_suf. And c7 = d + a.
But also note that d = (X-1) - a, since prefix length X-1 has a 4s, rest are 7s (only 4/7). So d = (X-1) - a.
But maybe not needed.
Alternative approach: The condition a = b can be re-expressed in terms of cumulative counts.
Let T be the substring. For relative positions i=1..M, define f(i) = (#4 in prefix up to i-1) - (#7 in suffix from i to M). Then we need existence of i with f(i)=0.
Now #4 in prefix up to i-1 = count_4[1..i-1]
#7 in suffix from i to M = total_7 - count_7[1..i-1]
So f(i) = count_4[1..i-1] - (total_7 - count_7[1..i-1]) = count_4[1..i-1] + count_7[1..i-1] - total_7 = (i-1) - total_7, because count_4+count_7 = i-1 (since only 4 and 7). Indeed count_4[1..i-1] + count_7[1..i-1] = i-1.
So f(i) = (i-1) - total_7.
That is surprisingly simple! Because both prefix and suffix counts depend on i and total_7. Let's verify:
Count_4(prefix up to i-1) = number of 4's before i.
Count_7(suffix from i) = total_7 - count_7(prefix up to i-1).
Then f = count_4(prefix) - (total_7 - count_7(prefix)) = (count_4+count_7) - total_7 = (i-1) - total_7.
Yes! So f(i) is independent of the distribution of 4 and 7; it only depends on i and total number of 7s in the substring.
Thus condition f(i)=0 becomes (i-1) = total_7. That is, there exists a split point i (1 ≤ i ≤ M) such that i-1 = total_7. Equivalently, i = total_7 + 1. Since i must be between 1 and M inclusive, the condition is: total_7 + 1 ≤ M (since i ≤ M) and also total_7 + 1 ≥ 1 (always). So the condition becomes: The length M of substring must be at least total_7 + 1. Also i = total_7+1 must be an integer in range.
But wait: Is there any other constraint? i must be integer between 1 and M. So condition is: total_7 + 1 ≤ M. But also i = total_7+1 must be such that the split point exists: X = total_7+1. But we must ensure that the definition of f(i) used only counts; it seems that for any substring, if total_7 + 1 ≤ M, then we can set X = total_7+1 and we get A = #4 in prefix up to total_7 =? Let's check: prefix length = total_7. Then #4 in that prefix is? Actually f(i) = (i-1) - total_7, which is zero. But does that guarantee that #4(prefix) = #7(suffix)? Since we derived f(i)=0 exactly that condition, yes. So any substring with M >= total_7 + 1 is balanced? But we need to verify carefully.
We derived: f(i) = (#4 in prefix up to i-1) - (#7 in suffix from i) = (i-1) - total_7. So setting (i-1)=total_7 yields zero. That's independent of arrangement. But careful: The suffix from i includes character i. At i = total_7+1, prefix length = total_7. The suffix length = M - total_7. Number of 7s in suffix =? It is total_7 - #7 in prefix. Since prefix length total_7, but not necessarily all are 7? Actually prefix length total_7. It could contain some 4s. But the condition we set is that the difference is zero. So indeed #4(prefix) = total_7 - #7(prefix) maybe? Let's compute: #4(prefix) = (i-1) - #7(prefix) = total_7 - #7(prefix). And #7(suffix) = total_7 - #7(prefix). So they equal. So yes.
But wait: What about the requirement that i must be an integer between 1 and M. So condition is M >= total_7+1. However, is it also necessary that total_7+1 >= 1? Always true. So the condition is simply M > total_7? Since M >= total_7+1 <=> M > total_7. So any substring where length > number of 7s is balanced? Let's test with examples.
Example: S = "4". length=1, total_7=0, M > total_7? 1 > 0 => balanced? But earlier they said S="7" is not balanced. For "7", length=1, total_7=1, M > total_7? 1 > 1 false, so not balanced. So matches. For "4", balanced? According to the problem: For a substring of length 1, is "4" balanced? Let's test: Need X (1..1). For X=1: #4 in prefix T[1,0]=0, #7 in suffix T[1,1] is 0 because char is 4 => 0=0, so yes, it is balanced. So "4" is balanced. Good. For "47": length=2, total_7=1, M > total_7? 2>1 true => balanced. Indeed they counted "47" as balanced. Also "74": length=2, total_7=1, 2>1 => balanced, they counted "74" as balanced. But they also had "4" as balanced? In first test case, they counted S[1,1]=4 and S[1,2]=47. So indeed "4" counted. In second test case, "74" and "4". So matches.
Third test case: "477". Let's compute possible substrings: positions:1:4,2:7,3:7. Substrings:
[1,1]="4": M=1,t7=0=>balanced.
[2,2]="7": M=1,t7=1=>not balanced.
[3,3]="7": not balanced.
[1,2]="47": M=2,t7=1=>balanced.
[2,3]="77": M=2,t7=2=>2>2? false => not balanced.
[1,3]="477": M=3,t7=2=>3>2 true => balanced.
So balanced substrings: "4","47","477" => count 3. That matches output 3.
Fourth test case: "4747477": length 7. They say answer 23. We'll test later.
Thus the condition is: A substring S[L,R] is balanced iff (R-L+1) > (# of 7's in that substring). In other words, length > count_7. Equivalent: count_4 >= 1? Not exactly; because length > count_7 means count_4 + count_7 > count_7 => count_4 > 0. So actually balanced iff there is at least one '4'? Wait: length > count_7 => count_4 > 0. So if the substring contains at least one '4', then it's balanced? Let's test: substring "777": length=3, count_7=3, length > count_7? false, not balanced. Indeed no 4. Substring "774": length=3, count_7=2, count_4=1 => length > count_7 true => balanced. Check if "774" is balanced: positions: 7,7,4. length=3, total_7=2, M > total_7 => 3>2, we set X = total_7+1=3, then prefix T[1,2]="77": #4 in prefix=0, suffix T[3,3]="4": #7 in suffix=0, balanced. Yes. What about "474": length=3, total_7=1 => balanced. Indeed. What about "4" balanced, "44" length=2 total_7=0 balanced, "447" length=3 total_7=1 balanced, "4477" length=4 total_7=2 balanced. So any substring that contains at least one '4' appears to be balanced. But is that always true? Let's test a case: "7 4"? Already balanced. "7 4 7"? length=3, total_7=2, count_4=1 => length> total_7? 3>2 yes => balanced. "7 4 4"? total_7=1, length=3>1 balanced. "4 7 7"? we tested. "4 4 7"? balanced. So indeed if there is at least one '4', then count_7 ≤ length-1, so length > count_7? Since count_4≥1 => length = count_4+count_7 ≥ 1+count_7 => length > count_7. So yes, length > count_7 always. So the condition reduces to: substring contains at least one '4'.
But wait: check substring "7 7 4"? that's got a 4, so balanced. Substring "4 7"? balanced. So indeed any substring that contains at least one 4 is balanced. And substrings with no 4 (i.e., all 7's) are not balanced because length = count_7, so length > count_7 fails. However, what about empty substring? Not considered. So the problem reduces to: count number of substrings that contain at least one '4'.
But careful: The definition of balanced required existence of X (1 ≤ X ≤ M). For substrings containing at least one '4', is it always possible to find such X? According to derived condition, if there is at least one '4', then length > count_7, so we set X = count_7+1. But is it guaranteed that X lies within [1, M]? Since length > count_7, count_7+1 ≤ length, okay. So that split works regardless of the arrangement. So indeed any substring with at least one 4 is balanced.
But we should double-check the derivation: f(i) = (i-1) - total_7. This holds because we used count_4(prefix) + count_7(prefix) = i-1. That's correct because all characters are either 4 or 7. So the condition is purely based on i and total_7. So indeed the existence condition is that there exists integer i (1..M) such that i-1 = total_7. That is exactly total_7 + 1 ≤ M. So it's exactly length > total_7. So any substring with length > total_7 is balanced. Since total_7 ≤ length, and equality only if all chars are 7. So condition is equivalent to substring not all 7's.
Thus the answer for a given S is the number of substrings that are not all 7's. That is total number of substrings minus number of substrings consisting only of 7's.
Total substrings = N*(N+1)/2.
Number of substrings consisting only of 7's: For each maximal contiguous run of 7's of length L, number of substrings within that run is L*(L+1)/2. Sum over runs.
Thus answer = total - sum_{runs of 7} L_i*(L_i+1)/2.
But is that correct? Let's test with examples.
Example 1: "47". N=2, total substrings=3. Runs of 7: there is a single '7' at position 2? Actually string "4 7". Run of 7: length 1. So subtract 1*2/2=1 => answer=3-1=2. Matches.
Example 2: "74". Run of 7: first character is 7? Actually string "7 4". Run of 7: length 1 at position 1. So subtract 1 => total 3-1=2. Matches.
Example 3: "477". N=3, total=6. Runs of 7: positions 2-3 have "77" length 2, so subtract 2*3/2=3. Also perhaps single 7? But the run includes both, so we only count once. So subtract 3, answer=3. Matches.
Example 4: "4747477". N=7, total=78/2=28. Identify runs of 7: The string: 4,7,4,7,4,7,7. So runs of 7: positions 2: single 7? Actually it's isolated? Sequence: index1:4, 2:7, 3:4, 4:7, 5:4, 6:7, 7:7. So runs: at index2: length1; index4: length1; indices6-7: length2. So sum of lengths: run1 L=1 => 1; run2 L=1 =>1; run3 L=2 =>3 (23/2=3). Total subtract =1+1+3=5. Then answer =28-5=23. Matches output.
Thus indeed solution reduces to counting substrings with at least one 4. Very simple!
But wait: Is there any nuance? Let's double-check the derivation carefully to avoid pitfalls: The condition is existence of X such that number of 4's in T[1..X-1] equals number of 7's in T[X..M]. We set prefix length = X-1, suffix starts at X. Our derivation used the fact that in T, all characters are either 4 or 7. So total number of characters in prefix = X-1, and number of 7's in suffix = total_7 - number of 7's in prefix. So condition becomes: #4(prefix) = total_7 - #7(prefix). But #4(prefix) = (X-1) - #7(prefix). So equation: (X-1) - #7(prefix) = total_7 - #7(prefix) => X-1 = total_7. So indeed independent of #7(prefix). So it works for any X satisfying X-1 = total_7. But X must be an integer position. The equation X = total_7 + 1. For this to be a valid split, X must be between 1 and M inclusive. So we need total_7 + 1 ≤ M, i.e., total_7 ≤ M-1, i.e., M > total_7. That's it. So if M > total_7, then X = total_7+1 is a valid split, and the condition holds.
Thus balanced iff M > total_7. Since total_7 is number of 7s, M > total_7 implies at least one 4. Conversely, if at least one 4, then total_7 ≤ M-1, so condition holds. So balanced iff substring contains at least one '4'.
Thus problem is trivial: answer = total substrings - sum_{maximal consecutive 7 blocks} len*(len+1)/2.
Now we need to handle T up to 10, each length up to 100000. So just scan and compute.
Edge cases: empty string? N≥1. All 4s: then there are no runs of 7, subtract 0, answer = total substrings. All 7s: answer = 0. Because no substring contains a 4. That matches condition: any all-7 substring is not balanced.
Thus we can implement.
But before coding, let's ensure there's no trick: The original problem statement says "The Little Elephant calls some string T of the length M balanced if there exists at least one integer X (1 ≤ X ≤ M) such that the number of digits 4 in the substring T[1, X - 1] is equal to the number of digits 7 in the substring T[X, M]." Our derivation seems solid. However, we might check: what about substrings that contain a 4 but also many 7s? For example T="474". M=3, total_7=1, M>1, X=2. Prefix T[1]="4": #4=1, suffix T[2,3]="74": #7=1. Balanced. Works. What about T="4477"? M=4, total_7=2, X=3. Prefix "44": #4=2, suffix "77": #7=2. Balanced. Works. So yes.
But wait: What about T="7 4"? we tested.
Thus answer is as described.
Now, is there any possibility that a substring with at least one 4 might not be balanced because X would be such that the split point lies inside a 4? But the derivation doesn't require anything about the character at X; it's only based on counts. So it's fine.
Thus solution is simple.
Given the constraints, we can compute quickly.
Implementation: For each test case:
Read string S (no whitespace, just a line). Watch out: There could be newline, but typical input.
N = len(S).
total_substrings = N*(N+1)//2.
Count substrings of all 7's: iterate over S, find maximal contiguous runs of '7's. For each run length L, subtract L*(L+1)//2.
Print total_substrings - sum_7_substrings.
Complexity O(N) per test.
We must use 64-bit integers because N up to 1e5, total substrings ~5e9 fits in 64-bit (since 1e5*1e5/2 = 5e9). For multiple test cases, still within 64-bit.
Now, we need to write a Python solution. Since T up to 10, total length up to maybe 1e6, Python fine.
Edge Cases:
Input may have trailing spaces? We'll strip.
Could there be empty lines? Not likely.
Testing with sample:
Input:
4
47
74
477
4747477
We'll compute as above.
Thus answer.
But wait, I recall that there is known problem "Little Elephant and Strings" or similar, but this simplification is correct.
Before finalizing, let's double-check with a few more examples:
S="4": total=1, no 7 runs, ans=1. Balanced? yes.
S="7": total=1, run of 7 length=1, subtract 1 => 0. Balanced? No, as per problem, "7" is not balanced. Good.
S="44": total=3, runs of 7: none, ans=3. List substrings: "4", "4", "44". All contain at least one 4. Are they all balanced? Check "44": M=2, total_7=0, X=1? total_7+1=1, prefix empty (0), suffix "44": #7=0, balanced. Yes. So 3 balanced.
S="47": we saw 2.
S="74": 2.
S="77": total=3, subtract 3 (run len2 => 3) => 0. Balanced? "7","7","77". None have 4, so not balanced. Good.
S="747": N=3 total=6. Runs of 7: There is a run of length 1 at position2? Also maybe at? Actually S: 7,4,7. So runs: first char 7 (L=1), last char 7 (L=1). So subtract 1+1=2. Ans=4. List substrings: positions:
[1,1]="7": no
[2,2]="4": yes
[3,3]="7": no
[1,2]="74": yes (contains 4)
[2,3]="47": yes (contains 4)
[1,3]="747": yes (contains 4) => total 4. Good.
S="4747": N=4 total=10. Runs of 7: at positions 2 and 4, each isolated length1 => subtract 1+1=2 => ans=8. Substrings with no 4: only those entirely in 7's? There is no contiguous block longer than 1, so substrings that are single 7's: two of them. So 10-2=8. Works.
Thus solution stands.
We can even prove: balanced if and only if substring contains at least one 4.
Now implement.
But careful: The problem statement says "The Little Elephant has the string S of the length N. He wants to know the number of such pairs of integers (L; R) that 1 ≤ L ≤ R ≤ N and the substring S[L, R] is balanced." So indeed counting substrings.
Thus answer = total substrings - sum over runs of 7s of (len_run choose 1? Actually number of substrings within each run is len*(len+1)/2.
Thus done.
We'll implement solve().
Potential pitfalls: The input file does not contain any whitespaces, meaning each test case string is on its own line, without extra spaces. Use sys.stdin.read or sys.stdin.readline.
We'll parse T, then for each line read string, possibly stripping newline.
Make sure to handle large output.
Now, for completeness, we might discuss that the simplification indeed matches the original problem examples and constraints.
Thus final code.
We'll also note that the derived condition is very neat: balanced if and only if there is at least one '4'.
Edge case: If the string is very long, but our algorithm is linear.
Time complexity: O(total length) across test cases.
Thus answer.
Now we can produce final solution.
But wait, is there any tricky scenario where a substring contains a '4' but still might not be balanced? Let's check: Suppose M = total_7 + 1, then X = total_7+1 works. But what if M > total_7+1? Then also X = total_7+1 works because it's still ≤ M. So still works. So always works.
Thus confirmed.
So final solution.
Now I'll write explanation and code.
Will code in Python:
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)
total = n*(n+1)//2
subtract = 0
i = 0
while i < n:
if s[i] == '7':
j = i
while j < n and s[j] == '7':
j += 1
L = j - i
subtract += L*(L+1)//2
i = j
else:
i += 1
out.append(str(total - subtract))
sys.stdout.write("\n".join(out))
But note: data split splits on whitespace, which includes newline. Since strings consist only of digits 4 and 7, no spaces, fine.
Alternatively, use sys.stdin.readline.
Now test with given example.
Works.
I'll produce final answer.
But before final, think about if any substring of all 7's could be balanced if we choose X differently? According to derivation, f(i) = (i-1)-total_7 = (i-1)-M, because total_7 = M. Then f(i) = i-1-M, which ranges from -M to -1? For any i, f(i) = i-1-M, never zero because i-1 ≤ M-1. So indeed no zero. So all 7 substrings are not balanced.
Thus correct.
We'll output answer.
One more check: The problem statement's definition says "there exists at least one integer X (1 ≤ X ≤ M) such that the number of digits 4 in the substring T[1, X - 1] is equal to the number of digits 7 in the substring T[X, M]." In our derived condition we didn't use the character at X itself. So it's fine.
Thus final answer is trivial.
But wait, I recall that CodeChef problem "LEBALONS" or something maybe? Actually it's a problem "Balanced Strings" maybe. However, my reasoning seems correct.
We'll provide explanation.
Now produce final output. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out = []
pos = 1
for _ in range(t):
s = data[pos]
pos += 1
n = len(s)
total_substrings = n * (n + 1) // 2
code
# subtract substrings consisting only of '7'
subtract = 0
i = 0
while i < n:
if s[i] == '7':
j = i
while j < n and s[j] == '7':
j += 1
L = j - i # length of this maximal 7‑block
subtract += L * (L + 1) // 2
i = j
else:
i += 1
out.append(str(total_substrings - subtract))
sys.stdout.write("\n".join(out))