awoo's blog

By awoo, history, 4 years ago, translation, In English

1499A - Domino on Windowsill

Idea: adedalic

Tutorial
Solution (adedalic)

1499B - Binary Removals

Idea: BledDest

Tutorial
Solution (Neon)

1499C - Minimum Grid Path

Idea: adedalic

Tutorial
Solution (adedalic)

1499D - The Number of Pairs

Idea: Neon

Tutorial
Solution (Neon)

1499E - Chaotic Merge

Idea: BledDest

Tutorial
Solution 1 (awoo)
Solution 2 (awoo)

1499F - Diameter Cuts

Idea: BledDest

Tutorial
Solution (awoo)

1499G - Graph Coloring

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +111
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Atlast THE EDITORIAL

»
4 years ago, # |
  Vote: I like it +43 Vote: I do not like it

Why no best hackers :(

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +44 Vote: I do not like it

    Nice hacking bro

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    In problem D, why there are 2^(the number of prime divisors of k) such pairs. Any proof for that please explain!

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      You have two options for each prime factor, either it is the part of x or it is the part of y. So the total number of pairs = 2^(number of prime divisors).

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +12 Vote: I do not like it

      Consider each prime factor $$$ p $$$ of $$$ k $$$. Assume that the prime $$$ p $$$ appears $$$ x $$$ time(s) in the factorization of $$$ k $$$ ($$$ p^x | k, p^{x + 1} \not| k $$$), appears $$$ y $$$ time(s) in $$$ A $$$'s and $$$ z $$$ time(s) in $$$ B $$$'s.

      Since $$$ \gcd(A, B) = 1 $$$, either $$$ y $$$ or $$$ z $$$ must be equal to $$$ 0 $$$ (otherwise $$$ p|A, p|B, p|\gcd(A, B) $$$). Also we have $$$ y + z = x $$$ for $$$ AB = k $$$.

      So there are just $$$ 2 $$$ possibilities: $$$ y = 0, z = x $$$ or $$$ y = x, z = 0 $$$. Because $$$ p $$$ is one of the prime factors of $$$ k $$$, $$$ x > 0 $$$ holds. Therefore, the $$$ 2 $$$ situations are definitely different.

      For each prime factor there are $$$ 2 $$$ possibilities to "assign" it to $$$ A $$$ or $$$ B $$$. Apparently the situation of one prime factor is independent of others. So simply multiply them all and the answer is $$$ 2^{\text{number of prime divisors of k}} $$$.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        What's the matter of low prime, i am not getting it. Please explain it.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      For each prime divisors, we can allocate to either A or B. So for every new prime divisor we have, we can multiply the ans by 2. Hence 2 to the power of the number of prime divisors.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem C was tricky

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you explain me C problem pls?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes please, the editorial for problem C was not very clear.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I didn't solve it too, that's why I am saying it was tricky)

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +6 Vote: I do not like it

        i have solved using this approach... 1. we treat every index i in range [0,n) as the last segment and compute the total cost such that we reach [n,n] and we have used i segments to reach here, and take minimum of all total costs. 2. here, all even indices segments will be for y-axis or x-axis but not both similarly for odd indices segments. 3. now we calculate minimum value in even indices and minimum in odd indices till i. 4. lets say we travel y axis using even indices and x axis using odd indices. 5. To travel n in Y direction it will be ideal if we move by length 1 in all even indices segments and remaining length by minimum value even segment. As we are using all segments up to i, because our assumption is that i is our last segment. because any way we need to use all even segments for travelling y axis. 6. similarly for x axis 7. we take minimum of all values.

        my code: https://mirror.codeforces.com/contest/1499/submission/110382254

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        You can refer to this video for detailed solution as well as explanation. Link to Explanation

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +4 Vote: I do not like it

      The most important thing for problem $$$C$$$ is to realise that in any solution of length $$$k$$$ you are obliged to move at least $$$1$$$ in all the $$$k$$$ movements. Therefore, if you iterate over $$$k$$$ you know every single $$$c_i$$$ for $$$1 \leq i \leq k$$$ is added at least once to the cost. This means we can accumulate sum of $$$c_i$$$ up to current $$$k$$$, we will call this $$$accum_k$$$.

      If $$$k$$$ is even we moved $$$cntUp = k/2$$$ times up and $$$cntRight = k/2$$$ times right. If $$$k$$$ is odd then it's floor($$$\frac{k}{2}$$$) $$$+ 1$$$ in one direction and floor($$$\frac{k}{2}$$$) in the other, just try both possibilities of assigning this to $$$cntUp$$$ and $$$cntRight$$$. Since we have already taken into account moving at least 1 every time, we just have to calculate the cost of moving $$$n - cntUp$$$ units up and $$$n - cntRight$$$ times right considering only $$$c_i$$$ with $$$1 \leq i \leq k$$$.

      Actually, you can only use $$$c_i$$$ with odd $$$i$$$ for the direction you start with and only $$$c_i$$$ with even $$$i$$$ for the other direction. Also, you can easily see the optimal way to traverse the remaining units is to do so at the step with minimum $$$c_i$$$. This is why we compute a $$$minOdd$$$ and $$$minEven$$$ while we iterate over $$$k$$$.

      So to get the optimal answer for a given $$$k$$$:

      If $$$k$$$ is even $$$cntUp == cntRight$$$, so it doesn't matter if we start up or right and the answer is $$$accum_k + (n - cntUp)*minOdd + (n - cntRight)*minEven$$$

      If $$$k$$$ is odd we try both possibilities of starting up or right, so we choose the minimum between $$$accum_k + (n - cntUp)*minOdd + (n - cntRight)*minEven$$$ where $$$cntUp =$$$ floor($$$\frac{k}{2}$$$) $$$+ 1$$$ and $$$cntRight =$$$ floor($$$\frac{k}{2}$$$) and $$$accum_k + (n - cntUp)*minEven + (n - cntRight)*minOdd$$$ with $$$cntUp$$$ and $$$cntRight$$$ from the previous case swapped.

      We do this for every $$$k$$$, not forgetting to update $$$minEven$$$ and $$$minOdd$$$ in the corresponding iterations, and take the minimum from all possible $$$k$$$.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 5   Vote: I like it +4 Vote: I do not like it
      This is what i think: In order to go from (0, 0) to (n, n),
      we move both up and right exactly n times.
      
      So the sum of length of segment of each direction is equal to n.
      Ex: n = 3
      R1 U R2 (R1 + R2 = U = 3)
      1  3  2 is right
      3  3  0 is right // zero(s) don't appear in front of any positive number
      3  0  3 -> wrong !
                ____> (n, n)
               |
               |
      (0, 0)>__|
      
      The formula above can be explained: consider i (0<i<n) is the last positive number in
      the following lengths, the optimal choice is set all 0->i th the length 1 and one minimal cost the length (n - cnt + 1) (length i+1->n-1 = 0)
      
      So the answer is the minimal result of all i = 1->n-1
      
    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      This is what my approach to problem C was: We are to go from origin(0,0) to a point (n,n),using no more than (n-1) turns. We can either go up or go right.It is important to realize that whichever direction we may start(up or right),it doesn't matter as in both the directions,we need to cover a distance equivalent to 'n' because going from (0,0) to (n,n) implies going 'n' units along x-axis and going 'n' units along y-axis. Now,we may realize that the "costs" given to us can be seen as 2different options,i.e. costs at even positions may be used to traverse along x-axis,and costs at odd positions maybe used to traverse along y-axis(you may even say that even positioned costs are used to traverse along y-axis and so on.As I said earlier,it doesn't matter whether you start from right or up as your first step)Just make sure that going right or up means that you can only use alternate "cost" values. Now,we may try a greedy approach by saying that let our answer be : n*(cost[0])+n*(cost[1]) And we may say that in the above expression,there are (n-1) steps that we may remove and take those steps with another cost value,if we get a better alternative in future. WHY?because in the worst case,we at least need to take '1' step for cost[0] and '1' step for cost[1],to even reach in a position to use value of cost[2]. Now,we traverse along the cost vector,and at every alternate position,we see that if the current cost is less than the cost that we used before,it means that we can take away the "removable steps" from the previous "higher" cost,and consider them from the current "lower" cost. Alternatively,if we find that the current cost is greater than the previous cost,we can just take one step and look for future values to see,if the final cost comes out to be less. Finally,we store all the cost values as we go along computing the cost,and in the end, print the minimum value of cost. Here's a link to my implementation of the above logic: https://mirror.codeforces.com/contest/1499/submission/110359103

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        https://mirror.codeforces.com/contest/1499/submission/122382473

        This implementatiion is exactly on the same idea yet fails at a testcase and it is annoying as the idea is exactly the same. Can anyone suggest anything.

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
          Rev. 3   Vote: I like it 0 Vote: I do not like it

          You are calculating the minimum costs for going along the x-axis and y-axis independently and in the end, adding them up as the final answer. However, it may happen that the minimum cost you get while traversing the x-axis doesn't allow you to traverse far enough along the y-axis , so that you may obtain the minimum cost along y-axis. Suppose you have 12 different costs(you will consider 6 costs for the horizontal direction and 6 costs for the vertical direction). Suppose you get the minimum cost for traversing along the x-axis as some sum of the first 3costs(out of the 6 you have) . And in the same manner , you get the minimum cost for traversing along the y-axis as the sum of all 6 costs. You are adding both of these to obtain the final answer . However , this contradicts your own approach as if you took only the first 3 costs along the x-axis , you would never be able to traverse along the "costs" vector to take the 6minimum costs for the y-axis.(and hence never achieve the minimum cost along the y-axis).

