1789A - Serval и массив Mocha
Idea & Preparation: Bazoka13
Considering an array $$$a$$$ of $$$n$$$ ($$$n\geq 2$$$) positive integers, the following inequality holds for $$$2\leq i\leq n$$$:
Therefore, when the prefix $$$[a_1,a_2]$$$ of $$$a$$$ is good, we can show that all the prefixes of $$$a$$$ whose length is no less than $$$2$$$ are good, then $$$a$$$ is beautiful. It is obvious that $$$[a_1, a_2]$$$ is good when $$$a$$$ is beautiful. So we get the conclusion that $$$a$$$ is beautiful if and only if the prefix $$$[a_1, a_2]$$$ is good.
We can check if there exist $$$a_i, a_j$$$ ($$$i\neq j$$$) such that $$$\gcd(a_i, a_j)\leq 2$$$. If so, we can move $$$a_i,a_j$$$ to the front of $$$a$$$ to make it beautiful, then the answer is Yes
. If not, the answer is No
.
Time complexity: $$$O(n^2)$$$.
1789B - Serval и Инверсионная магия
Idea & Preparation: Bazoka13
If $$$s$$$ is palindromic initially, we can operate on the interval $$$[1,n]$$$, the answer is Yes
.
Let's consider the other case. In a palindrome $$$s$$$, for each $$$i$$$ in $$$[1,\lfloor n/2\rfloor]$$$, $$$s_{i}=s_{n-i+1}$$$ must hold. For those $$$i$$$, we may check whether $$$s_{i}=s_{n-i+1}$$$ is true in the initial string. For all the illegal positions $$$i$$$, the operation must contain either $$$i$$$ or $$$n+1-i$$$, but not both. For the legal positions, the operation must contain neither of $$$i$$$ nor $$$n+1-i$$$, or both of them.
If the illegal positions is continuous (which means that they are $$$l,l+1,\dots,r-1,r$$$ for some $$$l$$$ and $$$r$$$), we may operate on the interval $$$[l,r]$$$ (or $$$[n+1-r,n+1-l]$$$), making the string palindromic. The answer is Yes
.
Otherwise, there must be some legal positions that lie between the illegal ones. Suppose the illegal positions range between $$$[l,r]$$$ (but not continuous), and the operation is $$$[o_{1},o_{2}]$$$. Without loss of generality, let the operation lies in the left part of the string. Then $$$o_{1}\le l,r\le o_{2}<n+1-r$$$ must hold to correct all the illegal positions. This interval covers all the legal positions that lie between the illegal ones but does not cover their symmetrical positions. Thus, such kind of operation will produce new illegal positions. In other words, there are no valid operations in this situation. The answer is No
.
Time complexity: $$$O(n)$$$.
1789C - Serval и массивы Toxel
Idea & Preparation: Toxel
Consider the contribution of each value. We only need to count the number of concatenated arrays each value appears in, and sum all those counts up. The answer to this problem only depends on the number of appearances of this value. Notice that the appearance of each value forms some intervals. Each interval starts when it modifies another element (or in the initial array), and ends when it is modified (or in the $$$m$$$-th array). As there are no duplicate elements, the intervals do not intersect, so we can simply sum their lengths up.
Let's use an array $$$\text{appear}$$$ to track the appearance of each value. We first set the appearance of the initial elements to $$$0$$$, and other elements to $$$-1$$$, which means the value does not appear. Then, in the $$$i$$$-th modification, suppose we modified some elements from $$$x$$$ to $$$y$$$, then we should add $$$i-\text{appear}_{x}$$$ to $$$\text{count}_{x}$$$, and set $$$\text{appear}_{x}$$$ to $$$-1$$$. We should also set $$$\text{appear}_{y}$$$ to $$$i$$$. After all operations, for all $$$x$$$, add $$$m-\text{appear}_{x}$$$ to $$$\text{count}_{x}$$$ if $$$\text{appear}_{x}$$$ is not $$$-1$$$.
Value $$$x$$$ appears in $$$\frac{m(m+1)}{2}-\frac{(m-\text{count}_{x})(m-\text{count}_{x}+1)}{2}$$$ concatenated arrays.
Time complexity: $$$O(n+m)$$$.
1789D - Serval и сдвиг-сдвиг-сдвиг
Idea & Preparation: Toxel
First of all, it could be proven that the answer exists if and only if $$$a$$$ and $$$b$$$ are both zero or $$$a$$$ and $$$b$$$ are both non-zero.
If $$$a$$$ is zero, it remains zero after any operations. Therefore it cannot become $$$b$$$ if $$$b$$$ is non-zero. If $$$a$$$ is non-zero, logical left shift it will definitely increase its lowest bit or make it zero, thus changing it into a different number. The same applies to logical right shift. Therefore, the xor result must be non-zero and there are no possible operations if $$$b$$$ is zero.
We will show that it is always possible to change $$$a$$$ into $$$b$$$ in the other cases. We denote $$$\text{lb}(a)$$$ as the lowest bit of $$$a$$$ and $$$\text{hb}(a)$$$ as the highest bit of $$$a$$$. If $$$a$$$ and $$$b$$$ are both zero, no operations are needed. If they are both non-zero, the construction consists of four steps:
- If $$$\text{hb}(a)<\text{lb}(b)$$$, logical left shift $$$a$$$ by $$$\text{lb}(b)-\text{hb}(a)$$$ bits. Then $$$\text{hb}(a)$$$ must be equal or greater than $$$\text{lb}(b)$$$.
- For each bit $$$i$$$ of $$$\text{lb}(b)-1,\text{lb}(b)-2,\dots,1$$$, if $$$a_{i}=1$$$, we may logical right shift $$$a$$$ by $$$\text{hb}(a)-i$$$ bits to erase it. After that, we have $$$\text{lb}(a)\ge\text{lb}(b)$$$.
- If $$$\text{lb}(a)>\text{lb}(b)$$$, logical right shift $$$a$$$ by $$$\text{lb}(a)-\text{lb}(b)$$$ bits. Now it is guaranteed that $$$\text{lb}(a)=\text{lb}(b)$$$.
- For each bit $$$i$$$ of $$$\text{lb}(b)+1,\text{lb}(b)+2,\dots,n$$$, if $$$a_{i}\neq b_{i}$$$, we may logical left shift $$$a$$$ by $$$i-\text{hb}(a)$$$ bits to erase it. After that, we must have $$$a=b$$$.
Step 2 and step 4 require at most $$$n-1$$$ operations. We may also note that step 1 and step 3 never appear simultaneously. If step 1 is operated, then $$$\text{lb}(a)=\text{lb}(b)$$$ is guaranteed after step 2. Thus, we need not operate step 3 in this case. In conclusion, we may use no more than $$$n$$$ operations to change $$$a$$$ into $$$b$$$ if they are both non-zero.
Time Complexity: $$$O(n^{2})$$$ or $$$O(\frac{n^{2}}{w})$$$ by using std::bitset
.
1789E - Serval и музыкальная игра
Idea & Preparation: Serval
Consider the following two cases:
Case 1: $$$x$$$ is not a factor of $$$s_n$$$.
In this case we have $$$\left\lfloor{s_n\over x}\right\rfloor + 1 = \left\lceil{s_n\over x}\right\rceil$$$. Let $$$k = \left\lfloor{s_n\over x}\right\rfloor$$$. It can be shown that there are at most $$$2\sqrt{s_n}$$$ different values of $$$k$$$. The constraint of $$$s_i$$$ can be written in the following form:
For a certain $$$k$$$, such $$$p_i$$$ and $$$q_i$$$ do not exist if and only if $$$s_i\bmod k > \left\lfloor{s_i\over k}\right\rfloor$$$. To prove it, we show the contradiction that $$$q_i\bmod k = s_i\bmod k > \left\lfloor{s_i\over k}\right\rfloor \geq q_i$$$, and we can give a construction of $$$p_i$$$ and $$$q_i$$$ when $$$s_i\bmod k\leq \left\lfloor{s_i\over k}\right\rfloor$$$ that $$$q_i = s_i\bmod k$$$ and $$$p_i=\left\lfloor{s_i\over k}\right\rfloor - q_i$$$.
By observation, these $$$s_i$$$ are in one of the following $$$k-2$$$ intervals:
We can count the number of these $$$s_i$$$ by pre-calculating the prefix sums to calculate $$$f(x)$$$.
This case can be solved in $$$O(s_n)$$$ time, and we will show this fact:
- When $$$k\leq \sqrt{s_n}$$$, there are $$$k - 2$$$ intervals that need to be considered for a certain $$$k$$$. Since $$$\sum_{k\leq \sqrt{s_n}} k \leq s_n$$$, this part can be solved in $$$O(s_n)$$$ time.
- When $$$k>\sqrt{s_n}$$$, notice that there are at most $$$\left\lceil s_n\over k\right\rceil \leq \sqrt{s_n}$$$ intervals that need to be considered for a certain $$$k$$$. Recall that there are at most $$$\sqrt{s_n}$$$ different values of $$$k$$$ in this part, so it can be solved in $$$O(s_n)$$$ time.
Case 2: $$$x$$$ is a factor of $$$s_n$$$.
In this case we have $$$\left\lfloor{s_n\over x}\right\rfloor = \left\lceil{s_n\over x}\right\rceil$$$. Let $$$k = {s_n\over x}$$$. The constraint of $$$s_i$$$ becomes:
To calculate $$$f(x)$$$, we only need to count the number of multiples of $$$x$$$. To do this, we can first calculate $$$s'_i = \gcd(s_i, s_n)$$$ for all $$$1\leq i\leq n$$$ in $$$O(n\log s_n)$$$ time. It is obvious that $$$s_i'$$$ is a factor of $$$s_n$$$. For a certain $$$x$$$, we can enumerate all the factors of $$$s_n$$$, find out the multiples of $$$x$$$ among them, and sum up the times that they occurred in $$$s'$$$. Recall that $$$s_n$$$ has at most $$$2\sqrt{s_n}$$$ factors, so this takes $$$O(s_n)$$$ time.
This case can be solved in $$$O(n\log s_n + s_n)$$$ time in total.
Time complexity: $$$O(n\log s_n + s_n)$$$.
$$$O(s_n + \sigma(s_n))$$$ solutions can pass all the tests, where $$$\sigma(n)$$$ denotes the sum of all the factors of $$$n$$$. A well-implemented $$$O(s_n\log s_n)$$$ solutions may pass the tests, too.
Bonus: Solve this problem in $$$O(n + s_n)$$$ time.
1789F - Serval и Brain Power
Idea & Preparation: Serval
Assume that the longest powerful subsequence of the given string $$$S$$$ is $$$T$$$, which can be obtained by concatenating $$$k$$$ copies of string $$$T'$$$. Noticing that $$$|S|\leq 80$$$, we have the observation that $$$k\cdot |T'| \leq |S| \leq 80$$$, so it is impossible that both $$$k$$$ and $$$|T'|$$$ is large.
When $$$k < 5$$$, we only need to consider the $$$k = 2$$$ case and the $$$k = 3$$$ case. The $$$k = 4$$$ case is covered by $$$k = 2$$$ case, since $$$T = T'+T'+T'+T' = (T'+T') + (T'+T')$$$.
For the $$$k = 2$$$ case, we split $$$S$$$ into two parts $$$S=S_1+S_2$$$, then calculate the maximal length of $$$\operatorname{LCS}(S_1, S_2)$$$ by dynamic programming over all the possible splits. This case can be solved in $$$O(w_2\cdot|S|^3)$$$ time, where $$$w_2$$$ is a small constant.
It is similar to solve the $$$k = 3$$$ case. We split $$$S$$$ into three parts $$$S = S_1 + S_2 + S_3$$$, then calculate the maximal length of $$$\operatorname{LCS}(S_1, S_2, S_3)$$$ over all the possible splits. This case can be solved in $$$O(w_3\cdot|S|^5)$$$ time, where $$$w_3$$$ is a small constant. We will estimate $$$w_3$$$ later.
When $$$k \geq 5$$$, we have $$$|T'|\leq {|S|\over k}\leq {|S|\over 5}$$$. It can be shown that, if we split $$$S$$$ into $$$5$$$ parts, $$$T'$$$ will be the subsequence of at least one of them. We can split $$$S$$$ into equal lengths, then enumerate all the subsequences of these substrings as the possible $$$T'$$$. For a possible $$$T'$$$, we can find out corresponding $$$k$$$ by matching $$$T'$$$ and $$$S$$$ greedily. This case can be solved in $$$O(5\cdot 2^{|S|/5}|S|)$$$.
Now let us roughly estimate how small $$$w_3$$$ could be. The time that dynamic programming consumed for certain $$$S_1, S_2, S_3$$$ is $$$|S_1|\cdot|S_2|\cdot|S_3|$$$. Since $$$|S_1|+|S_2|+|S_3|=|S|$$$, we have $$$|S_1|\cdot|S_2|\cdot|S_3|\leq {1\over 27}|S|^3$$$. Recall that there are $$${|S|-1 \choose 2} \leq {1\over 2}|S|^2$$$ possible splits, then $$$w_3\leq {1\over 54}$$$ holds.
Time complexity: $$$O(w_3\cdot|S|^5 + 5\cdot 2^{|S|/5}|S|)$$$.