### zscoder's blog

By zscoder, history, 8 years ago,

Here are the editorials for all the problems. Hope you enjoyed them and found them interesting!

Code
Code
Code (O(nkm^2))
Code (O(nkm))
Code
Code
• +100

| Write comment?
 » 8 years ago, # |   0 Auto comment: topic has been updated by zscoder (previous revision, new revision, compare).
 » 8 years ago, # | ← Rev. 3 →   -17 Nice editorial!
•  » » 8 years ago, # ^ |   0 It will appear after system tests.
 » 8 years ago, # |   +3 Hey , http://codeforces.com/contest/711/submission/20254708 why does this time out ?
•  » » 8 years ago, # ^ |   +10 Why use a "For" loop for calculating power of 2? Use Modular exponentiation by Squaring instead.
•  » » 8 years ago, # ^ | ← Rev. 4 →   +2 Although a log(N) time complexity for computing the product is not the cause for the time out, I strongly recommend you to learn the fast exponential function.I am still reading the code to see if there is a possible infinite loop.Edit:Try this case:52 3 1 3 4You failed to handle cycles properly.Edit: I think my English got murdered today.
•  » » » 8 years ago, # ^ |   0 Thanks , btw what is the output of that operation ?
•  » » » » 8 years ago, # ^ |   0 24 if I am not mistaken, my code just passed the system test so it should be correct.
 » 8 years ago, # |   +12 Problems were very nice: short and to the point! Editorial was also given so fast, and the queue also displayed the verdict on time without any delays. Everything was perfect and I solved 4 problems until I received "WA on 105" in C.
•  » » 8 years ago, # ^ |   +4 I got "WA on 92" in B, great feeling.
•  » » » 8 years ago, # ^ | ← Rev. 2 →   +2 I'm on 94
•  » » » 8 years ago, # ^ |   0 My friend on 95
•  » » » 8 years ago, # ^ |   0 Now i got "WA on 100" ;)
•  » » 8 years ago, # ^ |   0 Oh boy, I get that pain when you fail to implement an easy solution. Feels bad to also fail in C.
 » 8 years ago, # |   +29 The main blog was deleted?
•  » » 8 years ago, # ^ | ← Rev. 2 →   +2 Sorry it was my fault. I accidentally clicked "Save Draft" when I was trying to publish the winners and it went missing. It is back now.
 » 8 years ago, # | ← Rev. 2 →   0 For what test cases would these 2 solution for A and B not work? :(
•  » » 8 years ago, # ^ |   +5 For 2A, it's incorrect if there is "XX|OO" where this is the first available row.For 2B you didn't take care of n=1
 » 8 years ago, # | ← Rev. 2 →   -17 I failed on C because I declared the array as n*k*n instead of n*k*m..and problem b because I didnt verify the boolean in case the answer is not possible,,,Yes, these are the mistakes I made.. Today d saved me, otherwise my day could have been 100x worse...Nice problems. I was 120 before sys tests, 830 afterwards..Thats life mate...
•  » » 8 years ago, # ^ |   +3 Same story about problem C. Solution was correct, but final cycle to find minimum value from last colors was from 1 to n instead of m.
 » 8 years ago, # |   +3 Could someone please explain Problem D more clearly. Do we just find the connected components using kosaraju's method and then proceed?
