### majk's blog

By majk, history, 11 months ago, ,
Code (by arsijo)
Code
Code
Code
Code
Code
Code (by winger)
Code

• +154

 » 11 months ago, # | ← Rev. 2 →   +9 1091A Tutorial is not available
•  » » 11 months ago, # ^ |   0 not
•  » » » 11 months ago, # ^ |   0 Dude it's 9 days ago, how could you not notice that?
 » 11 months ago, # |   +27 It was fast :D
 » 11 months ago, # |   +42 I haven't seen such fast editorial! Cool contest, I enjoyed it!
 » 11 months ago, # |   +2 Happy New Year!
 » 11 months ago, # |   +76 Problem D: How do you prove that, there are no pair of indices (i, j) such that sum of [i, j] is n(n + 1) / 2 and [i, j] is not a permutation of 1, 2, ..., N ?
•  » » 11 months ago, # ^ |   -8 Hi, when N = 5 there is a subsequence 1, 5, [4, 3, 2, 2, 1, 3], 4, 5 and there is subsequence of length 6 and sum 15, isn't it?
•  » » » 11 months ago, # ^ |   +8 The length of that subsequence should be equal to N
•  » » » » 11 months ago, # ^ |   +17 Thanks, I completely overlooked that.
•  » » 11 months ago, # ^ |   0 If (i,j) is just one of n! permutations it is obvious permutation. Lets call (i,j) substring. It has k characters as suffix of some permutation and n-k characters as prefix of NEXT permutation. This k first characters of our substring: a) If they are not in decreasing order we then the next permutation will only differ on last (or less)k characters which implies the fact that n-k first characters of next permutation are same as in this permutation before. So, indeed if this k characters are not in decreasing order we obtain substring (i,j) which is permutation of numbers 1,2,3..n. b) Second option is that first k characters of our substring are the suffix of before permutation and they are all in decreasing order. Then, we cannot actually obtain substring starting with this k characters. Why? This is about the thing that last k characters will differ in before and next permutation by exactly one letter, which implies that their prefixes of length n-k will differ exactly by one letter too. I know how to prove it when k is the longest decreasing suffix. Then it is pretty clear. In other case(k is decreasing suffix but not longest) i think that something similar is true. The proof when k is the longest decreasing suffix comes from algorithm for generating all permutations in lexicographical order. Im sorry for my poor english. Let me know if something is unclear or if im wrong somewhere.
•  » » » 11 months ago, # ^ | ← Rev. 3 →   0 can u please explain this natofp?for suppose 1,2,3,4,8,6,7 is a permutation and its next permutation will be 1,2,3,6,4,7,8. here if we consider the last four elements from first permutation and the first three elements from the next permutation it will form sum n(n+1)/2 but there it was said that we cannot form a sum if the suffix is in decreasing orderThanks in advance
•  » » 11 months ago, # ^ | ← Rev. 2 →   +5 I think the following is pretty unreasonable to come up with in contest...We will show inductively that for any consecutive permutations σ, σ + 1,  if the sum of the first k elements are the same, the first k elements must be permutations of each other. Let σ = (a1, ..., an), σ + 1 = (b1, ..., bn). Let k be the length of the longest common prefix of σ, σ + 1,  and let m < n be maximal such that and for now assume k > 0. The key idea is that (ak + 1, ..., an), (bk + 1, ..., b2n) are consecutive permutations. Thus if k < m,  we have ak + 1 + ... + am = bk + 1 + ... + bm which by the inductive hypothesis implies that ak + 1, ..., am are a permutation of bk + 1, ..., bm,  contradicting the maximality of k. We now take care of k = 0. Clearly σ = (a1, a2 > a3 > ... > an) and σ + 1 = (a1 + 1, b2 < b3 < ... < bn). Then it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. For length 2,  equality only holds if the biggest number in (1, ..., n) is one more than the smallest, i.e. n = 2 which is checked by hand.With his lemma, we can show that if a contiguous interval of length n has sum n(n + 1) / 2,  it must be a permutation of 1, ..., n. Indeed, let this contiguous interval be lodged between two consecutive permutations (c1, ..., cn), (cn + 1, ..., c2n). Then which by the lemma implies that (cn + 1, cn + 2, ..., cn + k) is a permutation of (c1, ..., ck). Thus as permutations, as desired.
•  » » » 11 months ago, # ^ |   0 There is few flaws in this proof. Thus if k < m,  we have a_(k + 1) + ... + a_m = b_(k + 1) + ... + b_m which by the inductive hypothesis implies that a_(k + 1), ..., a_m, are a permutation of b_(k + 1), ..., b_m. This induction implies that in inductive hypothesis, (a1 , ... ,  an) and (b1, ... ,  bn) don't need to form a permutation of consecutive numbers. We now take care of k = 0. Clearly σ = (a_1, a_2 > a_3 > ... > a_n) and σ + 1 = (a_1 + 1, b_2 < b_3 < ... < b_n). Then in this statement, σ + 1  might not begin with a1 + 1. It can be some other numbers in (a2,  a3 , ... , an) which is greater than a1.Besides, I don't think it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. Maybe it needs some further proof. Sorry for my poor intelligence.
•  » » » » 11 months ago, # ^ |   0 I'm not sure that I understand what you mean by the first point. What I'm using is that if two consecutive permutations have a common prefix of positive length, the remaining parts of the permutations are consecutive permutations of each other. For example 21354 -> 21435.I didn't explain the k = 0 case in much detail. The fact that σ + 1 must start with a1 + 1 comes from the fact that σ, σ + 1 are consecutive permutations and we need the first element in the permutation to swap out. For your last point, just see that the relative ordering of the ai and bi ensures the inequality... the details are a bit messy so I'll spare you, but I think it's clear (try 35421 vs 41235, for example to see what happens).
•  » » » 11 months ago, # ^ |   0 Brilliant ideas!!One question here, why you say contradicting the maximality of k? By the hypothesis of the induction, they are permutations of each other, however, this does not imply that ak + 1 = bk + 1 and so doesn't contradict.Additionally, I think m doesn't have to be the maximal, it can be any value greater than longest common prefix.
 » 11 months ago, # |   +3 Can someone explain how to check if the answer is on the left or the right?
