When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

awoo's blog

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

1613A - Long Comparison

Idea: BledDest

Tutorial
Solution (awoo)

1613B - Absent Remainder

Idea: BledDest

Tutorial
Solution (Neon)

1613C - Poisoned Dagger

Idea: BledDest

Tutorial
Solution (Neon)

1613D - MEX Sequences

Idea: BledDest

Tutorial
Solution (Neon)

1613E - Crazy Robot

Idea: BledDest

Tutorial
Solution (awoo)

1613F - Tree Coloring

Idea: BledDest

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

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

In my opinion , E might be a little easier than D , so I recommend swapping D and E .

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

Neat and Clean explanations especially on F. Thank you!

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

This was a great contest, learned about FFT technique from problem F which seems quite useful

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

great problems! great editorial! I learnt a lot.

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

i wrote this dp solution for problem D

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

Problem F can be solved in $$$O(n\log n)$$$ time:

Spoiler

There is also an much more complex algorithm that solve F in $$$O(n)$$$:

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

    Umnik: "Idk how Elegia's mind works"

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

      Those interested in how um_nik's mind works can read how to solve it in $$$O(N log N)$$$ in simpler notations here

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

    Captain EI txdy txdy txdy

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

    by the fast algorithm for P-recursive sequence

    I've never heard of this before, could you link to a tutorial / paper or something?

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

      The brief idea is to evaluate $$$\mathbf M(x+1)\mathbf M(x+2)\cdots \mathbf M(x+\sqrt n)$$$ for $$$x=\sqrt n, 2\sqrt n, \dots, \sqrt n \cdot \sqrt n$$$ where $$$\mathbf M(x)$$$ is a matrix with polynomial entries. Min-25 gives an algorithm that achieves the smallest log factor currently. Though his original blog was not available for some reason, there is a detailed description of this algorithm in the editorial of CF Round #700 div1. E.

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

        I read that editorial, but unfortunately don't see how it is connected to this problem.

        Oh, also, what's the P-recursive equation for $$$P$$$?

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

          Since $$$\displaystyle \log P = \sum_{k<B} \sharp_k \log (x-k)$$$, we take derivation on both sides, then we obtain

          $$$ P' \cdot \prod_{k<B} (x-k) = P\cdot \sum_{k<B} \sharp_k \prod_{j<B, j\neq k} (x-j) $$$

          By extracting the coefficients on both sides you obtained a P-recursive equation of $$$[x^i]P$$$ immediately. Consider $$$f_i$$$ is P-recursive, then $$$g_i = i! \cdot f_i$$$ is also P-recursive (You can obtain the equation by simply replacing $$$f_i$$$ with $$$g_i/i!$$$ and then multiplying $$$i!$$$ on both sides of the equation). So its sum can be evaluated through the algorithm.

          There are more tricky ways to deal with the recurrence to reduce the $$$\operatorname{poly} B$$$ factor, but I omitted the details here since it's not the bottleneck of the entire problem :)

  • »
    »
    2 years ago, # ^ |
      Vote: I like it -23 Vote: I do not like it

    it would be very interesting to see the code for the $$$O(n)$$$ solution

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

damn those are some very elegant solutions

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

I don't understand why the runtime is $$$O(n \log^2 n)$$$ in problem F.

We can write the divide and conquer runtime as $$$T(n) = 2 * T(n / 2) + O(n \log n)$$$. This is case 3 in the master theorem of divide and conquer which yields $$$T(n) = \theta(n \log n)$$$, no?

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

    Do you have an implementation? This sounds cool.

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

      I didn't solve this problem, I'm simply reading the solution from the editorial and wondering why their implementation is $$$O(n \log^2 n)$$$ instead of $$$O(n \log n)$$$.

      I think my mistake is probably that the 3rd case of the master theorem doesn't apply here, and the master theorem cannot be used. To use the master theorem case 3 there must exist a constant $$$\epsilon > 0$$$ such that $$$n \log n = \theta(n^{1 + \epsilon})$$$, and it might not exist.

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

        Because the $$$f(n) = \Theta(n \log(n)) = \Theta(n^{c_{crit}} log(n)^1)$$$, this recurrence actually falls in case 2 of the master theorem (if you hold on the order of cases in wikipedia). This solves to a running time of

        $$$T(n) = \Theta(n \log(n)^{1+1})$$$

        .

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

          I was referring to the cases from CLRS (page 94) and the case 2 is more specific there, and it doesn't fall into it.

          Indeed if I look at Wikipedia the case 2 is more generic and it does apply in this case, thanks!

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