•  » » 8 years ago, # ^ |   +17 First, consider the original oriented graph. Each node has exactly one edge going out. If we start at some point, we can follow the edges and obviously we will get looped (e.g. 1-2-3-4-5-3). For points inside loop, we will always stay inside loop. For other points, they will fall into some loop eventually.Conclusion: the graph consists of several disjoint cycles, and there are some "free" paths leading from "leaves" into some cycle. These paths do not intersect (only in cycle points).Now consider the unoriented graph, i.e. remove all directions. We can orient those "free" paths arbitrarily, independently: 2num_free_edges. And for each cycle, there are only two edge orientations that are bad for us — full clockwise cycle and full ccw cycle. Therefore for each cycle of length c we need to multiply the answer by 2c - 2.How to find cycles? We always have only one edge outgoing from any node (in the original directed graph). Let's go from each node and follow the edges until we meet an already visited node. We also keep track whether the node's cycle was already counted, and a "time" counter. Then when we get to the node second time, we check if its cycle was already counted and if not, we use the time difference to get the cycle length. Code vector vtime(N, -1); vector vsrc(N, -1); vector cycles; int free_edges = N; int t; FORN(v, N) { if (vsrc[v] != -1) continue; vsrc[v] = v; vtime[v] = t = 0; int u = v; while (1) { u = arr[u]; t++; if (vsrc[u] == -1) { vsrc[u] = v; vtime[u] = t; } else if (vsrc[u] == v) { cycles.push_back(t - vtime[u]); free_edges -= cycles.back(); break; } else break; } } 
•  » » 8 years ago, # ^ |   -6 Yes, finding strongly connected components with Tarjan or Kosaraju and the rest you can solve by fast exponentiation.
•  » » » 8 years ago, # ^ |   0 Thank you. :)
•  » » » » 8 years ago, # ^ | ← Rev. 2 →   +3 We don't need to find strongly connected components in this problem, since if we make the graph undirected, there will be only one simple cycle in each component, and we can find it by simply dfs and find the only back edge.
 » 8 years ago, # | ← Rev. 2 →   +1 Problem E. Why do we calculate complement probability as but not as (with k! in denominator) ?
•  » » 8 years ago, # ^ |   0 'cause we define the probability that no two ones have the same birth day as follow : k! *c[2^n][k] /((2^n)^k) now if u decompose c[2^n][k] in the numerator , u can find that we can delete the k! from both the numerator and denominator .this is true when k<=2^n
•  » » » 8 years ago, # ^ | ← Rev. 2 →   0 how from the problem statement follows that order matters? when I read it I thought, that order does not matter and therefore the probability you are speaking about is , not .
•  » » 8 years ago, # ^ |   0 I'm late but I'll try to explain. I think it's because the order in which we choose the birthdays matters, i.e. we assumed the i-th paranthesis in the numerator refers to the birthday of the i-th person.Example: let's have 3 days in a year (A, B, C) and 2 people. If we use the second formula you wrote, we'd have 3 cases for 'distinct birthdays': one person is born on A and the other on B, one is born on A and the other on C; and one is born on B and the other on C. If we use the author's formula we would have 6 cases for 'distinct birthdays'.
 » 8 years ago, # |   0 For 2E, why for test case 4 the output has to be "906300 906300"? Wouldn't "1 1" be the same?
•  » » 8 years ago, # ^ |   +3 Because you want original numbers to be coprime (not the answer after modulus) ==> If it would be possible, the process would be following: A) Find two numbers B) Make them coprime C) Do modulo operationHere, it can happen that two numbers can be coprime, but after modulo, the results are not:Little example: A=3,B=8,MOD=5A and B are coprime, but "A % MOD" and "B % MOD" (3 / 3) are not!Good Luck & Have Nice Day! :) ^_^
•  » » » 8 years ago, # ^ | ← Rev. 2 →   +5 But since k > MOD, A is guaranteed to be equal as B. What I don't get is why I must output (B%MOD)/(B%MOD) while 1/1 is not accepted . @_@UPD: Ahh I think I got it.
 » 8 years ago, # |   0 My codes were accepted during the system checking but now its showing skipped and there is no change in my rating.My all submissions of the contest are showing "skipped". Can somebody please help me regarding this issue?
•  » » 8 years ago, # ^ |   0 Probably your codes were very similar to some one else.
 » 8 years ago, # |   +1 I have a tendency of writing recursive solutions for DP problems. Will that be cause any problem in near future? Here is my accepted solution for DIV 2C (Complexity O(NKM2)). Can anyone help how to modify the same recursive solution so that complexity is reduced to O(NKM).
 » 8 years ago, # |   0 Can't find why is this WA?
 » 8 years ago, # | ← Rev. 2 →   0 dunnow why this results WA http://codeforces.com/contest/711/submission/20256542solved it ... thanks
 » 8 years ago, # |   0 Silly doubt, but really helpful. What happens if k>n in Problem C. How does DP gives answer for that case?