•  » » 11 months ago, # ^ | ← Rev. 4 →   +11 UPD: I'm explaining the solution of Problem E...In fact, the Theorem we used can be changed to:For each . So we only need to consider these two situations: point n+1 is in the set or not.
 » 11 months ago, # |   +53 When you didn't realize the link in E was useful, so you had to come up with your own graph realization algorithm :(
•  » » 11 months ago, # ^ |   +30 Problem E is really not so friendly...
 » 11 months ago, # |   +15 OMG! SO FAST!Wish every coder : Happy New Year!
 » 11 months ago, # |   +1 that was so fast , thank you for a great contest
 » 11 months ago, # |   +14 Bitset works much better than FFT. https://codeforces.com/contest/1091/submission/47761694
 » 11 months ago, # |   +1 That was fast insert Pikachu(o_o) meme
 » 11 months ago, # |   +8 Here's a tutorial for similar optimizations as in problem H.
 » 11 months ago, # |   +29 Alternate solution for B: Generate all points (xi + aj, yi + bj). Print the one that occurs n times.
•  » » 11 months ago, # ^ |   0 Can you please elaborate it ??? I find it difficult to understand
•  » » » 11 months ago, # ^ | ← Rev. 4 →   0 since it proves that T always exists, so the equality (xi + aj, yi + bj) = T is always able to be satisfied?so we compute all possible (xi + aj, yi + bj), since (xi + aj, yi + bj) = T is always able to be satisfied, and 1 ≤ i ≤ n,so this equality can be satisfied by (at least) n pairs of (i, j),so this solution is correct? nah tbh i'm not sure as well :/
•  » » » » 11 months ago, # ^ |   0 Thank You ! :)
•  » » » » » 11 months ago, # ^ |   +3 But the time complexity is O(N^2log(N^2)), wouldn't that be a little too slow?
•  » » » » » » 11 months ago, # ^ |   0 Not if n is 1000
•  » » » 11 months ago, # ^ | ← Rev. 2 →   0 T will be one of them (x_i + a_0 , y_i + b_0) (1<=i<=n)so , iterate over them and check for n points code
•  » » » » 11 months ago, # ^ |   0 Thank You ! :)
•  » » 11 months ago, # ^ |   0 Did the same but don't know why is it WA can you help please? 47738111
 » 11 months ago, # |   +18 Wikipedia is useful for E but I didn't found it:(
 » 11 months ago, # |   +1 I never saw this coming so fast :p
 » 11 months ago, # |   +13 One can find complement of D (n·n! - an) in OEIS: A038156
•  » » 11 months ago, # ^ |   +5 god OEIS :/
•  » » 11 months ago, # ^ |   +26 I checked the sequence itself and also some intermediate results, but unfortunately not the complement.
•  » » » 11 months ago, # ^ |   +3 If you had found the complement sequence, would you have put a different problem D?
•  » » » » 11 months ago, # ^ |   +27 Probably yes. And this problem would still be useful for a contest without Internet access, e.g. local ACM contest.
•  » » 11 months ago, # ^ |   0 Damn i tried finding a$_n$ directly and couldn't find it. had to come up with a recurrence myself by just seeing patterns.
•  » » » 11 months ago, # ^ |   0 Me too... looks like I need to work on OEIS skills :/
•  » » » » 11 months ago, # ^ |   +23 Maybe you should work on your problem solving skills?
•  » » » » » 11 months ago, # ^ |   +12 Haha, that goes without saying.
•  » » 11 months ago, # ^ |   0 Why do we subtract from n.n! ?
 » 11 months ago, # | ← Rev. 3 →   +1 How didn't my Recursive Backtracking get TLE on problem B?link here: http://codeforces.com/contest/1091/submission/47737516I can't prove its time complexity but it seems to be like due to its elapsed time?Well seems it works much slower than but it took only 0.5s ,how?
