Problem A
Only a few strings have the answer "YES".
For $$$n \ge 3$$$, the answer is "NO".
Let $$$n \ge 3$$$ and the resulting string be $$$a$$$. For there to be no palindromes of length greater than $$$1$$$, at least all of these inequalities must be true: $$$a_1 \neq a_2$$$, $$$a_2 \neq a_3$$$, and $$$a_1 \neq a_3$$$. Since our string is binary, that is not possible, so the answer is "NO".
For $$$n \le 2$$$, there are 4 strings that have the answer "YES": {$$$0$$$, $$$1$$$, $$$01$$$, and $$$1$$$}, and 2 strings that have the answer "NO": {$$$00$$$, $$$11$$$}.
Complexity: $$$O(n)$$$
Problem B
The cost of construction is a power of two.
The cost of construction is $$$2 ^ k$$$, where $$$k$$$ is the highest set bit in $$$n - 1$$$. .
Let $$$k$$$ be the highest set bit in $$$n - 1$$$. There will always be a pair of adjacent elements where one of them has the $$$k$$$-th bit set and the other one doesn't, so the cost is at least $$$2^k$$$. A simple construction that reaches it is $$$2^k - 1$$$, $$$2^k - 2$$$, $$$\ldots$$$, $$$0$$$, $$$2^k$$$, $$$2^k + 1$$$, $$$\ldots$$$, $$$n - 1$$$.
Complexity: $$$O(n)$$$
Bonus: count the number of permutations with the minimum cost.
Problem C
It is optimal to apply the third operation at most once.
It is obvious, that it is optimal to apply third operation at most once, because this operation does not decrease $$$a$$$ and always makes $$$b \le a$$$. Also, after third operation it is optimal to apply only second operations to make $$$a$$$ and $$$b$$$ equal.
So, in every optimal solution before applying third operation we apply some first operations and make $$$a$$$ equal to some value, let it be $$$a'$$$ $$$(a \le a')$$$ and apply some second operations and make $$$b$$$ equal to some value, let it be $$$b'$$$ $$$(b \le b')$$$. Now let is calculate how many operations we need to apply to make $$$a$$$ and $$$b$$$ equal. It is $$$(a' - a) + (b' - b) + ((a' | b') - b')$$$. And we must choose such $$$a'$$$ and $$$b'$$$ so that the expression takes the minimum value. Let is transform this expression. It is equal to $$$a' - a + b' - b + (a' | b') - b' = a' + (a' | b') - a - b$$$. $$$a$$$ and $$$b$$$ are constants, so we must find the minimum value of $$$a' + (a' | b')$$$.
To solve this problem we can iterate over $$$a'$$$ from $$$a$$$ to $$$b$$$ and find such $$$b'$$$ $$$(b \le b')$$$ that $$$a' + (a' | b')$$$ takes the minimum value. We can use greedy algorithm throught bits here. Let is iterate over bits from the highest. There can be 4 situations:
- Current bit of $$$a'$$$ is $$$0$$$ and $$$b$$$ is $$$1$$$. In this case we can set current bit of $$$b'$$$ to $$$1$$$ and continue algorithm.
- Current bit of $$$a'$$$ is $$$0$$$ and $$$b$$$ is $$$0$$$. In this case we can set current bit of $$$b'$$$ to $$$0$$$ and continue algorithm.
- Current bit of $$$a'$$$ is $$$1$$$ and $$$b$$$ is $$$1$$$. In this case we can set current bit of $$$b'$$$ to $$$1$$$ and continue algorithm.
- Current bit of $$$a'$$$ is $$$1$$$ and $$$b$$$ is $$$0$$$. In this case we can set current bit of $$$b'$$$ to $$$1$$$ and stop algorithm.
This algorithm could be done in $$$O(log(b))$$$ or in $$$O(1)$$$ using bit operations.
So, time complexity is $$$O(b \cdot log(b))$$$ or $$$O(b)$$$.
Bonus 1: solve the problem in $$$O(\log b)$$$ or faster.
Bonus 2: prove that is optimal to have either $$$a' = a$$$ or $$$b' = b$$$.
Problem D
Let's call a segment $$$[l, r]$$$ bad if $$$\gcd(a_l \ldots a_r) = r - l + 1$$$. There at most $$$n$$$ bad segments.
For a fixed $$$l$$$, as $$$r$$$ increases, $$$\gcd(a_l \ldots a_r)$$$ decreases.
Suppose you change $$$a_i$$$ into a big prime. How does this affect the bad segments?
Greedy
Read the hints above.
Let's find all of the bad segments. For a fixed $$$l$$$, let's find the largest $$$r$$$ that has $$$\gcd(a_l \ldots a_r) \ge r - l + 1$$$. This can be done with binary search and a sparse table / segment tree. If $$$\gcd(a_l \ldots a_r) = r - l + 1$$$, then the segment $$$[l, r]$$$ is bad.
If we change $$$a_i$$$ into a big prime, no new bad segments will appear. And all bad segments that have $$$i$$$ inside of them will disappear.
Notice that for two bad segments $$$[l_1, r_1]$$$ and $$$[l_2, r_2]$$$, if $$$l_1 < l_2$$$, then $$$r_1 < r_2$$$. In other words, they can not be nested. Let's sort the segments by $$$r$$$ and loop over $$$k$$$ — the length of the prefix. As soon as we encounter some segment with $$$r = k$$$, we add one to the current answer and then remove all segments with $$$l \le k$$$, which is simple, since our segments are not nested.
Complexity: $$$O(n \log n \log A)$$$ with a sparse table, where $$$A$$$ is the maximum value of $$$a_i$$$.
Notes:
There are many different modifications to the previous solution, some of them use two pointers (since segments are not nested) and some of them update the answer on the fly while searching for the bad segments. Using a segment tree and two pointers you can get the complexity $$$O(n (\log n + \log A))$$$.
You can also use the fact that for a prefix, there at most $$$O(\log A)$$$ different suffix $$$\gcd$$$ values. This leads to another way to find the bad segments.
Problem E
It is optimal to add edges of the form $$$(1, v)$$$.
Try to check if for a fixed $$$x$$$ the answer is at most $$$ans$$$.
For a fixed $$$x$$$ and answer $$$ans$$$, find the distance between nodes that have $$$depth_v > ans$$$.
For each node, find two children with the deepest subtrees.
Read the hints above.
Let $$$f_{ans}$$$ be the maximum distance between two nodes that have $$$depth_v > ans$$$. If for some $$$x$$$ the answer is at most $$$ans$$$, then either $$$ans \ge depth$$$ or $$$\lceil \frac{f_{ans}}{2} \rceil + x \le ans$$$, since we can add an edge $$$(1, u)$$$ where $$$u$$$ is in the middle of the path connecting the two farthest nodes with $$$depth_v > ans$$$. Since $$$f_ans$$$ decreases as $$$ans$$$ increases, we can use binary search. Also note that we can use two pointers and increase $$$ans$$$ as we increase $$$x$$$.
How to calculate $$$f_{ans}$$$? Let's find for each node its two children with the deepest subtrees. Let $$$a_v$$$ and $$$b_v$$$ be the depths of their subtrees ($$$a_v \ge b_v$$$). If there are not enough children, set this values to $$$depth_v$$$. If $$$b_v > 0$$$, do $$$f_{b_v-1} := \max(f_{b_v - 1}, a_v + b_v - 2 \cdot depth_v)$$$. After this, iterate $$$i$$$ from $$$n - 2$$$ to $$$0$$$ and do $$$f_i = \max(f_i, f_{i + 1})$$$.
Complexity: $$$O(n)$$$ or $$$O(n \log n)$$$ with binary search.