»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

In problem F, we can merge subtrees in $$$O(k)$$$ time by computing prefix sums but the only problem is that we might need to calculate inverses which TLE'd for me :( then i just iterated upto max height which worked well (along with calculating inverses).

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    We can merge all subtrees of a node $$$u$$$ in $$$O(deg(u)\cdot k)$$$ with prefix sums, which leads to a $$$O(n\cdot k)$$$ solution, I did it without any divisions.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Can you explain how you merged the subtrees without calculating mod inverses?

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 2   Vote: I like it +8 Vote: I do not like it

        Let's denote $$$dp_{u,x}$$$ as the number of ways to partition the subtree of $$$u$$$, having a diameter equal or smaller than $$$k$$$, and the longest path starting from $$$u$$$ having a length of exactly $$$x$$$, and $$$ac_{u,x}$$$ as the number of ways to partition the subtree of $$$u$$$, having a diameter equal or smaller than $$$k$$$, and the longest path starting from $$$u$$$ having a length of at most $$$x$$$.

        Now the transitions:

        • If $$$2\cdot(x + 1) \leq k$$$ then $$$ac_{u,x+1}$$$ is equal to $$$\prod_{v \in children(u)} ac_{v,x}$$$.
        • Else, $$$dp_{v,x+1}$$$ is equal to $$$\sum_{v \in children(u)} f_{v,x}$$$ where $$$f_{v,x} = dp_{v,x} \cdot \prod_{w \in children(u), w \neq v} ac_{v,k-x-2}$$$

        Having at least one of $$$dp_{u,x}$$$ or $$$ac_{u,x}$$$ we can get the other easily, then the first case can be computed naively, and for the second one, it's possible to precompute for each prefix of childrens $$$\prod ac_{v,k-x-2}$$$, and also for each suffix, with this we can get all the things we need from some node $$$u$$$ in $$$O(deg_u*k)$$$

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    How did you get a way around with the inverses?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      I suppose that is similar to the solution that I described above, but instead of precompute for each prefix and suffix of childrens $$$\prod{ac_{v,k-x-2}}$$$ maintaining the multiplication of that for all childrens, and dividing by the current one.

