The video editorial for Codeforces Round 1107 (Div. 3) is available here.
Do check out the CF Video Editorial Finder browser extension — it adds links to my video editorials directly from the contest/problems page.
Starting from $$$x$$$, what values can you reach?
Each operation replaces $$$x$$$ with $$$x / z$$$ where $$$z$$$ divides $$$x$$$.
So in a single step you can jump directly to any divisor of the current $$$x$$$.
Over multiple steps you can still only ever reach divisors of the original $$$x$$$.
To reach any divisor $$$d$$$ of $$$x$$$, just pick $$$z = x / d$$$ in one step — done.
Answer: YES if and only if $$$y$$$ divides $$$x$$$.
My submission — 380833126
What does $$$n \times 11$$$ look like for a single-digit $$$n$$$?
$$$1 \times 11 = 11$$$, $$$\; 2 \times 11 = 22$$$, $$$\; 9 \times 11 = 99$$$.
It's always the digit repeated — i.e. $$$n$$$ concatenated with itself.
For $$$n$$$ with $$$d$$$ digits, we want $$$z$$$ such that $$$n \times z = \overline{nn}$$$ (n concatenated with n).
Note that $$$\overline{nn} = n \times 10^d + n = n \times (10^d + 1)$$$.
So $$$z = 10^d + 1$$$, where $$$d$$$ = number of digits in $$$n$$$.
For $$$n = 12$$$: $$$d = 2$$$, $$$z = 101$$$, $$$12 \times 101 = 1212$$$.
For $$$n = 123$$$: $$$d = 3$$$, $$$z = 1001$$$, $$$123 \times 1001 = 123123$$$.
My submission — 380833909
The answer is almost always 1. Only a very specific type of string gives 2.
If the string starts and ends with the same character (e.g., both '0'), pick any "0[1...]0" substring — it is a palindrome — and delete one '1' from it at a time until the '1'-block disappears. Repeat for each '1'-block. The remaining '0's collapse to a single '0'. Answer 1.
Example:
01111101111011110
→ 001111011110
→ 00011110
→ 0000
→ 0
What if the first and last characters differ? E.g., starts '0', ends '1'.
Find any "1[...]1" substring (two '1's with characters between them) — "101" is a palindrome, for example. Delete the rightmost '1' from it, making the string end with '0'. Now first and last are both '0' — back to Hint 2. Answer still 1.
E.g., "0101": choose "101" (palindrome) → delete the last '1' → "010" → starts and ends with '0'. Answer 1.
The only stuck case: the string is sorted into exactly two blocks — all of one character followed by all of another (like "0000111111").
There is no '0' after the '1'-block, so no "0[1...]0" palindrome can be formed — the '1'-block can never be removed. At best each block compresses to one character, leaving "01". Answer 2.
My submission — 380835464
Any operation on $$$[l, r]$$$ decomposes into non-overlapping length-2 blocks plus at most one length-1 at the end:
$$$[l, r] = [l, l+1] + [l+2, l+3] + \cdots$$$ (and $$$[r, r]$$$ if the length is odd).
So, we should just use following 2 operations:
Length-2: $$$[i, i+1]$$$ — adds $$$+1$$$ to $$$a_i$$$, $$$-1$$$ to $$$a_{i+1}$$$.
Length-1: $$$[i, i]$$$ — adds $$$+1$$$ to $$$a_i$$$.
Greedy: fix positions left to right.
For $$$i = 0$$$ to $$$n-2$$$: apply $$$[i, i+1]$$$ repeatedly until $$$a_i = b_i$$$ (increases $$$a_i$$$, borrows from $$$a_{i+1}$$$).
For $$$i = n-1$$$: apply $$$[n-1, n-1]$$$ repeatedly until $$$a_{n-1} = b_{n-1}$$$.
Answer is NO if at any step $$$i$$$, after $$$a_0 \ldots a_{i-1}$$$ have been made equal to $$$b_0 \ldots b_{i-1}$$$, the current value of $$$a_i \gt b_i$$$.
My submission — 380836642
Let $$$P(u)$$$ = product of node values on the path from the root to $$$u$$$ (inclusive).
Then the product of values along any path $$$u \to v$$$ is:
$$$P(\text{lca})^2$$$ is always a perfect square, so dividing by it does not affect whether the result is a perfect square.
Thus $$$\text{prod}(u, \ldots, v)$$$ is a perfect square iff $$$P(u) \cdot P(v) \cdot a[\text{lca}(u,v)]$$$ is a perfect square.
Expand $$$\text{prod}(u,v) \cdot \text{prod}(v,w) \cdot \text{prod}(w,u)$$$ using the formula from Hint 1:
All $$$P^2$$$ terms are perfect squares and can be ignored. The triple is good iff $$$a[\text{lca}(u,v)] \cdot a[\text{lca}(v,w)] \cdot a[\text{lca}(w,u)]$$$ is a perfect square.
The three pairwise LCAs $$$\text{lca}(u,v)$$$, $$$\text{lca}(u,w)$$$, $$$\text{lca}(v,w)$$$ cannot all be distinct — at most 2 distinct values appear among them (hint: Auxiliary Tree).
So the product $$$a[\text{lca}(u,v)] \cdot a[\text{lca}(u,w)] \cdot a[\text{lca}(v,w)]$$$ always equals some $$$a[m]^2 \cdot a[m']$$$ (one node repeated, one appearing once) or $$$a[m]^3$$$ (all three equal). Either way, it is a perfect square iff the non-repeated node $$$m$$$ has a perfect square value.
The meeting point of a triple $$$(u, v, w)$$$ is the unique node that lies on all three pairwise paths. It is the non-repeated LCA from Hint 5.
For each node $$$m$$$ with $$$a[m]$$$ a perfect square, count triples $$$(u, v, w)$$$ where $$$m$$$ is the meeting point.
$$$m$$$ is the meeting point iff $$$u, v, w$$$ each lie in a different branch of $$$m$$$. Branches = $$$m$$$ itself (size $$$1$$$), the rest-of-tree above $$$m$$$ (size $$$n - \text{sub}[m]$$$), and each child subtree (size $$$\text{sub}[c]$$$ per child $$$c$$$).
Count $$$= \displaystyle\sum_{a \lt b \lt c} S_a \cdot S_b \cdot S_c$$$
Computed efficiently: for each branch $$$b$$$, add $$$S_b \cdot \bigl(\sum_{i \lt b} S_i\bigr) \cdot \bigl(\sum_{i \gt b} S_i\bigr)$$$ using prefix sums.
Total: $$$O(n)$$$ in a single DFS.
My submission — 380841093
Count $$$T$$$ = total inversions in the string — the number of pairs $$$(i, j)$$$ with $$$i \lt j$$$, $$$s_i = $$$ '1', $$$s_j = $$$ '0'.
If $$$T$$$ is odd, Alice wins immediately.
If $$$T$$$ is even and there exists an index $$$i$$$ that is part of an odd number of inversion pairs, Alice can delete all characters except $$$i$$$.
The deleted subsequence has $$$T - (\text{inversions involving } i)$$$ inversions. Since $$$T$$$ is even and inversions involving $$$i$$$ is odd, the deleted part has odd inversions — a valid move. The remaining single character has $$$0$$$ inversions, so Bob cannot move. Alice wins.
When every index has an even number of inversion pairs: start with chosen $$$= \emptyset$$$ (0 inversions) and remaining $$$=$$$ full string ($$$T$$$ inversions) — both even.
Each time you move one character into the chosen set, it has $$$k_C$$$ inversion pairs with other chosen characters and $$$k_R$$$ pairs with remaining characters, with $$$k_C + k_R$$$ even. So $$$k_C \equiv k_R$$$: both sides flip parity together or neither does.
So chosen and remaining always have the same parity of inversions. If Alice picks a valid subsequence (odd inversions), the remaining string also has odd inversions.
If neither Hint 2 nor Hint 3 applies, then whatever Alice picks with odd inversions, Bob sees a remainder with odd inversions and can take everything — Bob wins.
My submission — 380843436
$$$a_0$$$ cannot be changed — it has no left neighbor. So $$$a_0$$$ is always part of the answer range. Goal: bring all other elements as close to $$$a_0$$$ as possible.
Repeatedly subtract $$$a_0$$$ from $$$a_1$$$ until $$$a_1 \in [0, a_0)$$$, i.e. set $$$a_1 = a_1 \bmod a_0$$$. Then repeatedly subtract $$$a_1$$$ from $$$a_2$$$ until $$$a_2 \in [0, a_1)$$$, and so on — making the array non-increasing: $$$a_0 \geq a_1 \geq a_2 \geq \cdots$$$
Now add $$$a_0$$$ to all elements. Intermediate negative values are allowed. Subtract consecutive pairs right to left:
$$$[a_0, a_1, a_2, \ldots, a_{n-1}]$$$
$$$\to [a_0, a_1, a_2, \ldots, a_{n-2},\ a_{n-1} - a_{n-2}]$$$
$$$\to [a_0, a_1, a_2, \ldots, a_{n-3},\ a_{n-2} - a_{n-3},\ a_{n-1} - a_{n-2}]$$$
$$$\to \cdots$$$
$$$\to [a_0, a_1,\ a_2 - a_1,\ a_3 - a_2,\ \ldots,\ a_{n-1} - a_{n-2}]$$$
Now add left to right starting from $$$[a_0,\ a_1,\ a_2-a_1,\ a_3-a_2,\ \ldots,\ a_{n-1}-a_{n-2}]$$$:
$$$\to [a_0,\ a_1+a_0,\ a_2-a_1,\ a_3-a_2,\ \ldots]$$$
$$$\to [a_0,\ a_1+a_0,\ a_2+a_0,\ a_3-a_2,\ \ldots]$$$
$$$\to \cdots$$$
$$$\to [a_0,\ a_1+a_0,\ a_2+a_0,\ \ldots,\ a_{n-1}+a_0]$$$
Similarly, starting from $$$[a_0,\ a_1,\ a_2-a_1,\ a_3-a_2,\ \ldots,\ a_{n-1}-a_{n-2}]$$$, subtract $$$a_0$$$ from $$$a_1$$$ instead:
$$$\to [a_0,\ a_1-a_0,\ a_2-a_1,\ a_3-a_2,\ \ldots,\ a_{n-1}-a_{n-2}]$$$
Then add left to right:
$$$\to [a_0,\ a_1-a_0,\ a_2-a_0,\ \ldots,\ a_{n-1}-a_0]$$$
Since the array is non-increasing after Hint 2, all $$$a_i \in [0, a_1]$$$ for $$$i \geq 2$$$.
So in $$$[a_0,\ a_1+a_0,\ a_2+a_0,\ \ldots]$$$, every element satisfies $$$a_i+a_0 \in [a_0,\ a_1+a_0]$$$. Range $$$= a_1$$$.
Starting from $$$[a_0,\ a_1,\ a_2,\ \ldots,\ a_{n-1}]$$$, make $$$a_1 \to a_1 - a_0$$$ (a negative value). The new step size available to $$$a_2, \ldots, a_{n-1}$$$ is $$$a_0 - a_1$$$.
Since the array is non-increasing, all $$$a_i \in [0,\ a_1]$$$ for $$$i \geq 2$$$. By repeatedly taking $$$\bmod\ (a_0 - a_1)$$$, all $$$a_2, \ldots, a_{n-1}$$$ can be reduced to lie in $$$[0,\ a_0 - a_1]$$$.
Now add $$$a_1$$$ to each of $$$a_2, \ldots, a_{n-1}$$$: they move from $$$[0,\ a_0-a_1]$$$ to $$$[a_1,\ a_0]$$$. So $$$\min = a_1$$$, $$$\max = a_0$$$, range $$$= a_0 - a_1$$$.
The minimum achievable range is $$$\min(a_1 \bmod a_0,\ a_0 - a_1 \bmod a_0)$$$.
If $$$a_0$$$ divides $$$a_1$$$, the above arguments fail — the best we can do is make $$$a_1 = a_0$$$, contributing $$$0$$$ to the range. We then repeat the same argument for $$$(a_0, a_2)$$$, then $$$(a_0, a_3)$$$, and so on, skipping all multiples of $$$a_0$$$. The answer is determined by the first $$$a_r$$$ that is not a multiple of $$$a_0$$$. If no such $$$a_r$$$ exists, the answer is $$$0$$$.
For all subarrays: fix left endpoint $$$l$$$. Let $$$r$$$ be the first index $$$ \gt l$$$ where $$$a_r$$$ is not a multiple of $$$a_l$$$.
All subarrays $$$[l, l], [l, l{+}1], \ldots, [l, r{-}1]$$$ have answer $$$0$$$.
All subarrays $$$[l, r], [l, r{+}1], \ldots, [l, n{-}1]$$$ share the same first non-multiple $$$a_r$$$, so each contributes $$$\text{cost}(a_l, a_r)$$$.
Total contribution of $$$l$$$: $$$\text{cost}(a_l, a_r) \times (n - r)$$$.
To find $$$r$$$ for each $$$l$$$ efficiently: build a segment tree on $$$a$$$ supporting range GCD queries.
Binary search on the segment tree: find the first $$$r$$$ where $$$\gcd(a_l, \ldots, a_r)$$$ is no longer divisible by $$$a_l$$$. Use max_right from the AtCoder library for $$$O(\log n)$$$ per query.
Total: $$$O(n \log n)$$$.
My submission — 380794968









F is more clearly than official editorials, thank you.
Yes, this problem tricky to prove and must easier to guess.
big thanks for F, very clear.