•  » » 8 years ago, # ^ | ← Rev. 3 →   0 supposing that ur dp parameters are : 1 : the index of the tree (ind)2: how many different groups u have till now (diff)3: and the previous color (prev)of course when u reach ind=n(if u start enumerating the trees from 0 ,then u processed all the trees) and diff< k return oo ,so at the end ,when u finish ur dp function , if the result >= oo ,then there is no way to get k different groups ,so print -1;
•  » » » 8 years ago, # ^ | ← Rev. 2 →   0 But, how to prove that when k>n, result will be INF according to the dp made in editorial?
•  » » » » 8 years ago, # ^ | ← Rev. 3 →   0 You just don't have enough trees to have k groups.
 » 8 years ago, # | ← Rev. 2 →   +3 In the editorial for E, these 2 lines are used to calculate the highest power of 2 which divides 1.2.3...k-1. int digits = __builtin_popcountll(k — 1);v2 = k — 1 — digits;.Can somebody please elaborate on this formula.
 » 8 years ago, # | ← Rev. 2 →   0 Never mind!
 » 8 years ago, # |   0 For problem E, I think it should be (k-1)
 » 8 years ago, # | ← Rev. 2 →   -6 For problem c, Why complexity nkm^2 can pass all the test as n=k=m=100? Won't it be TLE?During the contest, I considered the solution of nkm^2. But because I am afraid to be TLE, I didn't write it.sad...:(
•  » » 8 years ago, # ^ |   +9 Now you know, 108 simple operations can be completed in < 1 second on Codeforces.
•  » » » 8 years ago, # ^ |   +3 Oh maybe I am wrong, I always consider n2 for N = 103, n3 for N = 102, or for N = 105 for N = 2 × 105.Maybe my entire outlook should be changed...
 » 8 years ago, # |   +3 for problem E : can anyone explain the details of the last part of the author's code — calculating and reducing the numerator — thanks in advance
 » 8 years ago, # |   0 Can you give me a webite link to explain "Legendre's formula" in the editorial of Problem E? I know nothing about that:(
•  » » 8 years ago, # ^ |   0 Here you go : Link
 » 8 years ago, # |   0 Can someone explain the Div 2C in a top- down manner? I am not able to understand the editorial.Thanks!
•  » » 8 years ago, # ^ |   0 the function would look like this long long calc(int now,int k,int prev); now :the index of the current tree, k :the number of contiguous groups until now, prev : the color of the previous tree.now for every uncolored tree you should try each color then move to the next tree then minimize the result, if the the tree is colored just ignore it. but remember to increase k when you find two adjacent trees with different colors , when you reach the end just see if k equals the given number of beauty otherwise it is not a valid solution. here is my solution
 » 8 years ago, # |   0 I did not get this line in first problem if(bus[i][j*3] == 'O' && bus[i][j*3+1] == 'O') please explain.
•  » » 8 years ago, # ^ |   0 The cycle for j is from 0 to 1. So on the first iteration we have 3 * j and 3 * j + 1 equals to 0 and 1. That's indexes of the first pair seats. On the second we have 3 * j and 3 * j + 1 equals to 3 and 4. That's indexes of the second pair seats.
 » 8 years ago, # |   +3 I didnt understand how problem-C was done in O(nkm). Can anyone please help. In the code given in editorial what is the purpose of m1[][], m2[][] and idx[][].