»
4 years ago, # |
  Vote: I like it -24 Vote: I do not like it

There are several cases where the logic provided for problem C fails. Like how can someone be so sure that choosing the minimum cost will give the right answer? It might happen that in order to reach the min cost, we might end up adding a lot of highly expensive cost when we could have just used a 2nd lowest or 3rd lowest cost to cover the entire up/right distance.

I really thought I needed to use dynamic programming here.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    Trick is you iterate over number of turns you make, and when you fix number of turns , it is always optimal to use the minimum cost in given prefix.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

please any one clarify Problem — B edutorial (method-1)

notice that, in the sorted string, there is a prefix of zeroes and a suffix of ones. It means that we can iterate on the prefix (from which we remove all ones), and remove all zeroes from the suffix we obtain. If we try to remove two adjacent characters, then we cannot use this prefix;

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    If I'm not mistaken, method 1 is $$$O(n^2)$$$ where n is the lenght of s.

    If you iterate $$$i$$$ from $$$0$$$ to $$$n$$$, at iteration $$$i$$$ you consider first $$$i$$$ characters to be part of "the prefix" of the solution(where you can only have $$$0$$$'s or nothing) and all next characters to be part of "the suffix" of the solution(where you can only have $$$1$$$'s or nothing).

    Now, you can remove the $$$1$$$'s from the prefix if and only if it doesn't contain the sequence $$$11$$$ and you can remove the $$$0$$$'s from the suffix if and only if it doesn't contain the sequence $$$00$$$. You just traverse them linearly to check this. If for some $$$i$$$ you meet both conditions, the answer is YES.

    The optimized method comes from realising that if you have a $$$00$$$ sequence at most you can remove one of the zeroes. Therefore, you can't leave any $$$1$$$ before the last occurrence of $$$00$$$, and you can only remove all $$$1$$$'s that appear before if there isn't a $$$11$$$ sequence.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please tell the 56th query of 2nd test case in Problem C

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    it's n=4;c[] is [1,2,2,1] you can get it by letting your program simply output it i failed it here too

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I guess in problem C the formula should have been sumOdd+minOdd⋅(n−cntOdd+1)+ sumEven+minEven⋅(n−cntEven+1) instead of sumOdd+minOdd⋅(n−cntOdd) + sumEven+minEven⋅(n−cntEven). Isn't it?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    yeah, My formula comes similar to this. current_ans = odd_sum + odd_min*(n-odd_cnt) + even_sum + even_min*(n-even_cnt).

    You can refer to this video for detailed explanation:- Link to Explanation

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +9 Vote: I do not like it

    if you do it using your formula, minOdd and minEven would be counted twice since they are already being considered once in sumOdd and sumEven

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why is maximum of value of k=(x/g+d)/c = 2*10^7 in Problem C?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Let $$$x = 10^7$$$, $$$g = 1$$$, $$$d = 10^7$$$, $$$c = 1$$$. It is the bounding values for this task.

