### BledDest's blog

By BledDest, history, 3 years ago, translation,

• +88

 » 3 years ago, # | ← Rev. 2 →   +11 We can solve problem E in using two pointers: find the smallest range where product of numbers inside this range is divisible by k, then res = res + (n - end) * (start - previousStart) 28156236
•  » » 3 years ago, # ^ |   +3 It's actually. For example test n = 1e5, k = 1 << 29, and a[i] = 2. You will iterate over each subrange of length 29.
•  » » » 3 years ago, # ^ |   0 Oh, that's right. Thanks :)
 » 3 years ago, # |   +86 for problem G we can build a flow network with only O(n) edges, so normal minimum cost max flow would work: So we'll have 4 vertices for each array position, 2 of them are connected by an edge with cost -1, if this edge is part of the answer, then we would use this array position for one of the sequences, but we'll need to have a path from ai to every aj such that i < j and abs(ai — aj) = 1 or abs(ai — aj) % 7 == 0, for every array position i have an additional vertex which has a path to all those j > i such that ai = aj, we only need to connect this vertex to the first j which has the attributes mentioned, so we would only need to connect ai to the first j's additional vertex that is ai — 1, and the first j's additional vertex that is ai + 1 to have path to all such j's that aj = ai — 1 or aj = ai + 1, a similiar thing can be done for the modulo 7 part, with another additional vertex for every vertex (it can also be reduced to 7 overall, because there are only 7 modulos), you can see my code for better understanding
•  » » 3 years ago, # ^ |   +10 We can even simplify it to 3 vertices for each array position, so we'll have 3 * N vertices and 9 * N edges (in a worst case). Also, we can use dynamic programming approach to find initial potentials(like described in the editorial). So total complexity will be O(f*n*log(n)), where f = 4 => we have O(n*log(n)) solution,Link to submission: http://codeforces.com/contest/818/submission/28193043
 » 3 years ago, # | ← Rev. 3 →   +30 In problem G, am I mistaken by saying that almost everybody who solved it used a compressed graph with O(N) edges? That's what I understood from most of the sources: that for each i they add just about 3 edges backwards to the last j with +-1 different value or the same residuum modulo 7. Is that a correct approach? I haven't thought of some counter example but I cannot prove the correctness of this approach either...So could someone shade some light on this issue?PS: Ok, it seems Reyna said about the same thing while I was typing the comment, so nevermind
 » 3 years ago, # |   +23 For E, I built a sparse table of products of segments modulo k. It seemed simpler than fiddling with vectors of prime powers.
 » 3 years ago, # |   +31 In E i used two pointers and queue, which can return (product of numbers inside) % k, similar to queue for finding min/max. Solution O(n) 28158111
•  » » 6 months ago, # ^ |   0 Can you elaborate this technique ?
•  » » » 6 months ago, # ^ |   0 Here you can see the basics, it's easy to change for the product
 » 3 years ago, # | ← Rev. 2 →   0 I solved E with factorisation and then binary search (fix right border, binary search for left border). I am getting Runtime error on case 16 when n = 100000 and k = 1000000000. I tried some smaller n and k = 1000000000 and it works oK. Any ideas ?http://codeforces.com/contest/818/submission/28159818
•  » » 3 years ago, # ^ |   +5 there's extra 0 hereif( k < 1000010 && is[k] == true ) kd.pb( k );
•  » » » 3 years ago, # ^ |   +5 Unfortunatelly, that didnt fix :( Still, thank you for trying to help.http://codeforces.com/contest/818/submission/28161706
 » 3 years ago, # | ← Rev. 3 →   +5 If I'm not mistaken, my solution to F is O(1) : Solution I guess it's technically O(Q).
•  » » 3 years ago, # ^ |   +5 Math.sqrt works in O(1) in java?
•  » » » 3 years ago, # ^ |   0 Yup :)
•  » » 3 years ago, # ^ |   0 It is same as the editorial, u just did the job of ternary search by hand.
•  » » » 3 years ago, # ^ |   0 I realize that, but when I actually implemented the ternary search, I got TLE so I resorted to this.
 » 3 years ago, # |   +3 For Problem C,I have something puzzled.There is one sentence in problem,"Sofa A is standing to the top of sofa B if there exist two such cells a and b that ya < yb".Why ya < yb ?Should it be ya > yb?
•  » » 3 years ago, # ^ |   +3 Authors assume that y axis runs from top to bottom.
•  » » » 3 years ago, # ^ |   0 3ks!
•  » » » 3 years ago, # ^ |   0 hello! I'm confused with the statement" Also some sofa A can be both to the top of another sofa B and to the bottom of it"
•  » » 3 years ago, # ^ |   0 Agree, it is quite hard to imagine examples in this way.
 » 3 years ago, # |   +5 In Problem C Editorial , shouldn't we decrement the result by 1 when x1i!=x2i instead of when x1i=x2i
•  » » 3 years ago, # ^ |   0 Yes, I think it should be either x1i ≠ x2i or y1i = y2i.
•  » » 3 years ago, # ^ |   0 Yup, you are right. Fixed, it will take some time to update.
 » 3 years ago, # |   +1 For problem D, if cntA(n) = cntB(n) = 0 then which case should it be classified as? From the problem statement it appears that both Alice and Bob would win, but that sounds like a draw to me.
•  » » 3 years ago, # ^ |   0 I dont think its a valid case as n >= 1 so either one of them will be greater than 0
•  » » 3 years ago, # ^ |   0 Alice would not win. Alice's condition includes strictly greater sign, while Bob's has greater or equal. 8shubham, it is valid case though.
 » 3 years ago, # |   +1 In Problem E I built a segment tree for modulo K so I can check if a segment [L..R] is divisble by KIt looked much easier that factorizing numbers and suchMy Code
