### awoo's blog

By awoo, history, 7 weeks ago, translation,

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)

• +88

 » 7 weeks ago, # |   +56 In my opinion , E might be a little easier than D , so I recommend swapping D and E .
 » 7 weeks ago, # | ← Rev. 2 →   +16 Neat and Clean explanations especially on F. Thank you!
 » 7 weeks ago, # |   +12 This was a great contest, learned about FFT technique from problem F which seems quite useful
 » 7 weeks ago, # |   +6 great problems! great editorial! I learnt a lot.
 » 7 weeks ago, # | ← Rev. 3 →   -18 i wrote this dp solution for problem D Spoilerconst int mod = 998244353; void solve() { int n; cin >> n; vector arr(n); for(int i = 0; i < n; i++) { cin >> arr[i]; } map, ll> dp; for(int x: arr) { // mex == x - 1 dp[mp(x - 1, x + 1)] = dp[mp(x - 1, x + 1)] * 2 % mod; dp[mp(x - 1, x + 1)] = (dp[mp(x - 1, x + 1)] + dp[mp(x - 1, x)]) % mod; // mex == x + 1 dp[mp(x + 1, x + 2)] = dp[mp(x + 1, x + 2)] * 2 % mod; dp[mp(x + 1, x + 3)] = dp[mp(x + 1, x + 3)] * 2 % mod; // mex == x dp[mp(x + 1, x + 2)] = (dp[mp(x + 1, x + 2)] + dp[mp(x, x + 1)]) % mod; if (x == 0) { dp[mp(1, 2)] = (dp[mp(1, 2)] + 1) % mod; } else if (x == 1) { dp[mp(0, 2)] = (dp[mp(0, 2)] + 1) % mod; } } ll ans {}; for(auto p: dp) { ans = (ans + p.ss) % mod; } cout << ans << '\n'; } 
 » 7 weeks ago, # |   +471 Problem F can be solved in $O(n\log n)$ time: SpoilerConsider maintaining the product for those vertex such that $d_i \ge k$ and loop $k$ from $n$ to $1$. Each time we only need to multiply $(1+kx)^{\sharp_k}$, where $\sharp_i$ denotes the number of vertices such that $d_i = k$. The complexity can be bounded by $\sum_{k} \left( \sum_{i} [d_i \ge k] \right) \log n = \left( \sum_i d_i \right) \log n = O(n\log n)$There is also an much more complex algorithm that solve F in $O(n)$: SpoilerWe define linear functional $L: x^n \mapsto n!$, our goal is to evaluate $\displaystyle L\left(\prod_{i=1}^n (x-d_i)\right)$. Let $\sharp_k = \sharp i \mid d_i = k$, let $\displaystyle P(x)= \prod_{k < B} (x-k)^{\sharp_k}$ and $\displaystyle Q(x) = \prod_{k\ge B} (x-k)^{\sharp_k}$.Since $\displaystyle L(PQ) = \sum_{i\le n/B} L(x^iP) \cdot [x^i]Q$. Noticed that $\displaystyle \sum_{i=1}^n d_i = n-1$, evaluation of $Q$ has an upper bound $O\left(\frac{n\log^2 n}{B}\right)$. (This estimation might be improved)Since $P$ has an P-recurrence with order $B$, we can evaluate $L(P)$ in $\tilde O(\sqrt n \operatorname{poly} B)$ by the fast algorithm for P-recursive sequence. Then we show that $L(x^i P)$ has an recurrence respect to $i$.Noticed that $L(P) = P(0) + L(P')$ for all $P$, we have the recurrence of $L(x^iP)$ \begin{aligned} L (x^iP) &= L(ix^{i-1}P) + L(x^{i}P')\\ & = i L(x^{i-1}P) + L \left(x^i P \cdot \sum_{k •  » » 7 weeks ago, # ^ | +114 Umnik: "Idk how Elegia's mind works" •  » » » 7 weeks ago, # ^ | +40 Those interested in how um_nik's mind works can read how to solve it inO(N log N)$in simpler notations here •  » » 7 weeks ago, # ^ | +21 Captain EI txdy txdy txdy •  » » 7 weeks ago, # ^ | +29 by the fast algorithm for P-recursive sequence I've never heard of this before, could you link to a tutorial / paper or something? •  » » » 7 weeks ago, # ^ | +64 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. •  » » » » 7 weeks ago, # ^ | +10 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$? •  » » » » » 7 weeks ago, # ^ | +10 Since$\displaystyle \log P = \sum_{k
•  » » 7 weeks ago, # ^ |   -23 it would be very interesting to see the code for the $O(n)$ solution
 » 7 weeks ago, # |   +11 damn those are some very elegant solutions
 » 7 weeks ago, # |   0 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?
•  » » 7 weeks ago, # ^ |   0 Do you have an implementation? This sounds cool.
•  » » » 7 weeks ago, # ^ | ← Rev. 3 →   +1 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.
•  » » » » 7 weeks ago, # ^ | ← Rev. 2 →   +3 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})$.
•  » » » » » 7 weeks ago, # ^ |   0 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!
 » 7 weeks ago, # |   0
 » 7 weeks ago, # |   0 E is the easiest problem.
 » 7 weeks ago, # |   0 In problem D,Why the ans should become ans — 1?