In problem D,Why the ans should become ans — 1?

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

    We initialize $$$dp1_{0,0}$$$ with a $$$1$$$, which represents an empty subsequence (which has MEX equal to $$$0$$$). At the end we accumulate the answers and count it in. Since we are asked to count only non-empty subsequences, we should subtract it.

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

I cant understand why the number of colorings that meet $$$k$$$ choosen violations is $$$(n-k)!$$$ in problem F, can someone explain it in more details please?

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

    I can give an intuitive idea behind that. For every violated constraint, a node of the tree is already defined by its parent (it's just their parent's number minus one). So basically out of the $$$ n $$$ nodes, we can focus on the $$$ n - k $$$ nodes which aren't tied to their parents and think about the relative order between them.

    Let's build a sequence of these untied nodes in which the first node has the highest value $$$ n $$$, the second the next highest value and so on.

    Of course you can choose any of them to have the highest value $$$ n $$$, then choose any other node to have the next highest value (which depends on the previous node as it might have induced the value $$$ n - 1 $$$ to one of its children and maybe more values to its descendants) and so on. The thing is you can choose any of the untied nodes to go next in this sequence. Therefore you can have any possible relative order between them, so the number of colorings is $$$ (n - k)! $$$

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

I feel like F should have a "divide and conquer" tag based on what's written in the editorial, is it really so? UPD: Thanks for adding the tag, hope it will help people in their practice later ^_^

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

Can someone help me in E, 137783013 it keeps getting TLE but it's just a BFS.

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

    Try replacing your endl by "\n"

    endl is slower. It also flushes the output

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

      Thanks a lot! In a million years I wouldn't have thought of that

»
2 years ago, # |
  Vote: I like it -12 Vote: I do not like it

One thing I can't understand in the tutorial for D: The tutorial says that [0,0,1,1,1,2,3,3] is a MEX-correct sequence. However, this violates the definition of MEX-correctness. For example, let i = 7. Now MEX(0,0,1,1,1,2,3) = 4. |0 — 4| = 4, |1 — 4| = 3, and |2 — 4| = 2. These are all violations of MEX-correctness as described in the question. May you elaborate on how the above sequence is MEX-correct?

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

    Please read the second announcement. You got a wrong understanding of MEX-correct sequence.

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

https://atcoder.jp/contests/abc213/tasks/abc213_h The above problem from atcoder have the solution where one has to apply DNC FFT stuff. While The Problem F seems like a polynomial multiplication where one prefers to multiply shorter polynomial first. We can do it along the similar line of merge-sort or simply use priority queue and multiply shorter polynomials each time. 137796885. How is it DNC FFT? (Or what is DNC FFT exactly?)

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

I couldn't understand the editorial this much but can I solve E like this?? we implement a bfs or dfs from lab and just change to + the cells of grid which we can control and we can reach them with a path from the lab that we can control?? we can control means that the cell has two paths or less.

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

    You can start a BFS from the lab. Mark lab as + for generalization sake. During bfs, do the following: 1. If a cell you are looking at has at most one neighbour cell which is not blocked and not with plus, then mark that cell as +. 2. If a cell is marked + now, push all of its neighbour cells to the queue unless the neighbour cell was visited from this particular cell. In other words, keep visited not just for some cell, but for a parent-child relationship. If cell a already pushed cell b to a queue, then don't let a to push b again, but some other cell c can still push b. 3. Go to the next queue element. Pretty sure there is no way to solve this problem using DFS.

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

      There is a dfs solution, check my submission: 137662230. Consider the lab is the root, we follow the four "hallways". At any square we find it connects to more than 1 '.', stop there.

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

      thanks I understood:)

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

Thanks to div2, I became a pupil

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

Can anyone please tell me what's wrong in my submission for E?

I am getting TLE on : 137850453 Also this is the exact same solution which got passed in about 200 ms : 137675491

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

    Dont use std::endl, use "\n" instead.

    Flushing std::cout is very slow, especially when you need to do it 1000000 times.

    see this: 137852168 and 137853185

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

In problem E (Crazy Robot) I am getting wrong answer over test case 8

My solution:- https://codeforces.com/contest/1613/submission/137885700

If anyone can help me out