•  » » 3 years ago, # ^ |   +1 I did the same bro!! that approach is much easier..
 » 3 years ago, # | ← Rev. 4 →   +8 Could somebody take a look into my solution for E? I can't understand why I got WA :( (Counting prime divisors in k first, then moving R until we have not enough divisors from k in current segment). Thanks in advance.http://codeforces.com/contest/818/submission/28168045EDIT: Oops, my fault.. I took sqrt(10^9) = 10^3 somehow..
 » 3 years ago, # | ← Rev. 4 →   0 Why do we have edges in connected component? Isn't it just ?
•  » » 3 years ago, # ^ |   +3 Because we have to make the graph satisfy the property that at least half of the edges are bridges. Though we can connect at most k(k-1)/2 edges, we can add at most n-k edges (because we only have n-k bridges)
•  » » » 3 years ago, # ^ |   0 I see, thank you.
 » 3 years ago, # |   0 So,can I ask for the hack to problem A?
 » 3 years ago, # |   0 and fill the unvisited children with remaining values to form the permutation.It doesn't make a difference in which order we place them?
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Nope. We don't care about the order as we will never take these values.
 » 3 years ago, # |   +3 For problem F why is building the graph as suggested optimal? How to prove it?
 » 3 years ago, # |   0 "Firstly you need to learn to factorize any number in no more than O(logn)." How can this be done ?
•  » » 3 years ago, # ^ | ← Rev. 2 →   +3 In general case you can run sieve of Eratosthenes in to calculate the smallest prime divisor of each number up to N. Then factorization will take .Here N is up to 109, so we decided not to take all the primes which is present in number but only those that are also present in k. And this is described in editorial.
•  » » » 3 years ago, # ^ |   0 So we do need a pre-computation using Sieve then. That is what confused me initially since that was not stated.
•  » » » » 3 years ago, # ^ |   0 We don't. If the numbers were up to 105, for example, we could use it and don't even bother with all the useful primes. Please, reread the editorial. I believe, that everything needed is already stated.
•  » » » » » 3 years ago, # ^ |   0 Yeah I got it. Thanks !
 » 3 years ago, # | ← Rev. 2 →   0 I have a question for Problem F: since k * (k-1) / 2 is increasing and n-k is decreasing, wouldn't the maximal value of the function be at the index where k * (k-1) / 2 becomes greater than n-k? If that is true, could we just binary search to find the point where k * (k-1) / 2 > n-k, and get our answer from there? Submission: 28155258 (Sorry not so familiar with ternary search so just asking)
•  » » 3 years ago, # ^ |   0 Yes that's correct.f(k) is increasing when k ≤ k0 (asserted in the tutorial), because grows faster than (or as fast as, right at the peak) n - k decreases. Therefore k0 is the peak and can be found with binary search.
 » 3 years ago, # | ← Rev. 7 →   0 There is another (shorter and simpler than other solutions I saw) solution for E (it looks like O(Nlog(MAXN)), but without binary search or something like this): We can start iterating through the array and stop when the product of prefix mod k is 0. Fix last index as curEnd. If we have curEnd == n-1 and mod is not 0, stop iterating and print answer, because there are no more subarrays that are divisible by k. Then go left from curEnd until the product of elements mod k is 0. Now we have len of subarray that is divisible by k. We can add value (curEnd - len + 2 - start) * (n - curEnd) to the answer. In other words, we add all possible subarrays that contain our subarray (that is, array [curEnd-len+1, curEnd]). Then update start (it's 0 at the start of algorithm) to curEnd - len + 2 (start is the first index of subarray that we're gonna check on the next iteration, next value of start will be the second element of just found 'good' array, so we won't count it twice or more). Then repeat the same algorithm, but starting with the new start. So, why this solution is O(Nlog(MAXN))? The worst case is when k = 1<<29 and the array consists only of 2's (i.e. [2,2,2,2,2...,2]). In this case start will be 0 at the first iteration, 1 at the second iteration and so on, but on every iteration there will be len=29 (this is log(MAXN=1e9)).Link: 28180910
 » 3 years ago, # |   0 problem A Please someone explain upon problem a
 » 3 years ago, # | ← Rev. 2 →   0 Are the test cases weak for the 4th question?
 » 3 years ago, # |   0 Does anyone know the proof for why in problem F the optimal solution will be achieved by having one large component and simply connecting the remaining vertices with bridges to it? If it's rudimentary, then let me know please so that I can think about it for some more.
 » 3 years ago, # | ← Rev. 2 →   0 In C, I'm confused with the statement" Also some sofa A can be both to the top of another sofa B and to the bottom of it"
 » 3 years ago, # | ← Rev. 2 →   0 Problem D statement is so difficult to understand
 » 7 weeks ago, # | ← Rev. 3 →   0 For 818F - Level Generation, there can be an O(1) solution.Here is a mathematical solution that does not require the ternary search. It only uses two values of k. Let, x be the variable k from the tutorial.x = sqrt(2*n-1); n_minus_x = n - x;ans = min(x*(x-1)/2, n_minus_x) + n_minus_x;Another try x = sqrt(2*n-1) + 1;n_minus_x = n - x;ans2 = min(x*(x-1)/2, n_minus_x) + n_minus_x;ans = max(ans, ans2);Note: The min() is used so that the non-bridge edge count does not exceed bridge count. sqrt(2n-1) is derived from a formula similar to the one mentioned in the tutorial Code: 89187360 Hope it helps someone.Edit:Derivation of the formula:x*(x-1)/2 <= (n-x)-> x <= sqrt(2n-1)