•  » » 8 years ago, # ^ |   +6 m1- global minimum for i-th for pretty k, m2- the second minimum, idx-where we can find it!
•  » » » 8 years ago, # ^ | ← Rev. 2 →   0 Thanks, one more doubt, what does idx[][] = -2 mean? Does it mean that 2 minimum paths are possible so we can always pick minimum?
•  » » » » 8 years ago, # ^ |   0 Yes
 » 8 years ago, # |   0 My submission on B got wrong answer on test case 32. But I still don't understand what was wrong with my logic. I calculate D=max(all rowsum,all columnsum,diagonal1,diagonal2)-min(all rowsum,all columnsum,diagonal1,diagonal2) . Then I added D(if D>0) to empty cell and updated all sums. Then I recheck if(max(all rowsum,all columnsum,diagonal1,diagonal2)-min(all rowsum,all columnsum,diagonal1,diagonal2) == 0) then print D, else -1. I have also taken care of n==1 case. Here's my Submission. Can someone point out my mistake?
•  » » 8 years ago, # ^ |   +1 if(q==w) d1+=d; if(n-w-1==q) d2+=a; // <-- What is this? Why not d? 
•  » » » 8 years ago, # ^ |   0 Ohh! What a silly mistake! Thanks a lot for pointing it out.
 » 8 years ago, # |   0 Hey guys, I'm stuck at Problem D test 7 for 2 hours. I'don't know where did I get wrong.can somebody help please? I can't appreciate you more. Here's my code: 20279080
•  » » 8 years ago, # ^ |   0 Did you get your mistake ?
•  » » » 8 years ago, # ^ |   0 sadly no :(
•  » » 8 years ago, # ^ |   0 if i'm not too late.consider this case test52 3 4 3 1you output 8 but the answer is 16
 » 8 years ago, # |   0 how can i draw the 3 dimentional dp array in a piece of paper for problem C
•  » » 8 years ago, # ^ | ← Rev. 2 →   0 You don't need to think in 3 dimensions for this problem. When you're considering the i'th tree, all you need to know is the states from the previous tree i - 1. All you need to keep in your head is 2 planes. Each plane contains all the possible states for the corresponding tree. We only need to know how these planes relate to each other, namely, how can we compute i'th plane from i - 1 plane.I imagine planes made from pixels (like monitor). These planes are almost transparent, so that I can put one screen on top of the other and see the screen on the bottom through the screen on top. I know the values of the pixels of the bottom screen and I need to calculate the values of the pixels of the top screen. To traverse all the pixels of the screen on top we need 2 loops: for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) To compute the value of the single pixel of the top_screen[x][y] we need to consider all of the points of the bottom_screen[][]. So, each pixel in the bottom screen contributes it's value to the pixel top_screen[x][y]. To traverse all of the pixels of the bottom screen we also need 2 nested loops: for (int xx = 0; xx < width; xx++) for (int yy = 0; yy < height; yy++) When we combine these thoughts together, we get a general structure for the program: for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) for (int xx = 0; xx < width; xx++) for (int yy = 0; yy < height; yy++) top_screen[x][y] = contribution_of(bottom_screen[xx][yy]); 
 » 8 years ago, # |   0 I have a question regarding the C problem: could you please explain what does the "last colour" and "current colour" mean?
 » 8 years ago, # | ← Rev. 2 →   0 Problem E is very interesting.Is there an intuitive way to deduce the Legendre's Formula? I couldn't figure it out, I had to read a Wikipedia article :/
•  » » 8 years ago, # ^ |   +6 I thought like this: we have numbers 1,2,3,...,k-1 and we want to divide by 2 as many of them as possible and as many times as possible. First we count all even numbers, we divide them by 2: there are floor((k - 1) / 2) even numbers there. Then we count numbers divisible by 4. We have already divided them by 2, so we have to count them only once: floor((k - 1) / 4). Continuing, we get that the total product is 2t, with
 » 8 years ago, # |   0 Hello. In problem E 'One way to resolve this is to reduce it modulo MOD - 1, since 2MOD - 1 ≡ 1 modulo MOD by Fermat's Little Theorem.' I could not understand this. Why we take MOD — 1? If anyone could explain in details I would be gratified. Many thanks in advance.