•  » » 7 weeks ago, # ^ | ← Rev. 2 →   +9 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.
•  » » » 7 weeks ago, # ^ |   0 Thank you,I got it.
 » 7 weeks ago, # | ← Rev. 2 →   +1 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?
•  » » 7 weeks ago, # ^ | ← Rev. 3 →   +11 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)!$
•  » » » 6 weeks ago, # ^ |   0 Thank you, it was very helpful !
 » 7 weeks ago, # | ← Rev. 2 →   0 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 ^_^
 » 7 weeks ago, # |   +4 Can someone help me in E, 137783013 it keeps getting TLE but it's just a BFS.
•  » » 7 weeks ago, # ^ |   +8 Try replacing your endl by "\n"endl is slower. It also flushes the output
•  » » » 7 weeks ago, # ^ |   0 Thanks a lot! In a million years I wouldn't have thought of that
•  » » » 4 weeks ago, # ^ |   0 It's a very bad thing!!!How could one handle this at the contest time? Please explain anyone, who had handled such things.Is it a reason for a problem left unsolved?Those who have solved this problem by putting just "\n" instead of endl, please tell me, how did you figure out that at the contest time?
 » 7 weeks ago, # |   -12 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?
•  » » 7 weeks ago, # ^ |   -21 i think you should just solve problem A first..
•  » » 7 weeks ago, # ^ |   +1 Please read the second announcement. You got a wrong understanding of MEX-correct sequence.
•  » » 7 weeks ago, # ^ |   0 The sequence (x1, x2, x3, .., xi) is mex-correct if :- | xi — mex(x1, x2, x3, ..., xi) | <= 1 ; where xi is the last element in sequence. Hence for sequence (0,0,1,1,1,2,3) => mex(0,0,1,1,1,2,3) = 4 and xi=3 (last_element_in_sequence). Here, | xi — mex| = |3-4| = 1 and 1 <=1. Hence it is correct
 » 7 weeks ago, # | ← Rev. 2 →   0 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?)
 » 7 weeks ago, # |   0 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.
•  » » 7 weeks ago, # ^ | ← Rev. 2 →   0 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.
•  » » » 7 weeks ago, # ^ |   0 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.
•  » » » 7 weeks ago, # ^ |   0 thanks I understood:)
 » 7 weeks ago, # |   0 Thanks to div2, I became a pupil
 » 7 weeks ago, # |   0 Thank you for the contest!!
 » 7 weeks ago, # |   +5 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
•  » » 7 weeks ago, # ^ | ← Rev. 3 →   +8 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
 » 7 weeks ago, # |   0 In problem E (Crazy Robot) I am getting wrong answer over test case 8If anyone can help me outThanks in advance
•  » » 7 weeks ago, # ^ |   0 Your mistake is using stack qx; stack qy;. it should be stack 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.
•  » » » 5 weeks ago, # ^ |   0 Thanks bro
 » 7 weeks ago, # |   0 i should have joined the tournament
 » 7 weeks ago, # | ← Rev. 7 →   -11 [deleted]
•  » » 7 weeks ago, # ^ |   0 No, $1 \leq x_2 \leq 10^6$. Your input $1000000000000$ is too large.
•  » » » 7 weeks ago, # ^ |   0 thanks for explanation, i mistakenly read that as 10^16 .
 » 7 weeks ago, # | ← Rev. 2 →   0 An alternative solution for C without using binary search.Let $d_1, d_2, ... d_{n-1}$ be the differences $a_{i+1}-a_{i}$ sorted in non-descending order. Now it is easy to calculate the damage to the dragon as a function of $k$ if we observe that for $k = d_{i}$ it is equal to $\sum_1^{i} d_i + (n - i) \cdot k$, and for other values it is linear with the slope 1. https://codeforces.com/contest/1613/submission/137715371This solution would have an advantage if there were multiple queries for different values of $k$, since it would be $O(1)$ per query if we precalculate prefix sums.
 » 7 weeks ago, # |   0 Thanks for the great editorial for D!
 » 7 weeks ago, # |   0 Thanks for the problems and editorial.Can anyone provide some problems similar to problems D and E?
•  » » 7 weeks ago, # ^ |   0 Binary search tagged problems for D
 » 7 weeks ago, # |   0 Any ideas why my solution D timed out on test 10? Seems like I have done everything the right way.https://codeforces.com/contest/1613/submission/137705963
 » 7 weeks ago, # |   0 I can't get why are we printing "r + 1" answer in question C and not simply "r".
•  » » 6 weeks ago, # ^ |   0 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 :)
 » 7 weeks ago, # |   0 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
•  » » 7 weeks ago, # ^ |   0 Never mind It got resolved after passing multidimensional vectors by reference which helped me removing some unnecessary global containers.
 » 7 weeks ago, # |   0 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 <= 1So 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 3The 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
•  » » 7 weeks ago, # ^ |   0 [0 2 1] is not correctin 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;
•  » » » 7 weeks ago, # ^ |   0 Thanks, Wolowitz! I didn't notice it was inside a math absolute value, LOL. Shame on me~