codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

507.md (8739B)


      1 Source: https://codeforces.com/blog/entry/15975
      2 
      3 ## [507A - Amr and Music](/contest/507/problem/A "Codeforces Round 287 (Div. 2)")
      4 
      5 **Problem:** We have to split the number *k* into maximum number of elements of *a**i* such that their sum is less than or equal to *k*.
      6 
      7 **Hint:** To maximize the answer we have to split the number into the smallest numbers possible.
      8 
      9 **Solution:** So and since the limits are small we can pick up the smallest element of the array and subtract it from *k*, and we keep doing this *n* times or until the smallest number is larger than *k*. Another solution is to sort the array in non-decreasing order and go on the same way.
     10 
     11 **Time complexity:** ![](https://espresso.codeforces.com/5041f7482b690d42b6fe1f757903ad4d6d78b5df.png) or ![](https://espresso.codeforces.com/acfcb9c48f12a25fa065d71a21a535a793eefa3a.png)
     12 
     13 **Implementation:** [9529124](/contest/507/submission/9529124 "Submission 9529124 by RetiredAmrMahmoud") .
     14 
     15 ## [507B - Amr and Pins](/contest/507/problem/B "Codeforces Round 287 (Div. 2)")
     16 
     17 **Problem:** We have a circle with radius *R* at position (*x*, *y*) and we want to move it to (*x*', *y*') with minimum moves possible. A move is to choose an arbitrary point on the border of the circle and rotate the circle around it with arbitrary angle.
     18 
     19 **Hint:** What is the shortest path between two points? A direct line. So moving the center on that line with maximum move possible each time will guarantee minimal number of moves.
     20 
     21 **Solution:** Let's draw a straight line between the two centers.
     22 
     23 ![ ](/predownloaded/fb/5e/fb5ebcbfcf8fecac7fae6f050305ed514b80edd4.jpg)
     24 
     25 Clearly to move the center with maximum distance we need to rotate it around the intersection of the line and the circle with 180 degrees. So the maximum distance we can move the center each time is 2 * *R*. Let's continue moving the center with 2 * *R* distance each time until the two circles intersects. Now obviously we can make the center moves into its final position by rotating it around one of the intersection points of the two circles until it reaches the final destination.
     26 
     27 ![ ](/predownloaded/57/47/5747093ddd1c02d57fc37a8ceab7a261f56397e3.jpg)
     28 
     29 Every time we make the circle moves 2 * *R* times except the last move it'll be  ≤ 2 * *R*. Assume that the initial distance between the two points is *d* So the solution to the problem will be ![](https://espresso.codeforces.com/b53f42b77cb77e03754f74bde5e1520f918f022d.png).
     30 
     31 **Time complexity:** ![](https://espresso.codeforces.com/7d14bca19a169c94ce6129784f6abb062655d250.png)
     32 
     33 You have to be careful of precision errors. Here is a code that used only integer arithmetic operations [9529147](/contest/507/submission/9529147 "Submission 9529147 by RetiredAmrMahmoud").
     34 
     35 ## [507C - Guess Your Way Out!](/contest/507/problem/C "Codeforces Round 287 (Div. 2)")
     36 
     37 **Hint:** Simulate the algorithm until we reach a leaf node assume that it's not the exit. Now the question is Are there some nodes that are guaranteed to be visited before trying to reach the exit again?
     38 
     39 **Solution:** The first observation is that in order to return to a parent we will have to visit all nodes of the right or the left subtree of some node first. Now imagine we are in the situation below where *E* is the exit.
     40 
     41 ![ ](/predownloaded/1c/3e/1c3e5022ccd6fb742d3a2a09a01616fb18ca6dc8.jpg)
     42 
     43 By applying the algorithm we'll reach node *X*. Both the *E* and *X* are in different subtrees of the root. Which means before going to the proper subtree in which the Exit exists we'll have to visit all the nodes of the left subtree (marked in red).
     44 
     45 This means we have to get the node which the Exit and the current leaf node *X* are in different subtrees which will be the least common ancestor (LCA) of the two nodes. Assume the subtree height is *h*1. This means we visited ![](https://espresso.codeforces.com/91f5b33e220d6fa3c0d483f102fc44e4d68fe25c.png) node. By adding the nodes above the subtree which we visited during executing the string for the first time the total number of visited nodes will be ![](https://espresso.codeforces.com/73860b3a41fd2eecf0f89ce271614fb670bc8a73.png). Now let's go to the other subtree. Obviously we don't need any other nodes except this subtree. So let's do the same we did to the original tree to this subtree. Execute the algorithm until we reach a leaf node, get the LCA, add to the solution ![](https://espresso.codeforces.com/ecda25d5cf2ebc96e571e28237158b21fb4b3f25.png) where *h*2 is the height of the subtree of the LCA node where the leaf node exists. And so on we keep applying the rules until after executing the algorithm we will reach the exit.
     46 
     47 Also we can do the same operations in *O*(*h*) by beginning to move from the root, if the exit is located to the left we go to the left and ans++ and then set the next command to 'R' else if it is located to the right we will visited the whole left subtree so we add the left subtree nodes to the answer ![](https://espresso.codeforces.com/2b165db006bd332dd9eb2f2ca02c52a2cf819d60.png) and then set the next command to 'L' and so on.
     48 
     49 **Time complexity:** ![](https://espresso.codeforces.com/9a20c7b96289c93f7e768cb4305c25f5e84b4a4f.png) or ![](https://espresso.codeforces.com/6f6e1c82f48c48307bdeae1174f55b8d87ec9192.png)
     50 
     51 **Challenge:** What if the pattern is given as an input (e.g. "LRRLLRRRLRLRLRR..."), How can this problem be solved?
     52 
     53 **Implementation:** [9529181](/contest/507/submission/9529181 "Submission 9529181 by RetiredAmrMahmoud")
     54 
     55 ## [507D - The Maths Lecture](/contest/507/problem/D "Codeforces Round 287 (Div. 2)")
     56 
     57 **Hint:** Dynamic programming problem. To handle repetitions we have to construct the number from right to the left and calculate the answer when we reach a number equivalent to 0 modulo *k*.
     58 
     59 **Solution:** Let's define ![](https://espresso.codeforces.com/de5409337499b68c9045d6329fe42c9644c9412b.png) as a recursive functions calculates the number of numbers consisting of *n* digits satisfying the conditions of the problem and with a specific suffix of length *i* *S**i* such that ![](https://espresso.codeforces.com/2c66e9e73608d7b00c21115578944eeb1f550ee1.png).
     60 
     61 We want to avoid repetition so by constructing the number from the right to the left when we reach a state with *j* = 0 with suffix  ≠ 0 we return the answer immediately so any other suffix that contains this suffix won't b calculated.
     62 
     63 So the base cases are ![](https://espresso.codeforces.com/61af5be2aa2da167e4f6bb42d953911f1d653ec6.png), ![](https://espresso.codeforces.com/e133a9466fab540be9f4b7833086edbe9dce012d.png).
     64 
     65 So state transitions will be ![](https://espresso.codeforces.com/05e5217792715c3ca7daf883cffed23cb93e29e3.png) (We add a digit to the left).
     66 
     67 And we can handle *j* = 0 case coming from a zero suffix easily with a boolean variable we set to true when we use a digit  ≠ 0 in constructing the number.
     68 
     69 **Time complexity:** ![](https://espresso.codeforces.com/ebc5b5b6d0025eb9721a92c89cd6cc8d8fe37216.png)
     70 
     71 **Implementation:** [9529210](/contest/507/submission/9529210 "Submission 9529210 by RetiredAmrMahmoud")
     72 
     73 ## [507E - Breaking Good](/contest/507/problem/E "Codeforces Round 287 (Div. 2)")
     74 
     75 **Hint:** Consider we've chosen a certain path with length *d* where *d* is the length of the shortest path from 1 to *n* and it has *x* edges that are working. Assume that *y* is the total number of edges that are working in the whole country. So we need to make *d* - *x* changes (to make the malfunctioning edges on the path work) and *y* - *x* changes (to blow up all other edges that don't lie on the path). So we will totally make *d* + *y* - 2 * *x* changes where *d* and *y* are constants. So the optimal solution will depend only on number of working edges along the path. So we'll have to maximize this number!
     76 
     77 **Solution:** We will use dynamic programming on all nodes that lies on some shortest path. In other words, every node *x* that satisfies that the shortest path from 1 to *x* + the shortest path from *x* to *n* equals *d* where *d* is the length of the shortest path from 1 to *n*. Let's define *Max*[*x*] is the maximum number of working edges along some shortest path from 1 to *x*. We can calculate the value *Max*[*x*] for all nodes by dynamic programming by traversing the nodes in order of increasing shortest path from node 1. So at the end we'll make *d* + *y* - 2 * *Max*[*n*] changes. We can get them easily by retrieving the chosen optimal path.
     78 
     79 **Time complexity:** ![](https://espresso.codeforces.com/aa0db070d61b821248d15f6683f412ce5c0320a4.png)
     80 
     81 **Implementation:** [9529272](/contest/507/submission/9529272 "Submission 9529272 by RetiredAmrMahmoud")