Division
InterviewTime limit1sMemory limit512 MB
Count ways to cut a sequence into four nonempty contiguous parts with equal sums, allowing negative values.
- Level
Medium6 of 10
- Topics
- Prefix sum, Hash map, Array, Combinatorics
- Solved
- No attempts yet
Problem
You are given a sequence of integers . You want to split the sequence into four contiguous parts. Each part must contain at least one number, and the sums of the four parts must all be equal. That is, for some (), split the sequence into .
For example, suppose the given sequence is . If you split it as below, the sums of the parts differ, so this form is not allowed.
If you split it as below, the sums of all parts are equal.
The splits below also give equal sums for all parts.
or
Write a program that reads the sequence and counts the number of possible ways to split it as above.
Input
The first line gives the length of the sequence, .
The second line gives the integers , separated by single spaces.
Output
Print the number of possible ways on the first line.
The answer can be very large, so in C and C++ you must use a variable of type long long, and in Java a variable of type long.
Constraints
- For all ,