•  » » 11 months ago, # ^ |   0 The Verdict is Accepted, isn't it?
•  » » » 11 months ago, # ^ |   0 so i'm confused :/i thought it should get a TLE but it didn'tthis makes me puzzled
•  » » » » 11 months ago, # ^ |   0 N was only upto 1000
•  » » 11 months ago, # ^ | ← Rev. 2 →   0 Your solution's time complexity is about . In test 47 and 49, it equals 5 × 108.Thanks to Codeforces' amazing servers, it can pass the system test.
 » 11 months ago, # |   +19 Wait, where did it say that sqrt in problem G returns a sqrt randomly?
•  » » 11 months ago, # ^ |   +29 You are choosing x randomly so in the n = pq case, the system doesn't know which of the four possible values of x yield a square of x2. The square root returned is random in the sense that it only depends on and not x.
 » 11 months ago, # |   +1 I can't understand C. Can Anyone explain?
•  » » 11 months ago, # ^ | ← Rev. 4 →   +1 it's easy to understand that, m = DivisorCountOf(n);Now, how to get f_1, f_2 , f_3, ......f_m ?What will be the series for some k , n (for all k , n%k == 0) k = 1, n = 6 , series a: 1 2 3 4 5 6 k = 2, n = 6 , series a: 1 3 5 k = 3, n = 6 , series a: 1 4 k = 6 , n = 6 ,series a: 1 series len = n/k a_1 = 1 a_len = n-(k-1); /// cause , (a_len + k)%n = 1 so , series sum : len*(a_1 + a_len)/2Here is the Code
•  » » » 11 months ago, # ^ | ← Rev. 2 →   0 thanks
•  » » » 11 months ago, # ^ |   0 very good explanation
•  » » » 11 months ago, # ^ |   0 how do you come to the point that for all the values of k such that n%k! =0, the resulted series will be a repeated form of some k for which n&k==0?
•  » » » » 11 months ago, # ^ |   0
•  » » » 11 months ago, # ^ |   0 How you get the formula to get serious sum? Can explain a bit? :)
•  » » » » 11 months ago, # ^ |   0 For an A.P. The sum S = (n / 2)[2a + (n - 1)d] which can be written as S = (n / 2)[a + a + (n - 1)d] which is equivalent to S = (n / 2)[a + alast]
•  » » » 11 months ago, # ^ |   0 why k can't be 5 in above case
•  » » » » 11 months ago, # ^ |   0 5 isn't a divisor of 6 (-_-)
 » 11 months ago, # |   0 When is the tutorial for A coming ? ( ͡° ͜ʖ ͡°)
 » 11 months ago, # |   +1 Can anyone explain Problem C solution with example ?
•  » » 11 months ago, # ^ | ← Rev. 2 →   0 using some examples, u can see that for values of k which are factors of n, the ball reaches in a single round(it's obvious). but, for other values of k for a given n, u will get the same result as obtained by one of the factors of n.for ex n=6 k=1 gives [1 2 3 4 5 6 1], k=2[1 3 5 1], k=4[1 5 3 1], k=5[1 6 5 4 3 2 1]; 1=5 and 2 = 3;
 » 11 months ago, # |   +3 Statement of problem B was very long and hard to understand hope to fix problems like that next time :)
•  » » 11 months ago, # ^ |   0 No.
•  » » 11 months ago, # ^ |   0 Exactly :)
 » 11 months ago, # | ← Rev. 2 →   0 Both My Solutions for B and C failed on main tests, but I can't seem to find my error.Edit: I realized my Mistake with B
•  » » 11 months ago, # ^ |   0 I think it is because your condition in selecting minimum and maximum pointif(a.x > max.x) { max = new Point(a.x, a.y); }maybe the a.x = max.x but a.y > max.y you don't handle this condition in your codethe same thing in the min condition Java
•  » » » 11 months ago, # ^ |   0 Thanks, I forgot to consider that. Do you know what could possibly be wrong with my solution for C?
•  » » » » 11 months ago, # ^ |   0 i think the problem in "find" function make sure that it is the right way to calculate fun number. here is how i calculated fun number // d -> divisor // n -> input number ll x=n/d; ll fun=x*(x+1)/2; fun*=i; fun-=n; fun+=x; st.insert(fun); 
 » 11 months ago, # |   0 Can anyone explain the recurrence in D?