Thanks in advance

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

    AC: https://codeforces.com/contest/1613/submission/137914304

    Your mistake is using stack<char> qx; stack<char> qy;. it should be stack<int> to not overflow as a char overflows quickly.

    As a tip, this is how I debugged your code. Use assert() and insert it at different points of your code to see where things went wrong. I assert()ed that your values were valid just before pushing it onto the stack, as well as assert()ing the values at the start of the function. It was okay before pushing it onto the stack, but not when you used it in the function. So I can deduce that something must have went wrong when you passed it into the function, and that's how I found out.

»
2 years ago, # |
Rev. 7   Vote: I like it -11 Vote: I do not like it

[deleted]

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

    No, $$$1 \leq x_2 \leq 10^6 $$$. Your input $$$1000000000000$$$ is too large.

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

Thanks for the great editorial for D!

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

I can't get why are we printing "r + 1" answer in question C and not simply "r".

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

    Hey, this has simply got to do with the way the binary search was written. From the code, you can see (thanks to the if else) that the variable r will be the greatest value for which the sum will be < h. You can understand this as: if the sum >= h, then r = r-1. So r will be lowered until it reaches the optimal solution-1. There is actually another way to write this binary search. In my solution, I wanted "lo" to be the smallest value such that the sum would be >= h. Link to my solution: https://codeforces.com/contest/1613/submission/138581286 I hope this is clearer to you now! Do not hesitate to reply if it is not clear enough :)

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

Can anyone please help me , Why am I getting memory limit exceeded on 8th test case in problem E ? https://codeforces.com/contest/1613/submission/138290812

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

    Never mind It got resolved after passing multidimensional vectors by reference which helped me removing some unnecessary global containers.

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

In 1613D — MEX Sequences, for the 1st test case, why [0,2,1] is not in the answer? MEX([0,2,1]) = 3 0 — 3 = -3 <= 1 2 — 3 = -1 <= 1 1 — 3 = -2 <= 1

So the correct answer should include: [0], [1], [0,1], [0,2], [0,2,1]

Thanks for any hint. If I understand it correctly, in the last test case:

0 1 2 3

The answer should include:

[0], [1], [0,1], [0,2], [0,1,2], [0,1,3], [0,1,2,3]

Please correct me if I am wrong.

Thanks heaps :D

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

    [0 2 1] is not correct

    in ith step we get mex from (x1, x2.... xi)

    i = 0; we have (0) now mex is 1. so |0-1| = 1 <= 1.. right. i = 1; we have (0, 2). now mex is 1. so |2 — 1| = 1 <= 1.. right. i = 2; we have (0, 2, 1). now mex is 3. so |1 — 3| = 2 > 1... wrong;

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

Please add 1000 more if else cases to problem D and make it problem A

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

    Update: Make it Problem A of Div4.Only Edu forces can come up with screwed up editorials.

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

For Testcase 1

3 0 2 1

why [2,1] is not included in answer ? Mex(2,1) = 0 abs(1-0)<=1

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

One way to think about this problem is in game theory terms.

Imagine a following game. Two players alternate moves. The first players chooses a direction. The second player chooses a if both players play optimally (as in the first player tries to win, the second player tries to draw)?

Does it sound easier? Well, it sure does if you ever dealt with solving games on arbitrary graphs. You . If a direction is not chosen (denote it with −1 ), it's the first player's move. Otherwise, it's the second player's move.

You can even implement it as is. Or you can adjust a part of this algorithm for this particular problem.

Initially, all the states are drawing, only the state (lab,−1) uch a direction that all neighbouring free cells except in this direction are winning states. Rephrase it. The sta is winning if can skim through this article if that's unfamiliar to you. The state of the game is a pair (cell,direction) is winning. What we different direction and moves a robot there. The game ends when the robot reaches the lab, and the first player wins. Otherwise, it's a draw. What's the outcome of the game basically need is a way to determine if a state is winning or not. From game theory, we can tell that the state Promote that one step further. The state is winning if there exists there's a transition from it to a losing state. The state is losing if all the transitions from it lead to winning states. So (cell,−1) is winning if any of (cell,direction≠−1) are losing.

te is winning if it has at least one winning state neighbour and no more than one non-winning state neighbour.traversal of a grid, this can be done with a Dcells. If some state becomes marked as winning, decrease the value for each of its neighbours by 1 . If some state's value reaches 0

ghbouring states for each cell. Initially, it's the number of neighbouring free does is basically a or 1 after this operation, mark it as winning.

Since what this Let's store the number of non-winning neiFS/BFS, starting from the lab.

Overall complexity: O(nm) per testcase.

Ipromoted this one step further and rephased it for better understading

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

D is the most felt hard dp problem I have ever solved till today