•  » » 8 years ago, # ^ |   +1 Fermat's Little Theorem states that ap ≡ a (mod p) // (if p is prime)In other words ap - 1 ≡ 1 (mod p) // (if a is not divisible by p)So instead of calculating xy (mod p) we can calculate x(y mod(p - 1)) (mod p). We can do that because of the second equation. We just substract p-1 from the exponent as long as we can since multiplication by 1 doesn't change the result.
 » 8 years ago, # |   0 In the solution of problem 711C, why the elements of dp array are being initialised to INF, when every time they are being updated by dp[i][j][a-1]+c[i][a-1]. Please clear my doubt
•  » » 8 years ago, # ^ |   0 dp[][][] array is initialized to INF as we need to find minimum value, that is why you can see a min() function called each time when updating dp[][][] value
 » 8 years ago, # |   0 can the problem C be done using 2-D dp arraay ? if yes, how ?
•  » » 8 years ago, # ^ |   0 I think No, as the state requires 3 information to goto a unique next state. Anything less than 3 information per state will give us more than 1 state to goto, so we wont be able to decide which one to go for.
 » 8 years ago, # |   0 Can somebody help take a look and point out what's wrong with my code for problem E? Thanks in advance. =Dhttp://codeforces.com/contest/711/submission/20341261
•  » » 8 years ago, # ^ | ← Rev. 2 →   0 What do you do hereif(a<0) a+=b*(-a/b+1); ?Change it to thisif(a<0) a+=MOD; .If the real answer is you output p mod MOD and q mod MOD.
•  » » » 8 years ago, # ^ |   0 Oh boy, that was dumb of considering p mod q instead of (p-q) mod MOD. :PThanks for the help!
 » 8 years ago, # |   0 could somebody help me with question 711C i am getting WA on test on test 12. I have applied the logic correctly, I guess so, here is the code http://ideone.com/OW6sYs Thnks
•  » » 8 years ago, # ^ |   0 I am afraid that INT_MAX is not good enough to be used as INF in this question.
•  » » » 8 years ago, # ^ |   0 thank you very much!! I changed it into 10^15+5 and it worked. ACCEPTED. But even LONG_MAX doesn't work and why does LONG_MAX gives negative values. Please suggest.
•  » » » » 8 years ago, # ^ |   +1 LONG_MAX = 2^32-1 is approximately 2e9, so it is still not good enough to be considered as INF.I am not sure about what did you type in your code to make LONG_MAX becomes negative, but chances are you are not doing something like (LONG_MAX + FOO) so it overflows and becomes negative.
 » 8 years ago, # |   0 In problem D, what if a road is part of two cycles. Say city 1->2->3->4->1 and 2->3->5->2 does the description above satisfy that case? Say, for 1,2,3,4 when we are considering the fact that 2->3 is flipped and becomes 3->2 we must not count the same case for the 2,3,5 component. Because they are the same. I am asking this question because there is no mention, that a road cannot be part of two different cycles.
•  » » 4 years ago, # ^ |   0 As we have just one directed edge out from each node, each connected component(not scc) of the graph will have exactly one cycle. Thus an edge must belong to at max one cycle.
 » 8 years ago, # |   0 In problem E : doubts regarding MOD: 1. in denom plz explaing the MOD-1 logic 2. in calculating numerator it should be multiplied by pow(2, -vtmp) right ?? But why it is multiplied by pow(pow(2, vtmp), MOD — 2 ) ?? 3. How does builtin_pop_count help here ?? vtmp stands for power of 2 in gcd right ?
