3069.Number of Ways

Time Limit: 1s Memory Limit: 256MB

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 \le i \le j \le n-1), that 3069_1.png.

Input Format(From the terminal/stdin)

The first line contains integer n (1 \le n \le 5 \cdot 105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| \le 109) - the elements of array a.

Output Format(To the terminal/stdout)

Print a single integer - the number of ways to split the array into three parts with the same sum.

Sample Input 1

Copy
5
1 2 3 0 3
 \n
 · · · · \n

Sample Output 1

Copy
2
 \n

Sample Input 2

Copy
4
0 1 -1 0
 \n
 · ·  · \n

Sample Output 2

Copy
1
 \n

Sample Input 3

Copy
2
4 1
 \n
 · \n

Sample Output 3

Copy
0
 \n

Submit

请先 登录

© 2025 FAQs