»
4 years ago, # |
  Vote: I like it +18 Vote: I do not like it

In F's solution code, d is not even calculated?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Whoops, turns out it works as is. Removed it, thanks.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

why in problem D the numbers A and B must be mutually simple?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    After you divide two numbers by their GCD, the resulting numbers are coprime. They don't share any common factor greater than 1.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I still do not understand, please explain in more detail, thanks)

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +15 Vote: I do not like it

        If you could be more precise about it maybe we can be more helpful, but right now I can only rephrase the editorial. We are considering two numbers $$$a$$$ and $$$b$$$ and their $$$GCD$$$. If $$$gcd(a,b) = g$$$ by definition $$$g$$$ is the GREATEST number that divides $$$a$$$ and $$$b$$$.

        So if we look at $$$A = \frac{a}{g}$$$ and $$$B = \frac{b}{g}$$$, there can't be any number $$$n > 1$$$ that divides both $$$A$$$ and $$$B$$$ because then $$$g*n$$$ would divide $$$a$$$ and $$$b$$$. Since $$$g*n > g$$$ it would have been false that $$$g$$$ is the $$$GCD$$$ of $$$a$$$ and $$$b$$$. This is the same as saying $$$gcd(A, B) = 1$$$, which also means they are coprime.

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Why in problem C, we can suppose to make exactly k−1 turns?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Finally, we should iterate over all k from 2 to n and find the minimum answer among all variants. It's easy to recalculate sums and minimums when we make transition form k to k+1.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Any explanation/solution for D(div 2) other than editorial?

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Please invest some time to make editorial good for everyone. Many newbies and pupils are not able to understand C.

  • »
    »
    4 years ago, # ^ |
    Rev. 3   Vote: I like it +11 Vote: I do not like it

    Take a look at this explanation.

    Let's try solving this problem: Given $$$c_1, c_2, c_3 \dots, c_k$$$, find $$$a_1, a_2, a_3, \dots, a_k \ \ (a_i > 0)$$$ such that $$$a_1 + a_2 + a_3 +\dots + a_k = n$$$ and the sum $$$c_1 \cdot a_1 + c_2\cdot a_2 + c_3\cdot a_3 + \dots + c_k\cdot a_k$$$ is minimum.

    Let's analyze the case when $$$k = 2$$$; we have $$$c_1, c_2$$$. Obviously $$$a_2 = n - a_1$$$, and the cost would be $$$c_1\cdot a_1 + c_2 \cdot (n - a_1) = c_2\cdot n + a_1\cdot (c_1 - c_2)$$$. If we consider $$$c_1 \ge c_2$$$, then in order to minimize $$$c_2\cdot n + a_1\cdot (c_1 - c_2)$$$, $$$a_1$$$ has to minimum. Also, since $$$a_i > 0$$$, $$$a_1 = 1$$$.

    Now, let's generalize that for arbirtrary $$$k \ge 2$$$. Let's build any assignment. Now, suppose there's a pair $$$a_i, a_j$$$ with $$$a_i + a_j = m$$$ ($$$m$$$ is just a constant), and without lossing generality, $$$c_i \ge c_j$$$; then from what we did above, it's optimal to choose $$$a_i = 1, a_j = m - 1$$$, this preserves $$$a_i + a_j = m$$$. We can do that as long as there're still two or more $$$a_i > 1$$$. This proves that for any $$$k \ge 2$$$, it's optimal to match the minimum $$$c_i$$$ with $$$a_i = n - k + 1$$$, and all the others with $$$a_i = 1$$$. Thus, summarizing, the cost would be $$$\left(\sum_{i = 1}^k c_i\right) + (n-k)\cdot \min{c_i, \ \forall i \in[1, k]}$$$

    Now, how to solve the real problem? Consider every prefix $$$k\ (k > 1)$$$ of the sequence $$$c_1, c_2, \dots, c_n$$$, and let's get the optimal solution with that prefix; we stay with minium solution among all prefixes. For a fixed $$$k$$$, the odd indexes are independent of the even indexes (we can consider odd indexes for UP movements, and even indexes for RIGHT movements), so for a fixed $$$k$$$, we have the subsequence of even indexes until k: $$$e_1, e_2, e_3, \dots, e_r$$$ and the subsequence of odd indexes $$$o_1, o_2, o_3, \dots, o_s$$$, and basically we want to solve the above problem for each of these two sequences.

    Now, since we go moving $$$k$$$ to the right, we need to keep track the sum of all $$$c_{2i}$$$ and the sum of all $$$c_{2i + 1}$$$, and also the minimum all $$$c_{2i}$$$ and the minimum of all $$$c_{2i + 1}$$$.

    These things can be updated easily when we go increasing $$$k$$$.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Oh thanks for the effort! but I got it already

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The editorial states that storing the min prime factor using sieve and calculating the ans in $$$\mathcal{O}\left(\sqrt{x} log x \right)$$$ is not fast enough , I did precisely that and it passed the test cases . $$$\newline$$$ Solution Link : https://mirror.codeforces.com/contest/1499/submission/110701050

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks for the editorial.. was waiting for it. I was asking for help for problem D from my friends, but no one was responding correctly , and that made me feel down... life is hard, when you are fart :(

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Resolved

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain in problem D, why the pairs generated of the form (Ag, Bg) would be unique and will not contain repetitive pairs? Can it not be possible that for 2 different g's, we get 2 different A's and B's such that A1g1 = A2g2 and B1g1 = B2g2 so that they are necessarily the same pair. I am having a hard time understanding this. Any help would be appreciated. Thank You.

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thank you for the editorial! The explanation for problem C is obvious.