Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Mickey Mouse Constructive

Let $a$ have $x$ copies of $1$ and $y$ copies of $-1$. What is the sum of the entire array? If you partition $a$ into $m$ nonempty consecutive parts with equal part sums, what must $m$ divide?

Answer to Hint 1: The total sum is $S = x - y$. If all parts have the same sum, that common sum is $S/m$, so $m$ must be a divisor of $S$ (in the integer sense when $S \neq 0$). If $S = 0$, the only possible equal positive part sum is $0$, which forces a different discussion.

So any valid partition into $m$ equal-sum parts requires $m \mid S$ when $S \neq 0$.

Answer to Hint 2: For a fixed array $a$, $f(a)$ counts the number of ways to choose cut positions so that every block sum equals $S/m$ for some $m \ge 1$. Equivalently, you choose $m \mid S$ and then count ways to split into $m$ parts each summing to $S/m$.

What familiar arithmetic function lower-bounds how many distinct $m$ can occur for a worst-case array?

Answer to Hint 3: You are minimizing, over arrangements of $x$ pluses and $y$ minuses, the number of valid partitions (not the number of parts). The minimum achievable value turns out to be the number of positive divisors of $|x-y|$ when $x \neq y$, and $1$ when $x = y$.

Try to connect “how many ways to cut into equal-sum blocks” to divisors of the total sum for an optimal arrangement.

Answer to Hint 4: One extremal arrangement is all $1$s first, then all $-1$s (or the reverse, depending on $x$ and $y$). For that shape, prefix sums behave monotonically within each block, which minimizes the number of admissible partitions to $\tau(|x-y|)$ when $x \neq y$.

Here $\tau(d)$ denotes the number of positive divisors of $d$.

Answer to Hint 5: Implementation: output $\tau(|x-y|)$ modulo $676,767,677$ by iterating $d = 1, \ldots, |x-y|$ and counting when $(x-y) \bmod d = 0$. For the example array, print all $1$s then all $-1$s if $x \ge y$, else all $-1$s then all $1$s (or the symmetric construction your proof uses).