•  » » 11 months ago, # ^ |   +1 as far as i understood :Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice alwaysI am not sure how they counted for a single part i had a different approach which is giving some answer right and some wrong..but the recurrence is there as all the n! length string have same contribution , all starting with 1 , contribute the same as all starting with 2 , 3 and so on
•  » » 11 months ago, # ^ | ← Rev. 6 →   +1 d(n) = (d(n - 1) + (n - 1)! - 1) × n
•  » » » 11 months ago, # ^ |   0 can you explain why the sum of from k to n-1 n!/K! works i understand that for the recurrence relation ship it's required n*n! to find all possible permutations but why i am subtracting it from n!/k! every time using different k thanks in advance?
 » 11 months ago, # |   -8 SO FAST!
 » 11 months ago, # | ← Rev. 2 →   0 Can anyone please explain D?
 » 11 months ago, # | ← Rev. 4 →   -24 Thanks for a fast editorial!
•  » » 11 months ago, # ^ | ← Rev. 3 →   -23 Did I say something wrong? If anyone has any problem with anyone's comment why not just skip it? I think all the persons here are programmers with a developed mentality. Only a freak or mentally disordered person can do such kind of stupidity. Sorry, if I am saying something wrong.
 » 11 months ago, # | ← Rev. 7 →   +2 walnutwaldo20 told me that on D, if you split the sequence evenly into n consecutive parts (each part has length n!) then: each part contributes the same amount to the final answer no valid subarray can overlap with 2 different parts But we don't know why this is true. Does anyone know the proof?edit: nvm ignore this
•  » » 11 months ago, # ^ |   0 Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice alwaysJust explaining in case someone else also didn't understand this
•  » » » 11 months ago, # ^ | ← Rev. 2 →   0 I have used a similar approach and got ac. The sequences having first i(0<=i<=n-2) digits same will have equal contribution.here's my solution http://codeforces.com/contest/1091/submission/47756044 In my solution I have added n! initially and looped from 1 to n-2 which is same as looping from 0 to n-2.
 » 11 months ago, # |   0 For problem B, I simply created an array of tuples for all possible answers that we might get. To do so, I simply took the coordinates of the first pillar and created all its combinations with the clues. So, it gave me total n possible answers. Then I created a map of tuples of the coordinates of the clues. Now, while iterating over each possible answer in the array that we created initially, I checked if there exists a coordinate in our map such that we would get the same T for which we are currently checking in our possible answers array, If we have all the required coordinates, then we have have found our answer or else we continue checking for the next possible answer.
 » 11 months ago, # | ← Rev. 2 →   +37 The constraints on problem E seem incredibly tight.Many O(N * log^2(N)) didn't pass (which is unsurprising, given 500K elements and 2 seconds). I thought the limit was so tight especially so that such solutions wouldn't pass (since there is a linearithmic solution), but the editorial does mention a O(N * log^2(N)) solution, so it seems like it was intended to pass after all?.
 » 11 months ago, # |   0 3 10 10 50 WGLIn problem F how is the ans 220 for this ? I think it should be 240? Am i missing something? pls help
•  » » 11 months ago, # ^ |   +6 Swimming for 120 minutes, gaining 40 stamina, then 50 minutes for grass getting another 10 stamina and 50 minutes for lava. 120 + 50 + 50 = 220min
 » 11 months ago, # |   0 Example of my patternI need help with why my solution is wrong for D So basicly my approach is dividing it into 2 parts , 1 is the obvious n! arrangements of 1 to n , then concatenations of 1 to 2nd and 2nd to 3rd and so on So what i found out was , when we check from 1st to 2nd we get exactly n-2 , from 2nd to 3rd we get n-3 this alternates all the way till the first digit is changed i.e ( 1 4 3 2 — > 2 1 3 4) so as each different digit startings ( 1 2 3 4 .... , 2 1 3 4 ... ) contribute the same amount so i found out the result for 1 2 3 4 and multiplied it by n for patterns starting with 1 , i did ((n-1)!)/2 * (n-2) + (((n-1)!/2) -1)* (n-3) , the -1 in 2nd case because last transition for digit change would have no substring that has all 1 to n Can anyone help me out http://codeforces.com/contest/1091/submission/47759220its giving correct answer for some cases and for some it is giving a wrong answer
•  » » 11 months ago, # ^ |   0 i also got the same logic but not understanding it is not applied on 10!;??have u got the probelms in this approach...??
 » 11 months ago, # |   0 Can I get a better explanation of D?
 » 11 months ago, # |   +3 I have solved 'D' by finding patterns.Divided into some points: Each legal sub array of length n must have numbers from 1 to n at most once. So, each permutation counts as one of the legal sub array. Now extra legal sub arrays come after concatenating permutations. Two adjacent permutations definitely have ELSA(Extra Legal Sub Array) from n-2 to 0. The number of ELSA varies according to the first place from the left side where two adjacent permutations don't have same digit. (Ex-1: n=3, 1234 & 1243; 2nd place from right or 3rd from left. Ex-2 : n=3, 1243 & 1324; 3rd place from right or 2nd from left.) Here comes my pattern finding. For 2nd place(from right) ELSA is n-2, for 3rd place(from right) ELSA is n-3, for 4th place(from right) ELSA is n-4, goes on unless ELSA is 1 at the (n-1)-th place. Now I have to find how many adjacent pairs of permutation have X-th digit(from right) variant. Then, (Number of X-th digit variant) * (n-X); Here we found total ELSA after concatenation, now add the number of permutation. Finding amount of each X-th digit variant: (By example, for n = 4): 1. 1-th variant or, number of permutation : 4 * 3 * 2 * 1 = 24 2. 2-th variant : 4 * 3 * (2-1) * 1 = 4 * 3 * 1 * 1 = 12 (ELSA = 2) 3. 3-th variant : 4 * (3-1) * 1 * 1 = 4 * 2 * 1 * 1 = 8 (ELSA = 1) 4. 4-th variant or n-variant never need because it's corresponding ELSA = 0. Now, 24 + (12*2) + (8*1) = 56.