•  » » 8 years ago, # ^ |   0 I assume that you know neither Fermat's Little Theorem nor Euler's extension on it. I strongly recommend you to Google it, it's is fundamental for handling modulus equations. why power to the MOD-1? This is because a^(p-1) = 1 mod p (where p is a prime), that means that we can reduce a^x to a^(x%(p-1)) since a^n has a cycle of p-1. Why pow(pow(2,vtmp), MOD-2)? This is the inverse function, this is a pretty common thing so I would leave it for Google to explain the meaning of a^-1 MOD b. I took a different way to calculate the bits so I can't answer it... :/
•  » » » 8 years ago, # ^ |   0 I got the theorem Just cant figure out the reduction done in the power :(
•  » » » » 8 years ago, # ^ |   0 nvm Got it :)) Pen and paper work helped clear it out :))
 » 8 years ago, # |   0 zscoder can you tell me my mistake in test case 7 on Div2 D http://codeforces.com/contest/711/submission/20560676
 » 8 years ago, # |   0 Could anyone explain me the following: ll cnt = n; for(int i = 0; i < cycles.size(); i++) { cnt -= cycles[i]; // Why? ans = (ans*(dp[cycles[i]]-2+MOD))%MOD; // Why adding MOD and then %MOD? } ans = (ans*dp[cnt])%MOD; // What is the goal?
•  » » 8 years ago, # ^ |   +3 Adding MOD then %MOD is to make sure the answer is always positive.I'm doing what I explained in the editorial, for each cycle with length cycles[i] I multiply the answer by 2cycles[i] - 2 and for each remaining edge that aren't in any of the cycles I'm multiplying the answer by 2.
 » 7 years ago, # | ← Rev. 3 →   0 Hi everyone, could anyone explain how the answer of this test of problem D would be as explained in the editorial 71 22 33 12 44 33 55 1
 » 5 years ago, # |   0 I wonder if some changes can be made to make my recursive solution of div2C accepted. Also, can anyone help me in finding its time complexity ? ( watch our for exit conditions )
•  » » 4 years ago, # ^ |   0 Yeah...! This is possible, You just have to store the state values. And whenever you encounter an already solved state, you can get the value in O(1) time. (DYNAMIC PROGRAMMING)Your recursive solution with memoization
 » 5 years ago, # |   0 Can somebody explain the O(nkm) approach of div2C?
 » 4 years ago, # |   0 Can somebody explain the O(nkm) approach of div2C?
 » 3 years ago, # | ← Rev. 4 →   -10 In D, how can there be a path, shouldn't every component contain a cycle? Please help.Okay, sorry for the stupid way of asking what I wanted to ask But this is what I wanted to know. 1 . Why are we not converting the graph to undirected graph, because if there is a undirected cycle in any component, we can always fina directed cycle by changing the orientation of the edges. 2. How can there be a path? A path would mean there is so undirected cycle, hence a component can have only x-1 edges,where x is the number of vertices in the component. But each component should have equal number of vertices and edges, as when you sum up number of vertices and edges, in the end you should get n vertices and n edges.Someone please help me. zscoder 1.
•  » » 3 years ago, # ^ |   -10 Yea you are right.
 » 3 years ago, # | ← Rev. 3 →   -6 zscoder For Div 2B There is testcase 10 0 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 1 536870913 1 536870913 536870913 536870913 536870913 536870913 536870913 1 536870913 536870913 536870913 1 536870913 536870913 536870913 536870913 1 536870913 536870913 536870913 536870913 536870913 1 536870913 536870913 1 536870913 536870913 536870913 536870913 536870913 536870913 536870913 1 1 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 1 1 536870913 536870913 536870913 536870913 536870913 536870913 536870913 1 536870913 536870913 1 536870913 536870913 536870913 536870913 536870913 1 536870913 536870913 536870913 536870913 1 536870913 536870913 536870913 1 536870913 536870913 536870913 536870913 536870913 536870913 1 536870913 1 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 1 by observation output should be 1 but they are expecting -1 , Can you plz tell me why is it so ??
•  » » 3 years ago, # ^ |   +6 The main diagonals must also have the same sum as the rows and columns.
•  » » » 3 years ago, # ^ |   +4 zscoder is so nice, always replying to his contest editorialsrespect++
 » 4 months ago, # |   -8 242974284 For Problem D: Directed Roads Can someone please tell why I am getting TLE on TC 7, I think it is O(n) solution basic dfs similar to editorial.