1718.md (22978B)
1 Source: https://codeforces.com/blog/entry/106049 2 3 Sorry for not very good samples, and late editorial. It was our first round, so it was wary stressful for us, But we hope you liked the problems despite this! 4 5 [1719A - Chip Game](/contest/1719/problem/A "Codeforces Round 814 (Div. 2)") 6 7 **Hint 1** 8 9 Try to look at the parity of $n, m$. 10 11 **Hint 2** 12 13 The player who wins does not depend on player's strategy. 14 15 **Hint 3** 16 17 The only thing that the winning player depends on is the parity of $n, m$. 18 19 **Tutorial** 20 21 Note that the game will end only when the chip is in the upper right corner (otherwise you can move it $1$ square to the right or up). For all moves, the chip will move $n - 1$ to the right and $m - 1$ up, which means that the total length of all moves is $n + m - 2$ (the length of the move is how much the chip moved per turn). Since the length of any move is odd, then after any move of Burenka, the sum of the lengths will be odd, and after any move of Tonya is even. So we can find out who made the last move in the game by looking at $(n + m - 2) \operatorname{mod} 2 = (n + m) \operatorname{mod} 2$. With $(n + m) \operatorname{mod} 2 = 0$, Tonya wins, otherwise Burenka. 22 23 The complexity of the solution is $O(1)$. 24 25 [1719B - Mathematical Circus](/contest/1719/problem/B "Codeforces Round 814 (Div. 2)") 26 27 **Hint 1** 28 29 Instead of $k$, we can consider the remainder of dividing $k$ by 4. 30 31 **Hint 2** 32 33 There is no answer for the remainder 0. 34 35 **Hint 3** 36 37 For all other remainders, there is an answer. 38 39 **Tutorial** 40 41 Note that from the number $k$ we only need the remainder modulo $4$, so we take $k$ modulo and assume that $0 \leq k \leq 3$. 42 43 If $k = 0$, then there is no answer, because the product of the numbers in each pair must be divisible by $4 = 2^2$, that is, the product of all numbers from $1$ to $n$ must be divisible by $2^{\frac{n}{2} \cdot 2} = 2^n$, but the degree of occurrence of $2$ in this sum is $\lfloor\frac{n}{2}\rfloor +\lfloor\frac{n}{4}\rfloor$, which is less than $n$. 44 45 If $k = 1$ or $k = 3$, then we will make all pairs of the form $(i, i + 1)$, where $i$ is odd. Then $a + k$ will be even in each pair, since $a$ and $k$ are odd, and since $b$ is also even, the product will be divisible by $4$. Если $k = 2$, then we will do the same splitting into pairs, but in all pairs where $i + 1$ is not divisible by $4$, we will swap the numbers (that is, we will make $a = i + 1$ and $b = i$). Then in pairs where $i + 1 \operatorname{mod} 4 = 0$, $b$ is divisible by $4$ (therefore the product too), and in the rest $a + k$ is divisible by $4$ (since $a$ and $k$ have a remainder $2$ modulo $4$). 46 47 The complexity of the solution is $O(n)$. 48 49 [1719C - Fighting Tournament](/contest/1719/problem/C "Codeforces Round 814 (Div. 2)") 50 51 **Hint 1** 52 53 Who would win duels if the strongest athlete was the first in queue? 54 55 **Hint 2** 56 57 After what number of rounds is the strongest athlete guaranteed to be at the front of the queue? 58 59 **Hint 3** 60 61 Simulate the first $n$ rounds and write down their winners so that for each person you can quickly find out the number of wins in rounds with numbers no more than a given number. 62 63 **Tutorial** 64 65 Note that after the strongest athlete is at the beginning of the queue, only he will win. The strongest athlete will be at the beginning of the queue no more than after the $n$-th round. Let's simulate the first $n$ rounds, if the $j$-th athlete won in the $i$-th round, then we will write down the $i$ number for him. Now, to answer the query $(i, k)$, it is enough to find the number of wins of the $i$ athlete in rounds with numbers $j \leq k$, and if $k \gt n$, and the strength of the $i$ athlete is equal to $n$, then add to the answer another $k - n$. 66 67 The complexity of the solution is $O(n + q \operatorname{log}(n))$. 68 69 [1718A2 - Burenka and Traditions (hard version)](/contest/1718/problem/A2 "Codeforces Round 814 (Div. 1)") 70 71 **Hint 1** 72 73 Segments of length greater than 2 are not needed. 74 75 **Hint for A1 1** 76 77 For A1 you can use dynamic programming 78 79 **Hint for A1 2** 80 81 Dynamic programming where dp[i][v] means the minimum time to make $a_j = 0$ for $j \lt i$ and $a_i = v$. (Notice that v can be any number from 0 to 8191) 82 83 **Hint 2** 84 85 There is an answer in which segments of length $2$ does not intersect with segments of length $1$. 86 87 **Hint 3** 88 89 If $a_l\oplus a_{l + 1} \oplus \ldots \oplus a_{r} = 0$ is executed for $l, r$, then we can fill this segment with zeros in $r-l$ seconds using only segments of length 2. 90 91 **Tutorial** 92 93 There is an answer where the time spent is minimal and the lengths of all the segments taken are 1 and 2. because of the segment $l, r, x$ can be replaced to $\lceil\frac{r-l+1}{2}\rceil$ of segments of length 2 and 1, or rather $[l, l + 1, x], [l+ 2, l + 3, x], \ldots, [r, r, x]$(or $[r-1, r, x]$ if $(l - r + 1)$ even). 94 95 Note that if $a_l\oplus a_{l + 1} \oplus \ldots \oplus a_{r} = 0$ is executed for $l, r$, then we can fill the $l, r$ subsections with zeros for $r-l$ seconds with queries $[l, l+1, a_l], [l+1, l+2, a_l \oplus a_{l+1}], ... [r-1, r, a_l\oplus a_{l+1} \oplus \ldots \oplus a_r]$. 96 97 Note that if a segment of length 2 intersects with a segment of length 1, they can be changed to 2 segments of length 1. 98 99 It follows from all this that the answer consists of segments of length 1 and cover with segments of length 2. Then it is easy to see that the answer is ($n$ minus (the maximum number of disjoint sub-segments with a xor of 0)), because in every sub-segments with a xor of 0 we can spend 1 second less as I waited before. this amount can be calculated by dynamic programming or greedily. Our solution goes greedy with a set and if it finds two equal prefix xors($prefix_l = prefix_r$ means that $a_{l + 1} \oplus a_{l+2} \oplus \ldots \oplus a_{r} = 0$), it clears the set. [168724728](/contest/1718/submission/168724728 "Submission 168724728 by GoreMneGore") 100 101 The complexity of the solution is $O(n \operatorname{log}(n))$. 102 103 [1718B - Fibonacci Strings](/contest/1718/problem/B "Codeforces Round 814 (Div. 1)") 104 105 **Common hint** 106 107 Try to express $n$(the sum of all $c_i$) as $F_1 + F_2 + \ldots + F_m = n$. 108 109 **The way of mathematicians** 110 111 **Hint for mathematicians 1** 112 113 Let, $n = F_1 + F_2 + \ldots + F_m$, then try to find the minimum $x$ starting from which if there is $c_i=x$ there is no answer. 114 115 **Hint for mathematicians 2** 116 117 $x$ is (the maximum sum of the Fibonacci numbers, among which there are no neighbors) + 1. 118 119 **Hint for mathematicians 3** 120 121 $x$ is $F_1 + F_3 + F_5 + \ldots + F_{m} + 1$ for odd $m$ and $F_2 + F_4 + F_6 + \ldots + F_{m} + 1$ for even. 122 123 **Hint for mathematicians 4** 124 125 $x$ is $F_{m+1} + 1$ for odd $m$ and $F_{m+1}$ for even. 126 127 **Hint for mathematicians 5** 128 129 Try to understand if it can happen that there are two different answers in which the letter forming the block of length $F_{m}$ differs. 130 131 **Hint for mathematicians 6** 132 133 The correct answer is: yes if there is $c_i = F_m$ and $c_j = F_m$ and this is only way. 134 135 Now, using everything you have understood, write a greedy solution. 136 137 **The way of programmers** 138 139 **Hint for programmers 1** 140 141 Try to write a greedy solution. 142 143 **Hint for programmers 2** 144 145 The programmer does not need a second hint, he wrote a greedy solution after the first one. 146 147 **Tutorial** 148 149 At the beginning, let's check that the number $n$ (the sum of all $c_i$) is representable as the sum of some prefix of Fibonacci numbers, otherwise we will output the answer NO. 150 151 Let's try to type the answer greedily, going from large Fibonacci numbers to smaller ones. For the next Fibonacci number, let's take the letter with the largest number of occurrences in the string from among those that are not equal to the previous letter taken (to avoid the appearance of two adjacent blocks from the same letter, which cannot be). If there are fewer occurrences of this letter than this number, then the answer is NO. Otherwise, we will put the letter on this Fibonacci number and subtract it from the number of occurrences of this letter.If we were able to dial all the Fibonacci numbers, then the answer is YES. 152 153 Why does the greedy solution work? Suppose at this step you need to take $F_i$ (I will say take a number from $c_t$, this will mean taking $F_i$ letters $t$ from string), let's look at the maximum $c_x$ now, if it cannot be represented as the sum of Fibonacci numbers up to $F_i$ among which there are no neighbors, then the answer is no. It can be proved that if $c_x \gt F_{i+1}$, then it is impossible to represent $c_x$. 154 155 If there is exactly one number greater than or equal to $F_i$ at the step, then there is only one option to take a number, so the greedy solution works. 156 157 If there are two such numbers and they are equal, then the option to take a number is exactly one, up to the permutation of letters. the greedy solution works again. 158 159 If there are $c_j \geq F_i, c_x \geq F_i, j \ne x$, then we note that the larger of the numbers $c_j, c_x$ will be greater than $F_i$, if we don't take it, then at the next step this number will be greater than $F_{i+1}$ ($i$ will be 1 less), according to the above proven answer will not be, so, taking the larger of the numbers is always optimal. 160 161 The complexity of the solution is $O(k \operatorname{log}(n)\operatorname{log}(k))$. 162 163 [1718C - Tonya and Burenka-179](/contest/1718/problem/C "Codeforces Round 814 (Div. 1)") 164 165 **Hint 1** 166 167 The answer for $k=x$ and $k=\gcd(x,n)$ is the same. 168 169 **Hint 2** 170 171 In this problem, a general statement is useful: for any array $c$ of length $m$, $m\cdot\operatorname{max}(c_1, c_2, \ldots, c_m) \geq c_1 +c_2 +\ldots+c_m$ is true. 172 173 **Hint 3** 174 175 Try applying the second hint for $k_1$ and $k_2$ divisible by $k_1$, where $k_1, k_2$ are divisors of $n$. 176 177 **Hint 4** 178 179 We can consider only $k$ which equals to $\frac{n}{p}$, where $p$ is a simple divisor of $n$. 180 181 **Tutorial** 182 183 Let's note that the answer for $k=x$ and $k=\gcd(x,n)$ is the same. 184 185 Indeed, for the number $k$, we will visit numbers with indices $s + ik \mod n$ for $i$ from $0$ to $n-1$ inclusive, from this we can see that the index of the $i$-th number coincides with the index of $i + \frac{n}{\gcd(k,n)}$, and if we look at two indexes, the difference between which is $l$ and $l \lt \frac{n}{\gcd(k,n)}$, then they are different, since $k\cdot l \mod n \neq 0$, therefore, the answer is (the sum of numbers with indices $s + ik \mod n$ for $i$ from $0$ to $\frac{n}{\gcd(k,n)}-1$) $\cdot\gcd(k,n)$. 186 187 Now let's prove that the first $\gcd(k,n)$ numbers are the same for $(s,x)$ and $(s,\gcd(x,n))$, note that only those indices that have the same remainder as $s$ when divided by $\gcd(x,n)$ are suitable, but there are only $\frac{n}{\gcd(k,n)}$ of such indices, and we proved that we should have $\frac{n}{\gcd(k,n)}$ of different indices, so they are all represented once, therefore the answer for $k=x$ and $k=\gcd(x, n)$ is the same, because the sum consists of the same numbers. 188 189 So, we need to consider only $k$ being divisors of $n$, this is already passes tests if we write, for example, a segment tree, but we don't want to write a segment tree, so we go further, prove that for $k_1 = x$, the answer is less or equal than for $k_2 = x \cdot y$ if $k_1$ and $k_2$ are divisors of $n$, why is this so? 190 191 Note that for the number $k$, the answer beats for $\gcd(k,n)$ groups of numbers, so that in each group there is exactly $\frac{n}{\gcd(k,n)}$ and each number is in exactly one group, and for different $s$ the answer will be (the sum in the group that $s$ belongs to) $\cdot\gcd(k,n)$. 192 193 Let's look at the answer for the optimal $s$ for $k_1$, let's call the set at which it is reached $t$, note that in $k_2$ for different $s$ there are $m$ independent sets that are subsets of $t$. Let $m_i$ be the sum in the $i$-th set. Now note that we need to prove $max(m_1, m_2, \ldots, m_y) * y\geq m_1 +m_2 +\ldots m_y$ this is true for any $m_i$, easy to see. 194 195 So you need to iterate the divisors which equals to $\frac{n}{p}$ where $p$ is prime, now it can be passed with a set. Hurray! 196 197 For the divisor $d$, it is proposed to store answers for all pairs $(s, d)$, where $s\le\frac{n}{d}$ and the maximum among them, they can be counted initially for $O(n log n)$ for one $d$, each request is changing one of the values, this can be done for $O(log n)$. 198 199 The complexity of the author's solution is $O(6\cdot n\operatorname{log}(n))$, where 6 is the maximum number of different prime divisors of the number $n$. 200 201 [1718D - Permutation for Burenka](/contest/1718/problem/D "Codeforces Round 814 (Div. 1)") 202 203 **Hint 1** 204 205 Try to reduce the task to a single array and tree. 206 207 **Hint 2** 208 209 Try to reduce the tree to a match of bipartite graph. 210 211 The first part: For each $a_i=0$, a segment of suitable values. 212 213 The second part: The set $S$ and the number $d$. 214 215 **Hint 3** 216 217 There exist $L$ and $R$ such that the answer to the query is ''YES'' if and only if $L \le d \le R$. 218 219 **Tutorial** 220 221 Let's build a tree recursively starting from the segment $[1, n]$, at each step we will choose the root of the subtree $v = \operatorname{argmax}([p_l, p_{l+1}\ldots, p_r]) +l - 1$, then recursively construct trees for subtrees $[l, v-1], [v+1, r]$ if they are not empty, then create edges from the root to the roots of these subtrees. What we got is called a Cartesian tree by array. This Cartesian tree will be mentioned further in the analysis. 222 223 It is easy to see that Cartesian trees by arrays coincide if and only if these arrays are similar. Then the necessary and sufficient condition for the array $a$ to be similar to $p$ is that for any pair $u, v$ such that $u$ is in the subtree $v$, $a_v \gt a_u$ is satisfied, in other words $a_v$ is the maximum among the numbers in the subtree of the vertex $v$. 224 225 Let's call the position $i$ initially empty if initially $a_i = 0$. 226 227 Let's call an array $a$ almost similar to $p$ if for any pair $u, v$ such that $u$ is in the subtree $v$, $a_v \gt a_u$ is executed, **or both** positions $u$, $v$ are initially empty. 228 229 Let's prove that if there is a way to fill in the gaps in $a$ to get an array almost similar to $p$, then there is also a way to fill in the gaps to get a similar to $p$ array $a$. 230 231 Indeed, let's look at the $a$ array almost similar to $p$. let's walk through the tree recursively starting from the root. At the step with the vertex $v$, we first start recursively from all the children of $v$, now it is true for them that the maxima of their subtrees are in them, let's look at the maximum child $v$, let it be $u$, then if $a_v \gt a_u$, then everything is fine, otherwise $a_v \lt a_u$ note that $v$ is initially an empty position, because for all initially non-empty positions it is true that they are maximums in their subtrees (this is easy to see in the definition), but $v$ is not. Note that $u$ is initially an empty position. Otherwise, we have never changed $a_v$ and $a_u$, therefore, in the original array $a$ almost similar to $p$, $a_v \gt a_u$ contradiction was executed, it is contradiction. so $v, u$ are initially empty, we can perform $swap(a_v, a_u)$ and everything will be executed. 232 233 After executing this algorithm, we changed only the initially empty elements and got $a$ similar to $p$. Q.E.D. 234 235 How to check the existence of an array almost similar to $p$? Let's call the number $r_i$ the minimum among all the numbers $a_v$ such that $i$ is in the subtree of $v$. Let's call the number $l_i$ the maximum among all the numbers $a_v$ such that $v$ is in the subtree of $i$. it is easy to see that $a$ is almost similar to $p$ if and only if for all $i$, $l_i \lt a_i \lt r_i$ is satisfied. 236 237 That sounds incredibly good. So, all we need is to find a matching of the set $S$ and number $d$ and segments $[l_i, r_i]$ for $i$ that are initially empty. Now it is easy to prove that the suitable $d$ is a continuous segment (let's say the answer is yes for $d_1$, we don't know the answer for $d_2$, try looking at the alternating path from $d_1$ to $d_2$ in good matching for $d_1$, it's easy to see that if there is such a path, then for $d_2$ the answer is yes, otherwise, no. If you look at the structure of the matching of points with segments, you can see that an alternating path exists for a continuous segment of values $d_2$). the boundaries can be found by binary search or by typing greedily twice. 238 239 Final complexity is $O(n \operatorname{log} n)$ or $O(n \operatorname{log}^2 n)$ 240 241 [1718E - Impressionism](/contest/1718/problem/E "Codeforces Round 814 (Div. 1)") 242 243 **Hint 1** 244 245 you can notice that a pair of operations $1\, i\, j$, $2\, k\, t$ and $2\, k\, t$, $1\, i\, j$ lead to the same result, what does this mean? 246 247 **Hint 2** 248 249 This means that operations can be represented as two permutations describing which swaps will occur with rows and which swaps will occur with columns. 250 251 **Hint 3** 252 253 Try to reduce this problem to a graph isomorphism problem. 254 255 **Tutorial** 256 257 Let's have two permutations: $p$ of length $n$ and $q$ of length $m$. Initially, both permutations are identical. For operation $1\, i\, j$, we will execute $\operatorname{swap}(p_i, p_j)$, for operation $2\, i\, j$ we will execute $\operatorname{swap}(q_i, q_j)$. It is easy to see that after performing all operations in the position $i, j$ will be $a_{p_i,q_j}$. 258 259 Now we need to find permutations of $p, q$ so that $a_{p_i,q_j} = b_{i, j}$. 260 261 Let's look at a bipartite graph where the first part is rows and the second is columns. Let if $a_{i,j}\ne 0$, then from $i$ there is an undirected edge in $j$ of the color $a_{i, j}$. if we construct the same graph for $b$, then we need to check for isomorphism two bipartite graphs, each edge of which has a color, and as we remember, by the condition of the vertex, 2 edges of the same color cannot be Incidence. Let's call them graph $a$ and graph $b$. 262 263 This problem is largely for implementation, therefore, starting from this moment, the actions that you can observe in the author's solution are described [168728958](/contest/1718/submission/168728958 "Submission 168728958 by GoreMneGore"). 264 265 Let $n \lt m$ otherwise let's swap $n$ and $m$ in places. Let's try to match the elements of the array $a$ to the elements of the array $b$. Let's go through the indices from $1$ to $n$. Let's now choose which pair of $b$ to choose for the $i$-th vertex of the first fraction of the graph $a$. If we have already found a pair earlier, skip the step, otherwise let's sort out the appropriate option for the pair among all the vertices of the first component $b$ that do not yet have a pair, there are no more than $n$, after the vertices are matched, the edges are also uniquely matched (because there are no 2 edges of different colors), let's then run the substitution recursively for edges (if we have mapped the vertex $v$ to the vertex $u$ and from $v$ there is an edge of color $x$ in $i$, and from $u$ there is an edge $x$ in $j$, then it is easy to prove that in the answer $i$ will be mapped to $j$), which means we will restore the entire component. Let the sum of the number of vertices and the number of edges in the component be $k$, then we will perform no more than $n \cdot k$ actions to restore the component. 266 267 by permutations, getting a sequence of swaps is a matter of technique, I will not explain it, but this is a separate function in the author's solution you will understand. 268 269 in total, our solution works for $O(n(n\cdot m + n + m))$ or $O(min(n, m)(n\cdot m + n + m))$, where $n\cdot m + n +m$ is the total number of vertices and edges in all components. 270 271 feel free to ask any questions 272 273 [1718F - Burenka, an Array and Queries](/contest/1718/problem/F "Codeforces Round 814 (Div. 1)") 274 275 **Hint** 276 277 The only one hint to this problem is don't try to solve, it's not worth it. 278 279 **Tutorial** 280 281 In order to find the number of numbers from $1$ to $C$ that are mutually prime with $x$, we write out its prime divisors (various). Let these be prime numbers $a_{1}, a_{2}, \ldots, a_{k}$. Then you can find the answer for $2^{k}$ using the inclusion-exclusion formula. 282 283 Because $\left\lfloor\frac{a}{b\cdot c}\right\rfloor = \left\lfloor\frac{\left\lfloor\frac{a}{b}\right\rfloor}{c}\right\rfloor$, a similar statement is made for $k$ numbers (you can divide completely in any order). 284 285 Let's split the primes up to $2\times 10^4$ into 2 classes — small ($\leq 42$) and large (the rest). There are 13 small numbers, and 2249 large ones. Separately, we will write down pairs of large numbers that in the product give a number not exceeding $10 ^ 5$. There will be 4904 such pairs. 286 287 Let's match each set of small primes with a mask. Let's write $dp[mask]$ — alternating sum over the $mask$ submasks of numbers $\left\lfloor\frac{C}{a_{1}\cdot a_{2}\cdot\ldots \cdot a_{k}} \right\rfloor$, where $a_{1}, \ldots, a_{k}$ — prime numbers from the submask. Similarly, we define $dp[mask][big]$ (in addition to the mask, there is a large prime $big$) and $dp[mask][case]$ (in addition to the mask, there are a pair of primes from a pair of large primes $case$). Each $dp$ can be counted for $states\times bits$, where $states$ — is the number of states in $dp$, and $bits$ — is the mask size (number of bits). 288 289 If we write out all the large primes on the segment for which $mask$ — mask of small primes, the answer for this segment will be the sum of $dp[mask] + \sum dp[mask][big_{i}] + \sum dp[mask][case_{i}]$ (for $big$ and $case$ lying on the segment). Thus, the request can be answered in 7000 calls to $dp$. 290 291 In order to find a set of prime numbers on a segment, you can use the MO algorithm. 292 293 Final complexity is $O(n\sqrt{n} + q\times(\pi(m)+casesCount(C)))$ 294 295 It is worth noting that (with a very strong desire) it is possible to further optimize the solution using avx, to speed up the calculation of the amount by submasks in dynamics by 16 times, to speed up the calculation of the amount of $dp[mask][big]$ by 8 times, which will allow you to answer the request in ~5000 (instead of 7000) calls to $dp$, and the pre-calculation for $4\cdot 10^7$ instead of $7\cdot 10^8$ (in fact, the pre-calculation works much faster due to compiler optimizations). 296 297 Please rate the problems, it will help us make the problems better next time! 298 299 **Problem Feedback** 300 301 You can choose one best problem, or not choose if you think there is no such task. 302 303 **div2A** 304 305 * The best problem 306 * Good problem 307 * Mid problem 308 * Bad problem 309 310 **div2B** 311 312 * The best problem 313 * Good problem 314 * Mid problem 315 * Bad problem 316 317 **div2C** 318 319 * The best problem 320 * Good problem 321 * Mid problem 322 * Bad problem 323 324 **div2D/div1A** 325 326 * The best problem 327 * Good problem 328 * Mid problem 329 * Bad problem 330 331 **div2E/div1B** 332 333 * The best problem 334 * Good problem 335 * Mid problem 336 * Bad problem 337 338 **div2F/div1C** 339 340 * The best problem 341 * Good problem 342 * Mid problem 343 * Bad problem 344 345 **div1D** 346 347 * The best problem 348 * Good problem 349 * Mid problem 350 * Bad problem 351 352 **div1E** 353 354 * The best problem 355 * Good problem 356 * Mid problem 357 * Bad problem 358 359 **div1F** 360 361 * The best problem 362 * Good problem 363 * Mid problem 364 * Bad problem