•  » » 11 months ago, # ^ |   +1 Thank you so much ! :)
•  » » 11 months ago, # ^ |   +1 Can you please further explain how to find the X-th digit(from right) variant? And one more thing i want to clarify, ELSA's of 1-th variant (from right) = no. of permutations i.e. n! Thanks in advance:)
•  » » » 11 months ago, # ^ |   0 Finding amount of each X-th digit variant: (By example, for n = 4): 1-th variant or, number of permutation : 4 * 3 * 2 * 1 = 24 [[Yes, it is n!] 2-th variant : 4 * 3 * (2-1) * 1 = 4 * 3 * 1 * 1 = 12 (ELSA = 2) 3-th variant : 4 * (3-1) * 1 * 1 = 4 * 2 * 1 * 1 = 8 (ELSA = 1) 4-th variant or n-variant never need because it's corresponding ELSA = 0. Now, 24 + (12*2) + (8*1) = 56. I just decrement X-th place number(from right) for each X-variant. I did this to find how many pairs of permutation are there, where they create X-th variant. If you think in a permutational way, you will find that this is a correct way to calculate those pairs.Check My Implementation: https://codeforces.com/contest/1091/submission/47767219
•  » » 11 months ago, # ^ |   +1 Your method is perfect! Thank you very much :)
 » 11 months ago, # |   +31 Problem E can also be solved without using the property that the answer lies in a contiguous interval (yeah, guess who didn't realize this during the competition).Following the Erdos-Gallai theorem, assume we want to check whether d could be the answer. Let p be the position d would be in the degree array. For every position i ≤ p, d would appear on the left hand side of the inequality, thus posing a lower bound on d; for every position i > p, d would appear on the right hand side, thus posing an upper bound.The bounds at each position can be preprocessed using prefix sums (for right hand side we could do a binary search instead of value counts), and we can also perform another scan to obtain the "prefix lower bounds" and "suffix upper bounds" (i.e. intersection of prefix/suffix bounds). To check whether d could be the answer, just check the prefix and suffix bounds, and also whether the inequality satisfies at position p itself. Checking one value takes time (just have to do a binary search), so the whole algorithm is .One caveat that can be ignored: we still need to take the min function on the right hand side into account: if i < d then we should ignore the bound. This means that when checking the answer, instead of using the "prefix" bound, we should use the bound of an interval, which is the maximum value in the interval. This is an static RMQ problem and can be solved in O(1) using sparse tables. However, it seems that the lower bounds are non-decreasing (bounds at higher indices are always tighter (larger)), so this is unnecessary.
•  » » 11 months ago, # ^ |   0 In the contest I solved it only realizing it had to be a contiguous interval between [ai, ai + 1] where the ai's are the integers in the input in sorted order.
•  » » 11 months ago, # ^ |   +3 Well, I just want to mention that this solution can be hard to implement. During the contest I also came up with a similar idea, but it took me more than 1 hour to code.
•  » » » 11 months ago, # ^ |   0 Yes, you're right. I had a bunch of off-by-one errors before I could get it to work.
•  » » » 11 months ago, # ^ |   +8 Indeed, it's quite a bother to implement. The version I got in the contest is actually O(n) after sorting: 47753124. The prec0 and prec1 arrays correspond to the "sum of mins" part in the formula which can be precomputed by two pointers, and the lower/upper bounds are accumulated once and for all in opposite directions.
•  » » 11 months ago, # ^ |   0 How do you mean its O(logN) per value? For every value, you need to check all preffixes, not only one, isnt it O(n) per value? Looks like you are checking only prefix till new value but that does not make sense, becase that is not only prefix that can make inequality wrong.
•  » » » 11 months ago, # ^ |   0 To check whether degree d could be an answer, we need to first find the position p of d in the sorted array of degrees. Then, we need to check whether all n inequalities hold. These can be categorized by their positions i into three types: i < p, i > p, and i = p. As I mentioned in the previous post, each type could be checked within time with a bit of preprocessing. It's a bit hard to explain so I won't go into details here, feel free to check out my submission: 47769662.
•  » » 11 months ago, # ^ |   0 can you please explain this part of the code from the editorial of problem E - int is_score(int value) { vector C(N+1,0); for (int i = 0; i < N; ++i) ++C[A[i]]; ++C[value]; int less = 0; long long left = 0, right = 0; for (int k = 0, i = 0; k <= N; k++) { int val = (i == k && (i == N || A[i] < value)) ? value : A[i++]; left += val; --C[val]; right -= min(val, k); less += C[k]; right += N-k-less; if (left > right + (long long)(k+1)*k) { return (i == k) ? TOO_BIG : TOO_SMALL; } } return OK; } 
 » 11 months ago, # |   -10 I am still a Beginner ^ _ ^ need help in A please
