You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.
More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .
Input
The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| ≤ 109) — the elements of array a.
Output
Print a single integer — the number of ways to split the array into three parts with the same sum.
Examples
Input
5
1 2 3 0 3
Output
2
Input
4
0 1 -1 0
Output
1
Input
2
4 1
Output
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 ways to split array into three contiguous parts with equal sum. That is, find indices i, j such that sum_{1..i-1} = sum_{i..j-1} = sum_{j..n} and i >=2, j <= n-1? Actually problem statement says: find number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that sum_{1}^{i-1} = sum_{i}^{j-1} = sum_{j}^{n}. Wait condition "2 ≤ i ≤ j ≤ n - 1". Yes, i can be from 2 to n-1 inclusive, j from i to n-1 inclusive. Then parts are: first part from 1 to i-1, second from i to j-1, third from j to n. So each part non-empty? Since i-1 >=1, so first part at least one element. Second part from i to j-1, if j=i then second part is empty? Actually if j=i, then second part indices i to i-1, which is empty. But condition says "2 ≤ i ≤ j ≤ n - 1", j can equal i. That would give second part empty, sum zero. But can sum of empty part be equal to sums of other parts? Possibly if sum of first part and third part are both zero? But then second part empty sum zero, so it could be valid if total sum zero? But is it allowed? Typically splitting into three parts means each part non-empty? The problem statement: "split all the elements of the array into three contiguous parts". That implies we partition the array into three contiguous segments that together cover all elements. So each part should contain at least one element. Since it says "split all the elements" and "three parts", I'd think each part must be non-empty. The constraints on indices: 2 ≤ i ≤ j ≤ n-1. Let's examine: i is start of second part, j is start of third part. The segments are [1, i-1], [i, j-1], [j, n]. For these to be non-empty, we need i-1 >= 1 => i>=2 (okay), j-1 >= i => j >= i+1? Actually if j = i, then second part [i, i-1] empty. So to have second part non-empty, we need j >= i+1. But constraint says j can be i. So maybe empty parts are allowed? However typical problem "Split the array into three parts with equal sum" (like Codeforces problem 466C?) is known: Number of ways to split array into three parts with equal sum. Usually indices are i, j such that 1 <= i < j < n, and sum(1..i) = sum(i+1..j) = sum(j+1..n). That is more standard. Let's check known problem: Codeforces 466C - Number of Ways. Indeed the problem is: Given an array, count number of ways to split into three contiguous parts with equal sum. The indices i,j such that sum(1..i) = sum(i+1..j) = sum(j+1..n). There, i and j are indices that end first and second parts. i from 1 to n-2, j from i+1 to n-1. So i is end index of first part, j is end index of second part. That ensures each part non-empty. The input constraints: n up to 5e5. Output count, maybe large, use 64-bit.
But here problem statement: "pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that sum_{1}^{i-1} = sum_{i}^{j-1} = sum_{j}^{n}" . This is equivalent to i being start of second part, j start of third part. Equivalent to typical definition with i'=i-1 end of first part, j'=j-1 end of second part. Then conditions become i' from 1 to n-2, j' from i' to n-1? Actually j'=j-1, i'=i-1. Then i'>=1 (since i>=2) and j' <= n-1? Actually j <= n-1 => j-1 <= n-2, so j' <= n-2? Wait typical condition: j <= n-1 => third part starts at j, so third part from j to n, which includes at least one element because j <= n-1 ensures n>=j, so third part non-empty. For second part to be non-empty, we need j >= i+1 => j-1 >= i, i.e., j' >= i'? Since i' = i-1, j' = j-1, then condition j >= i+1 => j-1 >= i => j' >= i' + 1? Actually i = i'+1, j >= i+1 => j >= i'+2 => j' = j-1 >= i'+1. So j' > i' . So second part non-empty if j' > i'. The given condition allows j = i, which would give j' = i'-1? Wait if j=i, then j-1 = i-1? Actually if i=j, then j = i, so j-1 = i-1, so j' = i'-1? Since i'=i-1, then j' = j-1 = i-1 = i'. Actually if i=j, then i-1 = j-1, so i' = j'? So j' = i'. That yields second part empty because second part from i to j-1 = i to i-1, empty. So typical problem requires non-empty parts, so we need j > i (strict). But note the given bounds say i ≤ j, not i < j. But maybe they allow j = i? Let's test examples: Example1: n=5, array [1,2,3,0,3]. Total sum = 9? 1+2+3+0+3 = 9. So each part sum = 3. For split into three parts with equal sum 3. The ways: indices: i=2, j=4? Let's compute: First part [1] sum=1, not 3. Actually typical known answer for this array is 2: splits: (1,2,3,0,3) => first part [1,2] sum=3, second [3] sum=3, third [0,3] sum=3? That would be i=2 (end index of first part) and j=3? Actually if we use definition with ends: i=2 (first part ends at 2), j=3 (second part ends at 3). Then parts: 1..2 = 1+2=3, 3..3=3, 4..5=0+3=3. That works. Another split: first part [1,2,3]? That sum=6 not 3. Actually known answer is 2: also i=3? Let's find: First part [1,2] sum=3, second [3,0] sum=3, third [3] sum=3 => i=2, j=4? But if j=4 (second part ends at 4) then first part 1..2=3, second part 3..4=3+0=3, third part 5..5=3. That also works. So two ways: (i=2, j=3) and (i=2, j=4) in typical notation where i is end of first, j end of second. That gives i from 1 to n-2, j from i+1 to n-1. In the alternative notation with i start of second, j start of third: For first split: i=3? Let's map: typical (i_end=2, j_end=3). Then first part indices 1..2, second 3..3, third 4..5. So start of second part = 3, start of third part = 4. So i=3, j=4. Check: i=3, j=4 (both between 2 and n-1=4). Condition i ≤ j? Yes 3 ≤ 4. Sums: first = a[1..2]=3, second = a[3..3]=3, third = a[4..5]=3. Works. Second split: typical (i_end=2, j_end=4). Then first 1..2, second 3..4, third 5..5. So i=3, j=5? start third = 5. i=3, j=5, but n=5 so j can be 5? But constraint says j ≤ n-1 =4. So j=5 is not allowed. Hmm. Actually in typical split, second ends at 4, third starts at 5. That would be j=5, but j must be ≤ n-1 in the given constraint? Because third part starts at j and must have at least one element, so j ≤ n. But the problem statement says j ≤ n-1? Let's reread: "pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1)". That says j ≤ n-1. That would forbid j = n, i.e., third part would start at n? Actually if j=n, third part is a[n..n] one element, but j ≤ n-1 means j cannot be n. So third part always starts at index ≤ n-1, meaning third part has at least two elements? Wait if j = n-1, third part is a[n-1..n], length 2. So third part always has at least 2 if j = n-1? Actually if j = n-1, then third part indices n-1 to n, length 2. If j = n, then third part would be index n only, length 1, but j ≤ n-1 forbids that. That seems odd. Typically third part can have length 1. Let's check typical problem: In Codeforces 466C, they use indices i, j such that 1 ≤ i < j < n. So i is end of first part, j is end of second part. Then third part is j+1..n. So third part length at least 1 because j < n ensures j+1 ≤ n. So third part can be length 1. In our alternative, j is start of third part. If j ≤ n-1, then third part is from j to n, length at least 2? Because if j = n-1, length 2; if j = n, length 1 but j ≤ n-1 forbids j=n. So third part min length 2. That's different. Let's test example: the second split third part is just element 5, length 1. So in our notation j would be 5, but j=5 is not allowed (since n-1=4). So only one split would be counted? But example output is 2. So our interpretation must be different: maybe the condition "2 ≤ i ≤ j ≤ n - 1" is for i and j as end indices? Let's read carefully: The problem statement: "More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that ." The image likely shows summations: sum_{k=1}^{i-1} a_k = sum_{k=i}^{j-1} a_k = sum_{k=j}^{n} a_k. Yes, that's typical notation: i is the start of second segment, j is the start of third segment. Then condition i ≤ j, and also i ≥ 2 ensures first segment non-empty, and j ≤ n - 1 ensures third segment non-empty? Actually if j = n, then third segment is a[n..n], non-empty. But they bound j ≤ n-1, meaning j cannot be n, so third segment would be at least from n to n? That would require j ≤ n? Hmm. Let's think: If j = n, then sum_{j}^{n} = a[n], which is one element. That should be allowed. Why would they say j ≤ n-1? Maybe they consider j as end index of second part? Wait if j is end index of second part, then condition would be sum_{1}^{i} = sum_{i+1}^{j} = sum_{j+1}^{n}. Then i and j are end indices. Then condition i from 1 to n-2? They say i from 2 to n-1? That doesn't match. Let's parse: "pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1)". If i is end index of first part, then first part sum_{1}^{i}, so i must be at least 1, but they say i ≥ 2, so maybe they want first part at least two elements? That seems unlikely. If i is start of second part, i ≥ 2 ensures first part at least one element (index 1 to i-1). j ≤ n-1 ensures third part at least one element? If j ≤ n-1, then third part from j to n, length n - j + 1. For j = n-1, length 2; j = n, length 1 but not allowed. So third part would have length at least 2? But typical splitting doesn't require min length 2. So maybe j is end index of second part? Let's test: Suppose i is end index of first part, j is end index of second part. Then conditions: sum_{1}^{i} = sum_{i+1}^{j} = sum_{j+1}^{n}. To have three non-empty parts, we need i ≥ 1, i+1 ≤ j, j ≤ n-1. So i from 1 to n-2, j from i+1 to n-1. That's standard. Now if we rename variables: let i' = i, j' = j, the constraints given say (2 ≤ i ≤ j ≤ n-1). If i is end index, then i≥2 means first part must have at least 2 elements? Not necessary. Example1: first split had i=2 (end of first part). That's i=2, okay. Second split had i=2 also. So both have i=2, which is ≥2, okay. But j values: first split j=3, second split j=4. Both j ≤ n-1? n-1=4, so j=3 and 4 both allowed. So (i,j) pairs (2,3) and (2,4) satisfy 2 ≤ i ≤ j ≤ n-1? i=2, j=3: 2≤2≤3≤4 yes. (2,4): 2≤2≤4≤4 yes. So both valid. In this interpretation, i is end index of first part, j is end index of second part. Then the summations would be: sum_{1}^{i} = sum_{i+1}^{j} = sum_{j+1}^{n}. But the problem statement's image likely shows sum from 1 to i-1, i to j-1, j to n. But maybe the image is different? Without seeing image, we need to deduce.
Given typical problem "Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same." This is exactly Codeforces 466C. The standard formalization: find pairs (i, j) with 1 ≤ i < j < n such that sum_{1}^{i} = sum_{i+1}^{j} = sum_{j+1}^{n}. However, here they state (2 ≤ i ≤ j ≤ n - 1) and summations from 1 to i-1, i to j-1, j to n. That's equivalent: just shift indices: define i' = i-1, j' = j-1, then i' from 1 to n-2, j' from i' to n-2? Actually j' from i' to n-2? Let's map: i' = i-1, j' = j-1. Given 2 ≤ i ≤ j ≤ n-1 => 1 ≤ i' ≤ j' ≤ n-2. And conditions: sum_{1}^{i-1} = sum_{1}^{i'} = first part; sum_{i}^{j-1} = sum_{i'+1}^{j'} = second part; sum_{j}^{n} = sum_{j'+1}^{n} = third part. So it's exactly the same as standard if we require j' ≥ i'? Actually standard requires i' < j'? Because second part must be non-empty: if i' = j', then second part sum from i'+1 to i' is empty? Wait if i' = j', then second part sum_{i'+1}^{i'} empty sum=0, which would be allowed only if total sum 0? But typical problem requires non-empty parts, i.e., i' < j'. However the condition given is i ≤ j, which translates to i' ≤ j'. So includes i' = j'. So they might allow empty second parts? But check example: n=4, array [0,1,-1,0] output 1. Let's compute: total sum = 0. Splits that work? According to standard problem, number of ways for that array is 1? Actually known example: Input: 4 \n 0 1 -1 0 -> output 1. Let's find split: first part [0] sum=0, second part [1, -1] sum=0, third part [0] sum=0. That would be i=1 (end of first part), j=3? Actually i=1, j=3? But j must be less than n (4), so j=3 works: first 1..1=0, second 2..3=1+(-1)=0, third 4..4=0. That yields (i,j)=(1,3). However i=1 is not >=2, so not allowed in given constraint. Another split: first part [0,1] sum=1 not 0. Another: first part [0,1,-1] sum=0, second part [0]? Then i=3, j=4? But j must be less than n? j=4? Actually j < n, so j=4 not allowed. So only one split: i=1, j=3. But given constraint (2 ≤ i ≤ j ≤ n-1) would exclude i=1. So maybe they count differently. Let's see if there's another split using their definition with i as start of second part? If i is start of second part, then for the split above, first part [0] sum0, second part [1,-1] sum0, third part [0] sum0. Then i=2 (start second), j=4 (start third). j=4? But j ≤ n-1 =3, so j=4 not allowed. So that split not allowed either. So maybe they allow j=n? Let's check: If they allow j ≤ n, then j=4 would be allowed if n=4 and j ≤ n? But constraint says j ≤ n-1, so no.
Maybe the array [0,1,-1,0] has another split: i=2, j=3? Then first part [0,1]? Actually if i=2, j=3, then first part a[1]=0? Wait i=2 => first part indices 1 to 1: [0] sum0; second part indices 2 to 2: [1] sum1; third part indices 3 to 4: [-1,0] sum -1. Not equal. i=3, j=3? first 1..2: [0,1] sum1, second 3..2 empty sum0, third 3..4: [-1,0] sum -1. Not equal. i=3, j=4? But j ≤ n-1=3, so j=4 not allowed. So no split if using i start second, j start third with j ≤ n-1. Yet output is 1. So maybe the constraint is actually (1 ≤ i < j < n) but they wrote (2 ≤ i ≤ j ≤ n - 1) by mistake? But the examples match typical Codeforces problem. In example2, known answer is 1 for array [0,1,-1,0] under standard definition. Indeed Codeforces 466C example: Input: 4 \n 0 1 -1 0 -> Output: 1. That matches. So it's the standard problem.
Thus I'm convinced it's the standard problem: Count pairs (i, j) with 1 ≤ i < j < n such that sum_{1..i} = sum_{i+1..j} = sum_{j+1..n}. Or equivalently, sum_{1..i} = total/3 and sum_{1..j} = 2*total/3, with total divisible by 3. So we need to compute prefix sums and count.
Given constraints n up to 5e5, a[i] up to 1e9 in absolute, total sum can be large, but we can use 64-bit.
We need to output the number of ways.
Thus algorithm: compute total sum S. If S % 3 != 0, answer 0. Else, let target = S / 3. Then compute prefix sums P[k] = sum_{1..k}. Then we need to count pairs (i, j) such that 1 ≤ i < j < n, P[i] == target, P[j] == 2target. But careful: P[i] == target ensures sum first part = target. Then second part sum from i+1 to j should also equal target, which is equivalent to P[j] - P[i] = target => P[j] = 2target. And automatically third part sum = target. So we just need to count number of indices i with P[i] = target (i from 1 to n-2) and for each such i, number of indices j > i and j < n with P[j] = 2target. That can be done by scanning from right to left, counting occurrences of 2target after each i.
Alternatively, we can first compute an array of count of prefix sum equal to 2target for indices from n down to 1, but j must be < n, i.e., j from 2 to n-1? Actually j < n, so j can be up to n-1. Also i < j, i can be from 1 to n-2. So we can count for each i (with P[i]==target), the number of j > i, j <= n-1 such that P[j]==2target. We can precompute suffix count for j positions. For each index pos, let suffix_count[pos] = number of indices k >= pos such that P[k]==2target and k < n? Actually we need to include only j < n, so we can precompute an array cnt2[pos] = number of indices k in [pos, n-1] (since j <= n-1) with P[k]==2target. Then answer = sum over i from 1 to n-2 where P[i]==target of cnt2[i+1] (since j > i, so j from i+1 to n-1). That's O(n).
Edge cases: if target = 0, we need to be careful. But the counting method works regardless.
But careful: For i, we need P[i] = target. However, we also need that i < j and j < n. So i can be up to n-2. Also j must be at least i+1, and j <= n-1. In our suffix count, we include j up to n-1. For i = n-1? Not allowed because i < j < n, so i <= n-2. So fine.
Implementation: read n, array a. Compute prefix sums in 64-bit (maybe long long). Also compute total S. If S % 3 != 0, print 0. Else compute target = S / 3. Then compute array cnt2 suffix: let suffix[n+1] = 0? We'll need indices from 1 to n. We'll iterate from n down to 1, maintaining count. But j only allowed up to n-1, so we must not count index n even if P[n]==2target? But P[n] = total sum = 3target, so it equals 3target, not 2target unless target=0? Actually if target=0, then 2target=0, and P[n]=0 as well. But j must be < n, so index n cannot be used as j because then third part would be empty. So we must exclude index n. So when building suffix count, we should only include indices 1..n-1? But we need suffix counts starting from i+1 to n-1. So we can define an array cnt2 of size n+2, and compute it for positions from n-1 down to 1: cnt2[i] = (P[i]==2target ? 1 : 0) + cnt2[i+1]. But careful: for i = n-1, cnt2[n-1] = (P[n-1]==2*target?1:0) + cnt2[n] where cnt2[n] we can set to 0. And then for i = n, not needed. So we can compute like that.
Then answer = 0; for i from 1 to n-2: if P[i] == target: ans += cnt2[i+1].
Note: if target = 0, then P[i] = 0 qualifies, and cnt2[i+1] counts indices with P[j] = 0 (j > i, j <= n-1). But be careful: j can equal i+1? Yes. That's fine. But we must ensure that second part is non-empty: j > i ensures second part has at least one element? Actually if j = i+1, then second part is from i+1 to j-1 = i+1 to i, empty? Wait if j = i+1, then second part sum from i+1 to (j-1)=i, empty, sum = 0. But that would be allowed if target = 0? However, standard problem usually requires non-empty parts, but in Codeforces 466C, they do require i < j < n, and that i and j are end indices of first two parts, which ensures each part non-empty: first part 1..i (non-empty because i>=1), second part i+1..j (non-empty because j>=i+1), third part j+1..n (non-empty because j <= n-1). So j = i+1 yields second part with one element? Actually if i is end index of first part, and j is end index of second part, then second part is from i+1 to j. If j = i+1, then second part includes exactly one element: i+1. That is non-empty. So the mapping to i (end of first) and j (end of second) ensures non-empty parts. In our derived method using prefix sums, we have condition P[i] = target and P[j] = 2target, with i < j and j < n. Then second part sum = P[j] - P[i] = target, which is non-zero? Not necessarily; target could be zero. But second part could be empty? Does i < j guarantee second part non-empty? Let's examine: The indices: first part: 1..i, second part: i+1..j, third: j+1..n. So second part has at least one element because i+1 <= j. So yes non-empty. So i < j ensures that. However, in our method using prefix sums, we directly count i and j where P[i]=target and P[j]=2target, and we only consider i < j. That's fine.
Now verify with example2: n=4, a=[0,1,-1,0]. total=0, target=0. Compute prefix: P1=0, P2=1, P3=0, P4=0. Indices: i can be 1..2 (since i < j < n, n=4 so i max 2). i candidates where P[i]==0: i=1 (P1=0), i=2? P2=1 not 0, i=3? i cannot be 3 because i< j < n and i max 2? Actually i can be up to n-2=2. So only i=1. Now j must be > i and < n, so j can be 2 or 3. We need P[j]==2*target=0. Check j=2: P2=1 not 0; j=3: P3=0 qualifies. So count 1. So answer 1. Works.
Now example1: n=5, a=[1,2,3,0,3]. total=9, target=3. Prefix: P1=1, P2=3, P3=6, P4=6, P5=9. i from 1 to 3 (n-2=3). i candidates: P1=1 not; P2=3 yes; P3=6 not; So i=2 only. j > i and <5: j can be 3,4. Need P[j]=2*target=6. P3=6 yes; P4=6 yes. So count 2. answer 2. Works.
Third example: n=2, a=[4,1] total=5 not divisible by 3, answer 0. Good.
So algorithm is correct.
Now we need to implement in C++ (since the conversation doesn't specify language, but we can just provide solution description? The user asked: "You've got array a[1], ..., a[n]. Count number of ways... Input... Output...". Possibly they want solution. Usually these are problem statements from codeforces. So we need to produce code. The user might be asking for solution. I'll write a solution in C++.
But the instruction says: "You are ChatGPT, ...". So likely we need to provide final answer with explanation. However the user provided the problem statement only. They might want code or explanation. Usually in these tasks, we should produce a solution, maybe with code. But the conversation is ambiguous. However typical approach: we output a single integer, so we could just implement solve().
Given that it's a problem from Codeforces, the expected answer is a program. I'll write a C++ solution.
Complexities: O(n) time, O(1) extra space if we compute suffix count on the fly? Actually we can compute suffix count in one pass: first count total number of indices j (with j < n) such that P[j] == 2target. Then while iterating i from 1 to n-2, we can decrement the count when we pass a j? But careful: we need for each i, count of j > i. So we can iterate i from 1 to n-2 and maintain a count of how many j (with j > i) satisfy P[j]==2target. We can precompute an array of occurrences for j from 1 to n-1. Then as we move i forward, we can subtract if current i+1 is a j? Actually we want for each i, count of j in (i, n-1] with property. We can compute an array cnt2 where cnt2[i] = number of indices k in [i, n-1] with P[k]==2*target. That requires an array of size n, or we can compute on the fly from right to left. But we can also do:
Initialize a variable cnt_j = 0; for j from n-1 down to 2? But we need to sum over i. Better: compute total count of j in [2, n-1]? Actually we need to exclude i itself. Approach: First compute array of prefix sum. Then iterate from n-1 down to 1 to compute suffix count of P[k]==2*target for k < n. Then loop i.
Simpler: we can first compute the prefix sums, then create a vector of bool or count. But we can also do in one pass without storing whole prefix array? We need to know P[i] values to check if they are target, and also we need to know count of later indices with P=2target. So we can first compute prefix sums and store them (or at least we need to store for each position whether it is 2target). Since n up to 5e5, storing prefix sums as 64-bit ints is fine. So we can store prefix array.
Alternatively, we can avoid storing entire prefix by scanning from left to right and counting number of times we've seen prefix = target, and then when we encounter prefix = 2target at position j (j < n), we can add the count of target seen so far. Because if we have prefix = target at i (i < j) and then later prefix = 2target at j, that forms a valid pair (i, j). But careful: i must be before j, and j < n. So we can iterate through indices 1 to n-1 (since j must be < n, we consider up to n-1). For each index k (1-indexed), we have current prefix sum P[k]. If P[k] == 2target and k < n, then the number of i < k such that P[i] == target are valid. But also we need to ensure i < k, and i can be any index before k. But we also need i < j, which holds because i < k. So we can maintain a count of how many times we've seen prefix equal to target (at positions < current). Then when we see a prefix equal to 2target at position k (with k < n), we add that count to answer. However, we must ensure that i is at least 1 and i < j < n. Also i cannot be equal to j, but if P[i]==target and also equals 2target? That can happen only if target=0. In that case, the same index could have both prefix 0. But if we use this method, when we encounter prefix = 2target at index k, we add count of target seen so far. If at the same index k, P[k]==target also (i.e., 0), then the count includes this index if we have already incremented count for target before processing this index? Typically we process indices in order, and we need to decide order of updates: when we reach index k, we first check if P[k] == 2target and k < n, then add current count of target (which counts indices <=? Actually count includes indices i < k that have been seen). But if P[k] itself is also target, we haven't yet added it to the target count because we are processing index k now. So it's not included. So we avoid counting i = j. But we must also ensure i < k. So correct: For each index k from 1 to n-1: (we can do 1-indexed loop). We'll maintain a variable count_target = 0. For each k, we first check if P[k] == 2target and k < n, then answer += count_target. Then after that, if P[k] == target, we increment count_target (provided maybe k < n-1? Actually i can be up to n-2, but if k = n-1, can i be n-1? No because i < j < n, so i <= n-2. So if we increment count_target at k = n-1, that count would only be used for future j > n-1? There is no j > n-1 because we only consider j < n, and we process k up to n-1. So adding count_target at k=n-1 after checking 2target won't affect answer because no later j. But we might inadvertently count i = n-1 as a first part if we later check for j? There is no later j, so it's fine. However, we must also ensure that i < n-1 because j must be > i and < n. If we increment count_target at k=n-1, that count won't be used because we have processed all j (j up to n-1). So it's safe. But careful: For k = n (if we loop to n), we might check if P[n] == 2target? But j must be < n, so we should not consider k = n for adding to answer. So we can loop k from 1 to n-1 (since only these can be j). But we also need to consider i up to n-2, but i's are counted when we see prefix target, and we only count them when encountered before j. So if we loop only up to n-1, we will still count i at index n-1? That's okay, but i=n-1 not allowed? Actually i can be n-1? For i to be a valid first part end, we need i < j < n, so if i = n-1, then there is no j > i and < n, so it never contributes. So we can safely count it, but it will never be added to answer because after we increment count_target at k=n-1, we don't have any later j to use it. So it doesn't affect answer. So algorithm:
Compute total sum S. If S % 3 != 0: print 0.
Else target = S / 3.
Initialize prefix = 0, count_target = 0, answer = 0.
For index k from 1 to n-1:
prefix += a[k];
if (prefix == 2*target) and (k < n): // always true since k <= n-1, but we need to ensure j < n, k is < n so ok
answer += count_target;
if (prefix == target):
count_target += 1;
After loop, print answer.
But we must ensure order: For a given index k, if it satisfies both conditions (target and 2*target, i.e., target=0), we should not count it as both i and j simultaneously because i must be < j. So we first treat it as potential j and add count_target (which counts earlier i), then treat it as i (increment count_target). That ensures i < j.
But we also need to ensure that j < n, i.e., k < n. Since we loop only to n-1, it's fine. And i can be any index < n-1? Actually i can be up to n-2, but if i = n-1 (k=n-1) we increment count_target after checking j, but no later j, so it doesn't matter. So algorithm is correct.
Check example2: n=4, a:0,1,-1,0. S=0. target=0.
Initialize prefix=0, count_target=0, ans=0.
k=1: prefix=0. Since k<4 and prefix==2target (0) true, ans+=count_target (0) => ans=0. Then if prefix==target (0) true, count_target=1.
k=2: prefix=0+1=1. Not 2target (0). Check j? prefix!=0, skip. Then if prefix==target? 1==0 false.
k=3: prefix=1+(-1)=0. k<4, prefix==0 -> ans+=count_target (1) => ans=1. Then prefix==target -> count_target=2.
k=4: we loop only up to n-1=3, so stop. Answer=1. Correct.
Edge case: target=0 and array all zeros. Example: n=3, a=[0,0,0]. S=0. All prefixes: P1=0, P2=0, P3=0. Valid splits? i=1, j=2? Let's see: first part 1..1=0, second 2..2=0, third 3..3=0 => valid. i=1? Actually i can be 1, j=2. Also i=1, j=3? But j must be < n=3, so j=2 only. Also i=2, j=? j>i and <3 => no j. So only 1 way. Our algorithm: loop k=1..2. count_target=0, ans=0.
k=1: prefix=0, (k<3) prefix==0? -> ans+=0 -> ans=0. Then prefix==0 -> count_target=1.
k=2: prefix=0, (k<3) prefix==0 -> ans+=count_target=1 -> ans=1. Then prefix==0 -> count_target=2. End ans=1. Good.
But what about n=4, all zeros: a=[0,0,0,0]. total=0. Possible splits: i and j with 1 ≤ i < j < 4. So j <4 => j can be 2 or 3. For i=1: j=2 or 3 gives two splits. For i=2: j=3 gives one split. So total 3 splits. Let's test algorithm: n=4, loop k=1..3.
k=1: prefix=0, ans+=count_target(0)=0, count_target=1.
k=2: prefix=0, ans+=count_target(1)=1, count_target=2.
k=3: prefix=0, ans+=count_target(2)=2, count_target=3.
ans=1+2=3. Good.
Now consider scenario where target != 0 but there are indices where prefix equals both target and 2target? That can't happen unless target = 0 because target and 2target are distinct unless target=0. So no conflict.
Thus the simple O(n) algorithm with O(1) extra space works.
One more nuance: For very large sums, using 64-bit integer is necessary because each a[i] up to 1e9, n up to 5e5, total up to 5e14, fits in 64-bit signed (max ~9e18). So use long long.
Now we need to produce final answer: print answer as 64-bit integer.
Implementation details: Input reading using scanf or cin with sync. Use int for n, long long for sum and prefix.
Edge case: if total not divisible by 3, answer=0.
Now we can write code accordingly.
But note: The problem statement originally gave index constraints (2 ≤ i ≤ j ≤ n - 1) and sum definitions. However, our derived algorithm matches the standard. But we should check if they intend i as start of second part and j as start of third part. Our algorithm counts ways where i (end of first) and j (end of second) satisfy 1 ≤ i < j < n. That is equivalent to i' = i-1, j' = j-1? Actually if i (our) is end of first, then start of second is i+1, start of third is j+1. In their notation, i' = i+1? Let's map: They defined i as start of second, j as start of third. Then first part sum_{1}^{i-1}, second sum_{i}^{j-1}, third sum_{j}^{n}. So if we set i' = i-1, j' = j-1, then first part sum_{1}^{i'}, second sum_{i'+1}^{j'}, third sum_{j'+1}^{n}. That's exactly standard with i' and j' as ends of first two parts. The constraints they gave: 2 ≤ i ≤ j ≤ n-1 translates to 1 ≤ i' ≤ j' ≤ n-2. But standard constraint is 1 ≤ i' < j' ≤ n-2? Actually in standard, we need i' < j' and j' ≤ n-2? Because j' must be ≤ n-2? Wait standard: i' end of first part, j' end of second part. Conditions: i' >= 1, i' < j', j' ≤ n-1? Actually third part is j'+1..n. For third part to be non-empty, we need j' ≤ n-1, but also j' < n? Actually j' ≤ n-1 ensures j'+1 ≤ n. So j' can be up to n-1. But if j' = n-1, then third part is n..n, one element. So j' ≤ n-1. Meanwhile i' can be up to n-2 because i' < j' and j' ≤ n-1, so i' ≤ n-2. So standard bounds: 1 ≤ i' ≤ n-2, i' < j', j' ≤ n-1. So i' from 1 to n-2, j' from i'+1 to n-1. That translates to i = i'+1, j = j'+1. Then i ranges from 2 to n-1? Because i = i'+1, i' max n-2 => i max n-1. And i' min 1 => i min 2. So i ∈ [2, n-1]. j = j'+1, j' max n-1 => j max n, but j' also must be ≥ i'+1 => j ≥ i+? Let's compute: i = i'+1, j = j'+1, with i' < j' => i' + 1 ≤ j' => i ≤ j'? Actually i' + 1 ≤ j' => (i'+1) ≤ j' => i ≤ j'? But j = j'+1, so j' = j-1, so condition i' < j' => i' + 1 ≤ j' => (i'+1) ≤ (j-1) => i ≤ j-1 => i+1 ≤ j. So j ≥ i+1. Also j' ≤ n-1 => j-1 ≤ n-1 => j ≤ n. So j ∈ [i+1, n]. However their constraint says j ≤ n-1, not j ≤ n. So there's discrepancy: they say j ≤ n-1, which would exclude j = n. But j = n corresponds to j' = n-1, which is allowed in standard (third part is single element at index n). So why would they exclude that? Let's check example1: In standard, we had split i'=2, j'=4? Actually example1 had n=5, two splits: (i'=2, j'=3) and (i'=2, j'=4). In their notation, for second split (i'=2, j'=4), then i = i'+1 = 3, j = j'+1 = 5. That yields j = 5, which is n. But they say j ≤ n-1 = 4, so would exclude that split. But example output is 2, so they must be counting that split. Therefore their constraint must be j ≤ n, not j ≤ n-1. But they wrote n-1. Could be a transcription error. Let's verify the original Codeforces problem statement: It says: "You need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n), that ..." Actually I recall the problem 466C says: "Find the number of pairs i, j (2 ≤ i ≤ j ≤ n) such that ..." Wait let's check memory: In Codeforces 466C, they define i and j as the boundaries between parts: the array is split into [1..i-1], [i..j-1], [j..n]. They want 1 ≤ i ≤ j ≤ n? Actually reading: "More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n), that ..." or maybe "1 ≤ i < j ≤ n"? I'm not sure. I'll search my memory: I think the problem statement is: "Count the number of ways to split array into three parts with equal sum. More formally, find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n), that sum_{1}^{i-1} = sum_{i}^{j-1} = sum_{j}^{n}." Actually many solutions mention i and j such that 1 ≤ i < j ≤ n. Hmm.
Let's check the example1: For split with third part being single element at index5, j would be 5, which is n. So j must be allowed to be n. So indeed j can be n. In their constraint they wrote (2 ≤ i ≤ j ≤ n - 1) which is likely a mistake; it should be (2 ≤ i ≤ j ≤ n). Or (1 ≤ i < j ≤ n)? In any case, we'll adopt the standard approach that counts i and j as end indices: 1 ≤ i < j < n. That's simpler and matches the known problem. Also examples match.
Thus we will proceed with the algorithm based on prefix sums and counting target and 2*target as described.
Now we should implement.
We'll write in C++:
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> a(n);
long long total = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
total += a[i];
}
if (total % 3 != 0) {
cout << 0 << endl;
return 0;
}
long long target = total / 3;
long long prefix = 0;
long long count_target = 0;
long long ans = 0;
// iterate over positions 0..n-2 (1-indexed 1..n-1)
for (int i = 0; i < n - 1; ++i) {
prefix += a[i];
if (prefix == 2 * target) {
ans += count_target;
}
if (prefix == target) {
++count_target;
}
}
cout << ans << endl;
return 0;
}
But careful: In loop we go to i < n-1, i.e., up to index n-2 (0-indexed) which corresponds to 1-indexed position n-1. But we need to consider j up to n-2? Wait j must be < n (1-indexed). In 0-indexed, indices: array a[0..n-1]. Let prefix up to index i (0-indexed) corresponds to sum of first i+1 elements. For a split (i_end, j_end) with 1-indexed i_end, j_end, we have i_end in [1, n-2], j_end in [i_end+1, n-1]. In 0-indexed, i_end corresponds to index i_end-1, j_end corresponds to index j_end-1. So the condition prefix[i_end-1] = target, prefix[j_end-1] = 2target. So the indices for prefix that correspond to possible i_end are from 0 to n-3 (since i_end <= n-2 => i_end-1 <= n-3). The indices for j_end are from 1 to n-2 (since j_end <= n-1 => j_end-1 <= n-2). So in our loop we iterate over positions i = 0 to n-2 inclusive? Actually we need to process all positions that could be either i_end or j_end. We'll use the counting method: when we encounter a prefix equal to 2target at position k (0-indexed), we add the number of earlier positions where prefix equals target. For this to count valid pairs, k must correspond to a possible j_end, i.e., k ≤ n-2? Because j_end-1 ≤ n-2 (since j_end ≤ n-1). So k can be up to n-2. Also i_end positions must be before k and at most n-3? But if we count target at position k where k could be up to n-2, and later we might count 2target at later positions, but we should ensure that the i_end candidate is not the last element? Actually if we have prefix equal to target at position n-2 (which corresponds to i_end = n-1? Wait position n-2 (0-indexed) is prefix sum of first n-1 elements. That would be i_end = n-1. But i_end must be ≤ n-2 for valid split because we need third part non-empty. However, if we count such a target at position n-2 and then later a 2target at some position > n-2? But there is no position > n-2 because we only go up to n-2? Actually in our loop we go i from 0 to n-2, so the last index is n-2. If at position n-2 we have prefix = target, it would be counted as a potential i_end. But could it be used? For a valid pair, we need a j_end after i_end, i.e., j_end-1 > i_end-1. That would require a later index with prefix=2target. Since we are at the last index (n-2), there is no later index within the loop, so it won't be used. So counting it is harmless. However, we must also consider that at position n-2, prefix could equal 2target and then we add count_target, which includes any target positions before, including potentially target at n-2? But we add before incrementing, so it doesn't include itself. So no issue. Also what about position n-1? We don't iterate to n-1 because j_end cannot be n? Actually j_end can be n-1? Wait j_end max is n-1 (1-indexed) so j_end-1 max is n-2. So we only need positions up to n-2. So loop to n-2 (0-indexed) is correct. But careful: n-2 is inclusive? If n=2, n-2 =0, loop runs i=0? But then we also need to consider that total sum divisible by 3? For n=2, there is no way because we need three parts, but if total divisible by 3, still impossible because need at least three elements? Actually n must be at least 3 to have three non-empty parts. But algorithm would still produce 0 because no j possible. For n=2, loop runs i=0 (since n-1? Let's compute: for n=2, we set loop for i=0 to n-2 inclusive? n-2 =0, so loop runs once. But we also check if total %3==0? Suppose a=[1,2] total=3, target=1. Then prefix at i=0 =1, which is target, we increment count_target=1. No 2*target encountered because prefix=1 only. At end ans=0. So output 0. That's fine. But also we should consider that for n=2, there is no valid split; indeed answer 0.
But for n=3, loop i from 0 to n-2=1. So indices 0 and 1. For a valid split, i_end=1, j_end=2? Actually need i_end < j_end < n, with n=3 => i_end can be 1, j_end can be 2? But j_end must be < n, so j_end <=2? Actually j_end < 3, so j_end max 2. And i_end < j_end, so i_end=1, j_end=2. So we need prefix[0]? i_end=1 => prefix index 0 must equal target. j_end=2 => prefix index 1 must equal 2target. In loop, i=0: prefix = a0; process? At i=0, we check if prefix ==2target? possibly, then add count_target (which is 0). Then if prefix==target, increment count_target. Then i=1: prefix += a1; check if prefix ==2*target? if yes, add count_target (which includes possibly the earlier target). So algorithm works.
Thus loop condition: for (int i = 0; i < n-1; ++i) // up to n-2 inclusive? Actually if we use i < n-1, then i max = n-2. That's correct.
But we must also ensure we don't process the last element (index n-1) because that prefix corresponds to total sum, which is 3target, not 2target (unless target=0). But if target=0, total=0, prefix at n-1 = 0 which equals 2target (0). Could that be considered a valid j? j_end cannot be n because third part would be empty. j_end must be < n, so j_end max = n-1, which corresponds to prefix index n-2? Wait j_end is end index of second part, so third part starts at j_end+1. For third part to be non-empty, we need j_end+1 ≤ n, i.e., j_end ≤ n-1. So j_end can be n-1. That means prefix index j_end-1 = n-2. So prefix at index n-1 (total) should not be used as j_end. So we must exclude index n-1 from being considered as j. In our loop we stop at i = n-2, so we never process index n-1. Good. However, what about case where target=0 and prefix at index n-1 = 0? That would be 2target? But we won't process it, so it's not counted as j. That's correct because j_end = n would be invalid. So fine.
Thus algorithm stands.
Now we must be careful about integer overflow when multiplying target by 2: target could be up to total/3 up to 5e14, so 2*target up to 1e15, still within 64-bit.
One more nuance: The count_target and answer may be large: up to O(n^2) in worst case when all zeros, n=5e5 => answer ~ (n-1 choose 2) ~ 1.25e11, fits in 64-bit (2^63-1 ≈ 9e18). So use long long for answer.
Thus solution is ready.
We'll produce final answer with explanation and code.
But the user asked: "You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways ... Input ... Output ... Examples". Possibly they want a solution, not just code. I'll respond with a comprehensive solution description and provide C++ code.
Thus final answer. We need to count the number of ways to split the array into three contiguous parts with equal sums.
Formally, we need pairs of indices (i, j) with 1 ≤ i < j < n (if we let i and j be the ends of the first two parts) such that
If the total sum (S) is not divisible by 3, the answer is 0. Otherwise, let (target = S / 3).
The conditions are equivalent to:
The prefix sum at position (i) equals (target).
The prefix sum at position (j) equals (2 \cdot target).
Thus we need to count pairs (i < j < n) satisfying these two equalities.
Algorithm
Compute the total sum (S). If (S % 3 \neq 0), output 0 and exit.
Let (target = S / 3).
Scan the array from left to right, maintaining the current prefix sum.
Keep a counter cnt_target of how many times we have seen a prefix sum equal to (target) so far.
For each position (k) (0‑based, from 0 to (n-2) inclusive, because (j) must be (< n)):
If the current prefix sum equals (2 \cdot target), add cnt_target to the answer (these are valid first parts ending before this (k)).
Then, if the prefix sum equals (target), increment cnt_target (this position may serve as a future first part).
Output the answer.
Time complexity: (O(n)), memory: (O(1)).
C++ Implementation
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
long long total = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
total += a[i];
}
if (total % 3 != 0) {
cout << "0\n";
return 0;
}
long long target = total / 3;
long long prefix = 0;
long long cnt_target = 0;
long long ans = 0;
// Process positions 0 .. n-2 (correspond to indices 1 .. n-1 in 1‑based)
for (int i = 0; i < n - 1; ++i) {
prefix += a[i];
if (prefix == 2 * target) {
ans += cnt_target;
}
if (prefix == target) {
++cnt_target;
}
}
cout << ans << '\n';
return 0;
}