•  » » 11 months ago, # ^ |   -13 iterate from 100 to 1 for finding valid number of flower code:: scanf("%d %d %d",&a,&b,&c); for(int i=105;i>=0;i--) { if(i<=a+1&&i<=b&&i+1<=c){ cout<<3*i;break; } }
 » 11 months ago, # |   -8 Let me share my approach on D!We can observe (I have yet to figure out how!) that valid subarrays are the ones that contain all the n distinct numbers (from 1 to n). So, for example, for n = 5: [4, 2, 3, 5, 1] is a valid one, but [4, 2, 2, 3, 1] is not!So, now let's write down the concatenation of the permutation for n = 4 (hoping to find a useful pattern):1 2 3 4 (p1)1 2 4 3 (p2)1 3 2 41 3 4 21 4 2 31 4 3 22 1 3 4.... (pn!)Obviously, for each p, all the subarrays that start at the beginning are valid (n! so far). (S0 = n!)What about the the ones that are beginning at the middle of p? (for example, on p1, the [2, 3, 4, 1] subarray?)As you can see, we need the part of the subarray that belongs to the next p (its prefix) must be equal to the prefix of the current p. So, for instance, on p1 if we take (again) the [2, 3, 4, 1] subarray, we observe that the part which overlaps with p2 (ie. the 1) occurs on p_1, too! So, by applying this reasoning, we can conlude the following:If the subarray begins from 2nd number of a p (it ends on the 1st number of the next p), it is invalid only when the 1st number is changed (see the last p on the example) (ie when 1st number on permutation become 1 (the first number), 2, 3, 4, ..., n). The valid subarrays for this case are S1 = n! (total permutations) — nIf the subarray ends on the 2nd number of the next p, it is invalid only when the 2nd number changes (ie. when the prefix becomes _ -> [1, 1], [1, 1] -> [1, 2], ..., [1, 4], [2, 1]) and this happens n * (n — 1) times, so S2 = n! — n * (n — 1)...By continuing like this, we get Sk = n! - n·(n - 1)·...·(n - k + 1)Hence, we need to calculate the following expression:ans = S0 + S1 + ... + Sn = n! + (n! - n) + (n! - n·(n - 1)) + ... + (n! - n!) = n·n! - (n + n·(n - 1) + ...)How can we calculate the second part?We can use DP, where:DP[n] = nDP[n - 1] = DP[n]·(n - 1)...DP[k] = DP[k + 1]·k (until k = 2 is enough)And we sum them! :)I hope that this helps!
•  » » 11 months ago, # ^ |   0 you don't need to calculate the second part using dp. just sum it is ok.
•  » » » 11 months ago, # ^ |   0 Oh really! How?
•  » » » » 11 months ago, # ^ |   0
 » 11 months ago, # |   0 For problem E: I found the Erdős–Gallai theorem after googling with something like "valid degree sequence". After some investigation into the theorem, I figured out that a solution could be devised using it. However, there are so many details (at least for me) involved in the adaption of the theorem to this problem that I couldn't make it within the last one and a half hour during the contest :(. For those who may be interested in this approach, see my submission 47772075 for detail. The complexity is .
 » 11 months ago, # |   +5 How does one show that the given solution for F is optimal?
 » 11 months ago, # |   +26 We use the Erdos-Gallai theorem linked in the statement to determine whether a sequence is graphic. I just thought the link was to explain the statement during the contest...Cannot imagine there exist some solutions...
 » 11 months ago, # |   +16 In the problem E,how to make Havel–Hakimi algorithm faster?
•  » » 11 months ago, # ^ |   0 calculate the prefix sum of di at first so you can calculate in O(1)enumerate k from n to 1,define cnti as the amount of dj(k ≤ j ≤ n) which equals to i,then you can calculate in O(1)(every time k is decreased,,and for dk,add it to sum and cntdk if dk ≤ k)then you can do it in O(n)
•  » » » 11 months ago, # ^ | ← Rev. 3 →   +15 Thank you.But what your said is Erdős–Gallai theorem.In the soluton above,majk mentioned that "Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an + 1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog2n) algorithm." .But I don't know the detail about it.
•  » » » » 11 months ago, # ^ |   +15 emmm... I don't know how to use a segment tree or a mulitset to make it faster,but I know how to use splay to do it.In Havel–Hakimi algorithm,you need to find the maximum ,delete it and decrease the biggest k numbers.take di as the key value,the first and second operation will be simple.for the third operator,splay the (k+1)th number to root(if the kth and (k+1)th are equal and on the same node,split the node),mark on the root's left son,delete the root and insert it back(after decreasing the biggest k numbers,the root might be less than its left son(assume we put the bigger node on the left),so we reinsert it to make it in the right place).note that the third operation is very complex.
•  » » » » » 11 months ago, # ^ |   +5 That's right ! Thanks a lot ! Happy new year!
 » 11 months ago, # |   0 Problem E is terrible for someone who didn't care about the link.It's really surprising that part of the solution is offered in the description.
 » 11 months ago, # | ← Rev. 2 →   +15 I passed H without bitset.Maintain a list of vectors, vector[i] contains all j's which grundy[j] = i. When calculating grundy[x], find y from vector[0], vector[1], ... orderly. While finding in vector[i], if x-y is available, break, and if no break happens in vector[j], then grundy[x] = i and finish.I can't prove the time complexity, but it performs well.
 » 11 months ago, # |   0 Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an+1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog^2 n) algorithm. So,can a solution Accepted this problem with O(n log^2 n)?n=500000 and n log ^2n=1.8e8.Also multiset is very slow.... P.S. 5e5 is enough to make python programmer TLE in input.
•  » » 11 months ago, # ^ |   0 It's said that the time limits are based on Java...
•  » » » 11 months ago, # ^ |   +3 In Java,5e5 is enough for TLE,I think
 » 11 months ago, # |   0 Shouldn't the sum in the explanation of D should be instead of
•  » » 11 months ago, # ^ |   0 Fixed, thanks.
 » 11 months ago, # |   +6 I cannot figure out how to make the Havel-Hakimi algorithm faster. How can I sort the degree sequences after each update operation if I use segment tree?
 » 11 months ago, # |   0 What is the analytical proof of the solution to problem B? I mean strict mathematical, not observation
 » 11 months ago, # |   0 If we do not work out the factor list first, but use the following codeif n mod k = 0 then continue, can it pass the tests in problem C?
 » 11 months ago, # |   +13 Can someone explain why maximum Grundy number on H is 100? I was hoping the editorial would explain it but it didn't.
 » 11 months ago, # |   +21 I made some videos with more detailed explanations for some of the problems. I think my solution to F is a bit simpler than the editorial; the idea is to start with flying over everything and proceed greedily from there.
 » 11 months ago, # |   +3 How to do binary search over a boolean array as said in editorial for E? Array is something like [ N, N, N, Y, Y, Y, N, N]. Plz suggest a general approach. I know how to binary search range in sorted sequence but this sequence does not appear to be sorted.
•  » » 11 months ago, # ^ |   +1 Once you determine that the answer is no, you can figure out if you are too high or too low based on where you fail. For example, for the degree sequence [4 3 2 1 ?], if we try setting ? as 3, and then do erdos gallai, the first 2 inequalities work (4<=0+4) & (4+3<=2 +(2+2+1)), but the third does not (4+3+3>6+(2+1)). Now, looking at just this inequality, the edge with length 3 increases the left side, but not the right. So increasing 3 will not work, and we know the answer is hence less than 3.If we try to make the answer 1, then the second inequality won't be satisfied, as 7>2+(2+1+1). If we decrease the answer further, the inequality still won't be satisfied, since it will be decreasing the right side of the inequality only (instead we will have 7>2+(2+1+0)). So the answer is greater than 1. Hence we only have to check 2 and that works.
 » 11 months ago, # |   0 Auto comment: topic has been updated by majk (previous revision, new revision, compare).
 » 11 months ago, # |   0 For those don't know Lexicographically Next Permutation algorithm.Algorithm link
 » 11 months ago, # |   0 Problem D: I read the tutorial and i interpret that we are trying to find the answer by subtracting all the false subarrays.For each permutation seperately , n! should be added to answerAnd for second part in the tutorial They are taking number of total subarrays as n.n! . I can't understand this Can anybody please further explain this? Thanks in advance:)
 » 11 months ago, # |   0
 » 11 months ago, # |   +10 In question E , i think testcases are not so strong. Because in following testcase , solution of ecnerwala fails. That testcase is :94 3 2 2 2 2 1 0 0Actual answer should be [0,2,4,6] , but according to his solution , output is [0,2,4,6,8] . Link to his solution is https://codeforces.com/contest/1091/submission/47743506 .
•  » » 11 months ago, # ^ |   +18 Oops, looks like you're right, I had an off-by-one that wasn't caught. It's fixed here 47800829 if anyone cares.
 » 11 months ago, # |   0 How to prove the greedy solution for F? In editorial there is an algorithm and reasons for it, but I still dont understand how to prove such solutions, when one changes previous steps of the strategy.
•  » » 11 months ago, # ^ |   0 First, you need to fly to pass lava. Then we greedily choose to gain as much stamina as we can. So we walk on grass and swim in water. There are two situations. 1. If we still run out of stamina at some place. Then we need to do some extra work to gain stamina. So we need to go backward and forward at some place before. 2. If through this method we can spare some stamina in the end, then in fact we can spend them before. The solution discuss this situation detailedly.
 » 11 months ago, # | ← Rev. 2 →   0 In problem C : Can someone explain What is the intuition or proof behind using only divisors as K values which give unique sums?
•  » » 11 months ago, # ^ |   0 yes i also dont understand the same thing. can anyone explain this ?
•  » » 11 months ago, # ^ |   0 for each k , series : 1 , 1+k , 1+2k , 1+3k , ...... , 1 + x*k ;where , (1 + x*k)%n == 1so , (x*k)%n == 0 convince yourself , that (x*k) == n , not 2n,3n,4n.....that means k is a divisor of n
•  » » » 11 months ago, # ^ |   0 why did you rejected 2n,3n,4n... ??
•  » » » » 11 months ago, # ^ |   0 Think the circle as 0,1,2,...n-1. You wanna back to 0 not 1. Now series : k,2k,3k,....,x*kyou stop tour after finding first x such that (x*k)%n == 0 ; Now, you tell me how a number forms with it divisors? say 12 ::3 , 2*3 , 3*3 , 4*3 (which is 12 itself)
•  » » » » » 11 months ago, # ^ |   0 got it , thanks :)
•  » » 11 months ago, # ^ |   0 The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)
 » 11 months ago, # |   0 In D, Why do we subtract from n*n!?
•  » » 11 months ago, # ^ | ← Rev. 8 →   +1 There are n! permutations , from each of them we can get at most n segments (1 from it , n-1 from its_suffix + next's_prefix) with sum of segment == n*(n+1)/2For our last permutation we don't have( suffix_i + prefix_(i+1) == n*(n+1)/2 ) so why we counting n from the last one?Look , last one has bad suffixes of length 1,2,3...n-1 ; All of which are includes in subtraction process ...-
 » 11 months ago, # |   0 Hey , I'm a newbie and just beginning CP. In Problem C , why the formula ,N*(i-1)/2 + i and N*(N/i-1)/2 + N/i , Can someone please explain this formula ? And it's significance in the problem ? What is the need for this formula , why not basic AP sum where d=i ? How do people crack such formula's within 2:30 hrs ?
•  » » 11 months ago, # ^ |   0 The numbers that are hit are 1 d+1 2d+1 ... n+1 (=1). If you sum the arithmetic sequence you get the formula mentioned. Wolfram Alpha can do these kinds of sums for you: https://www.wolframalpha.com/input/?i=sum+1%2Bd*k+for+k%3D0+to+n%2Fd-1 (not sure if this is the exact right one).I made a video explaining the solution in more detail that may help: https://www.youtube.com/watch?v=CFP_lctIss4
•  » » » 11 months ago, # ^ |   0 Thank you so much sir !!! Also , Thank You for both the links , 2 awesome resources !!! :)
 » 11 months ago, # | ← Rev. 2 →   0 I found the mistake. I was running "O(n) Erdős–Gallai graphicness check" every time i was printing the answers. That was O(n2) :D
 » 11 months ago, # |   0 Can't understand why i need to consider only k that divides n. Can anyone explain that moment in other words than in editorial pls?
•  » » 11 months ago, # ^ |   0 The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)
 » 11 months ago, # |   0 Problem E:How can you prove that "on the integers of same parity, the answer always forms a continuous interval"
•  » » 11 months ago, # ^ |   0 I do not have a simple mathematical proof, but you can definitely see this fact from the statement of Erdos-Gallai.
•  » » 11 months ago, # ^ | ← Rev. 3 →   0 We assume that x is the smallest possible answer for an + 1, assuming that there is an edge u - v in the graph, then we can change this edge to u - k, k - v without changing the degree of other points, obviously we have found that the degree of k has increased by 2, and this can be done for all other edges, so the possible answer for x is continuous and has the same parity.
•  » » » 11 months ago, # ^ |   +9 You also have assumed that neither of u-k and v-k was already formed
 » 11 months ago, # |   +18 In problem G: "The primes are of form 4x + 3 for a simple reason – we can find the square root of x modulo p by simply calculating ." Shouldn't it be ?
•  » » 11 months ago, # ^ |   0 I think so. This is a typo.
 » 11 months ago, # | ← Rev. 3 →   -8 毒瘤啊
• »
»
11 months ago, # ^ |
+1

# define TOO_HUGE lyz

 » 11 months ago, # |   0 Problem H:How can you proof that the Grundy number is less than 100?
 » 10 months ago, # |   0 Problem C , i didn't understand if (N%i==0) { ans.push_back(N*(i-1)/2 + i); if (i*i!=N) { ans.push_back(N*(N/i-1)/2 + N/i); } }