Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

### ch_egor's blog

By ch_egor, 3 years ago, translation,

Thanks for the participation!

1445A - Array Rearrangment was authored by meshanya and prepared by ch_egor

1445B - Elimination was authored by Helen Andreeva and prepared by ismagilov.code

1444A - Division was authored by vintage_Vlad_Makeev and prepared by grphil

1444B - Divide and Sum was authored and prepared by NiceClock

1444C - Team-Building was authored by V--o_o--V and prepared by wrg0ababd

1444D - Rectangular Polyline was authored by Zlobober and prepared by DebNatkh

1444E - Finding the Vertex was authored by V--o_o--V and prepared by 300iq

• +121

| Write comment?
 » 3 years ago, # |   +4 very fast tutorial thanks.
 » 3 years ago, # |   +15 Great sets of problems with short statements!
 » 3 years ago, # |   +1 Thanks for the quick tutorial!
 » 3 years ago, # |   +1 How the heck was this really 14 hours ago? xD
•  » » 3 years ago, # ^ |   +6 It was created 14 hours and kept as a draft and now it is made public.
•  » » 3 years ago, # ^ |   +6 prepared earlier and made private. made public only after the contest
 » 3 years ago, # |   +3 I do not understand the sentence: "Any difference |pi−qi| in our sum will be the difference of one element of R and one element of L." Sombody explain the meaning of it?
•  » » 3 years ago, # ^ |   +4 That means either pi belongs to R and qi belongs to L, or pi belongs to L and qi belongs to R. Since this is true for all pi and qi, then the sum of |pi — qi| is the same as the sum of R — L
•  » » 3 years ago, # ^ | ← Rev. 2 →   +2
•  » » » 3 years ago, # ^ |   0 The key point is that for any pair pi,qi it is true that one of the if from set L and the other from set R. Unfortunatly the sentence does not say that. Instead it states it as a property of the sum, which is confusing.
•  » » » » 3 years ago, # ^ |   0 can you please elaborate.. I cannot find the connection between L and R being sorted and this logic.
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   +5 L is per definion the lower half, and R the upper half. They are not sorted, but each element in L is smaller than each element in R.Then, if you have any permutation of all elements, cut them in two sets of same size and sort the two halfs/sets according to the statement. ie left one ascending, right one descenting.Then it turns out that for all p[i],q[i] the two values are from original L and other from R. That is true independend of any sum, so it is not a property of the sum, but a property of the way the sorting of the halfes is defined.
•  » » » » » » 3 years ago, # ^ |   0 got it, thanks
 » 3 years ago, # |   -9 Why does this give WA for (C) Division : https://codeforces.com/contest/1445/submission/97331021 ?Fails at 7th Test (Passed the Pretests)
•  » » 3 years ago, # ^ |   +6 In the first WA testcase, the numbers were $p=2^4 \times p_1 \times p_2$, $q=2 \times p_1$, where $p_1,p_2$ are some large primes. Your solution printed $2^4 \times p_2$, where the correct answer is $p_1 \times p_2$
•  » » 3 years ago, # ^ |   -10 Mine WA at 18th T_T Link to solution
 » 3 years ago, # |   +39 What does $O(n^2 \log n \ldots n^4)$ mean, wtf xd?
•  » » 3 years ago, # ^ |   +23 Well... It is something between $n^2 \log n$ and $n^4$ :) It depends on your $T(n)$.
•  » » 3 years ago, # ^ |   +33 It means that it is between $O(n^2 \log n)$ and $O(n^4)$ depending on your implementation. I agree, unexplained arbitrary notations should be avoided...
•  » » » 3 years ago, # ^ |   +33 Yeah, I am sorry.
 » 3 years ago, # |   +29 For Div1 Problem B, I just made random tc for n = 1, 2, 3 and solved on paper. Then I found that cost is always same and got AC xD
 » 3 years ago, # |   +24 I misread non-increasing as non-decreasing in problem B and worked almost 2 hours on it(
•  » » 3 years ago, # ^ |   0 same
•  » » 3 years ago, # ^ |   0 I'm still confused with the solution provided above , can you please eloborate it !
•  » » » 3 years ago, # ^ |   0 should i help or is it done by now?
 » 3 years ago, # |   +2 Problem statements were really very straight forward...No time wasted in reading useless story and understanding problem statements
 » 3 years ago, # |   0 Can anyone help with counting $С_{2n}^{n}$ without overflow?
•  » » 3 years ago, # ^ | ← Rev. 3 →   +22 Use Fermat's little theorem to find the modular inverse of n! via fast exponentiation.Basically k / x is congruent to ka (mod m) if m is prime and a=x^(m-2), so you can just find n! mod m and (2n)! mod m, and then use exponentiation to get (n!)^(m-2) mod m. The answer is just (2n)! mod m multiplied with (n!)^(m-2) mod m twice, modulo m the whole thing.
•  » » 3 years ago, # ^ | ← Rev. 2 →   +2 You can use modular inverse and store them like this 97357968. I think it is a good template
•  » » » 3 years ago, # ^ |   +3 Thanks! I will try :)
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 https://mathworld.wolfram.com/BinomialCoefficient.htmlin this link see the definition of Binomial coefficients for nonnegative integer y. For calculating 2n! use Fermat theorem
 » 3 years ago, # |   0 Div2 A, why do we have to check for all i? Like after sorting one array in descending order, shouldn't it be sufficient to check a[0]+b[0] and a[n-1]+b[n-1]? Can someone please give an example where this fail?
•  » » 3 years ago, # ^ |   +23 Let us consider the following test case 1 3 4 1 3 3 3 2 1 Here, a is sorted in ascending order. And b is sorted in descending order. also, a[0]+b[0] = 4, which is equal to 4, and a[n-1]+b[n-1] = 4, which is equal to 4.However, a[1]+b[1] = 5, which is greater than 4
•  » » » 3 years ago, # ^ |   +10 Oh cool! Thank you
•  » » » » 3 years ago, # ^ |   +10 your welcome :)
 » 3 years ago, # | ← Rev. 2 →   0 Did someone managed to solve div1B without the observation that any partition holds equal sum?
 » 3 years ago, # |   0 Can anyone please tell me why is my code giving wrong answer on test 18 for problem C- Division.
 » 3 years ago, # |   +4
 » 3 years ago, # |   +1 Thanks for update the rating fast!
 » 3 years ago, # | ← Rev. 2 →   0 My rating goes bruh 'cause I forgot to reset res = 0 after each testcase when solving C lol
 » 3 years ago, # | ← Rev. 2 →   +4 Can someone please explain solution of Problem Div2C/Div1A more clearly?
•  » » 3 years ago, # ^ |   +4 cases1) if p%q!=0, then answer is p itself2) else we need a largest factor of p such that it is not divisible by q let's say the prime factorization of p is 2^x*3^y.. and for q as 2^a*3^b.., as p%q==0 then x>=a, y>=b and so on for every prime factor of p.Now is the trick/move, we can find possible x of p by removing some factor of p from prime factorization of p, for eg. if I remove 2^(x-a+1), you can see that p/2^(x-a+1) is no more divisible by q. That's it you have to try removing different prime's power and the one with maximum p/prime^(x-a+1) is the answer.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 Your explanation is really very much nice. Loved it and understood it just by reading once. Wanted to thank you and also I have a doubt. Won't the prime factorization takes O(sqrt(p)) time and won't it be of the order 10^9. Isn't the time constraint of 1-second means 10^8 operations?? Or finding the prime factorization of q will be enough??
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   0 Welcome! It's enough to find prime factorization of q. As only removing factors of q from p will make x not divisible by q.It got AC with this one too, it is just factorization. https://codeforces.com/contest/1445/submission/97396204
•  » » » » » 3 years ago, # ^ |   0 Ok cool.Thanks for your help.
 » 3 years ago, # |   +5 What's wrong ???
•  » » 3 years ago, # ^ |   +3 Have some patience, it will automatically update in some time.
•  » » 3 years ago, # ^ |   0 how did you enable dark mode??
•  » » » 3 years ago, # ^ |   0 There are many extensions on the Chrome web store. I use "Stylish". It has both codeforces and codechef dark modes. Although they are not completely optimised for visibility.
 » 3 years ago, # |   -7 Please give a detailed explanation of Div 2 C
•  » » 3 years ago, # ^ | ← Rev. 7 →   +4 I understood one approach. Here it is: if $p\% q\neq 0$ then $x=p$ else we can represent $p$ and $q$ as follows: $p={p_1}^{k_1}*{p_2}^{k_2} \dots {p_y}^{k_y}*{p_{y+1}}^{k_{y+1}} \dots {p_m}^{k_m}$ $q={p_1}^{l_1}*{p_2}^{l_2} \dots {p_y}^{l_y}$ where $p_1,p_2 \dots p_m$ are prime numbers. Here $k_1 \geq l_1, k_2 \geq l_2, \dots ,k_y \geq l_y$ because $p\% q=0$ Now we have to take some factor of $p$ such that it is not divisible $q$. We can do this by taking some prime number power equal to one less than corresponding power in prime factorization of $q$ or you can say that we have to divide $p$ by that prime factor until $p$ is not divisible by $q$ . We can do this for all prime factors of $q$ and take the maximum one. Here is my submission
•  » » » 3 years ago, # ^ |   0 Exactly my approach. But here's what happened to my submission
•  » » » 3 years ago, # ^ |   0 You have done sieve upto 2e5+9 only right, how does it work when q is divisible by a prime number greater than N?
•  » » » » 3 years ago, # ^ |   0 What is N here?
•  » » » » » 3 years ago, # ^ |   0 N used in nubir345's code = 2e5+9
•  » » » » » » 3 years ago, # ^ |   0 see this line: if(temp>1){ v.eb(temp); } You should see that if there is such a prime then there can only one such prime with that of power 1. So after the loops, if temp>1 then it means that temp is a prime. And it is taken into consideration.
•  » » 3 years ago, # ^ |   0 I don't know if it will make more sense or not the way I thought is ,Let's call the power of a prime number in prime factorization of a number prime count.if a number(x) has at least one prime count greater than another number(y) than y%x!=0 Suppose , x=2*2*2*2*3and y=2*2*3*3*3*5 then y/x will be something like (3*3*5)/2 which won't be a integer.so now for the given two numbers p & q , we need to find a 'x' that where p%x==0 & x%q!=0and we have to make it the biggest. So we start by making x=p (in another way saying taking all the primes in p) then checking for every prime in q can we make the prime count of q greater than that of x. We print which ever gives the largest number. vectorprimes;//all the primes in the prime factorization of q. for(ll i=0;i
 » 3 years ago, # |   0 I passed Div1 D with $O(n^2C/bitset)$ using "malaysian knapsack", $C$ is the sum of length.
•  » » 3 years ago, # ^ |   +16 What is a "malaysian knapsack"?
 » 3 years ago, # |   0 Div2 D/Div1 B is a beautiful problem, with a wonderful proof, thanks
•  » » 3 years ago, # ^ |   +11 So sad I was not able to see this during contest. Intuition really plays a big factor in that I think. I straight went to calculating combinations.
 » 3 years ago, # |   +3 1444B — Divide and SumF how to think like that ? can some one explain the way of thinking step by step ? Please
•  » » 3 years ago, # ^ |   +3 Think of it this way-> Lets say the array is sorted, then the problem get reduced to choosing a subsequence of n elements, reverse it and then find the "cost". Now try to find which elements will always contribute a negative value ( that is -a[i]) and which will contribute a positive value (that is + a[i])............you will realise that elements from 1....n always contribute a negative value, while elements n+1.....2n contribute a positive value. In the end just multiply the final answer with the number of possible combinations.
 » 3 years ago, # |   +1 In the editorial of Problem C, why q is not divisible by x?
 » 3 years ago, # |   +1 proplem b was not clear enough and the proplem must be more than that
•  » » 3 years ago, # ^ |   +1 yeah... It took me one hour to understand.
 » 3 years ago, # |   0 How can one solve Div2D if p and q are both sorted in non-decreasing order? (spent hours trying to upsolve this variant then saw the editorial and was like f**k me :P )
•  » » 3 years ago, # ^ |   +11 same here. QAQI came up with a $O(N^2)$ solution, which I calculated the appearance of each pair $(a_i,\ a_{i+1})$ (sorted). Wondering if it's possible to reduce it down to $O(N)$?
 » 3 years ago, # |   -10 Problem E appeared at the eleventh POI.
•  » » 3 years ago, # ^ |   +13 Which problem was that?
•  » » » 3 years ago, # ^ |   0 "Jaskinia"
 » 3 years ago, # |   0 Can someone explain , why the solution got WA ? Thanks in Advance!! Problem — Div2A Link to my solution :https://codeforces.com/contest/1445/submission/97363313
•  » » 3 years ago, # ^ |   0 You didn't sort
•  » » 3 years ago, # ^ |   0 Sorry I didn't see in the problem that the arrays are already sorted.The problem is there are multiple cases in ONE test case, and if you are exiting the input with return (in one solve() iteration) once you found something >=x, the other numbers left in a,b array won't be read, therefore all the following inputs are wrong.
 » 3 years ago, # |   +14 There should be a special tag for problems like 2D/1B: LOL problem
 » 3 years ago, # | ← Rev. 5 →   +3 The way I thought about problem DIV2-D : Every element either gets added or subtracted, if we consider individual contribution of elements. Added : when the corresponding element in other sequence is lesser; Subtracted: when the corresponding element in other sequence is greater; Let's now see when an element is subtracted : Consider p, q be the non-decreasing and non-increasing sequences respectively;=> p[1] <= p[2] <= ....... <= p[n] ------- (1) => q[1] >= q[2] >= ....... >= q[n] ------- (2) Let's say p[i] contributes it's negative, i.e. it gets subtracted, Then we can say from (1) that number of elements greater than or equal to p[i] are n-i; Also since q[i] >= p[i] (as p[i] is being subtracted), all of (q[1], q[2] .... q[i]) >= p[i]; Now we can say that atleast (n-i) + i = n elements are greater or equal to p[i]; Similarly we can derive the condition for the other case ! Which proves that for a value to contributes it's negative it has to be in first half of the sorted array of all values ! Hope it helps, sorry if this approach is mentioned above, I dint read all the comments !
 » 3 years ago, # |   0 There is a spell mistake in solution of 1C.Para.4, the last word "answer" was mistaken for "asnwer".
 » 3 years ago, # |   +3 Fun Fact — sinus_070 was the first person to get an AC on 1B/2D. That's because he created the same problem on topcoder a few months ago. Click 1 Click 2
•  » » 3 years ago, # ^ |   0 Yeah, I think he also commented the same on the announcement blog.
 » 3 years ago, # |   +13 In Div1C, there are lots of submissions AC using DSU to find the number of "bad pairs" instead. Could anyone explain a little bit about this solution? Thanks so much <3.
•  » » 3 years ago, # ^ |   +8 DSU without path compression can cancel the previous merge. So you can add all the edge between two groups, using DSU to judge whether this group pair is 'bad', then delete the edges and restore the DSU.
•  » » » 3 years ago, # ^ |   0 I tried to use dsu during the contest.But will "delete the edges and restore the DSU" get TLE?
•  » » » » 3 years ago, # ^ |   0 I think one edge can be added or deleted up to k times.(unless compress the connected component of same color)
•  » » » » » 3 years ago, # ^ |   0 An edge which connect two different group would be added and deleted just once. Initially add all edges that the two node which it connect belong same group. Then select two group (ignore pairs which don't appear), add all edges between them, check if this pair valid, and delete these edge.
•  » » » » » » 3 years ago, # ^ |   0 Thank you very much . I understand.
•  » » 3 years ago, # ^ |   +11 You may see the technique using DSU with rollback to check bi-graph in this problem 813F - Bipartite Checking.
•  » » 3 years ago, # ^ |   0 Wow thanks <3
 » 3 years ago, # |   0 Verdict shows accepted then why my rating decreases?
•  » » 3 years ago, # ^ |   0 Read this to understand the rating system
 » 3 years ago, # |   0 Can somebody please explain me the logic of div 2 C. Please..
•  » » 3 years ago, # ^ |   0 I can tell you my logic which got accepted. for q, i calculated it's prime factors and their powers (how many times a particular prime no occured) . i stored this in a vector of pairs and then checked whether p is divisible by all prime factors of q or not. if not then p is the ans. else suppose the power of a prime factor of q in p is p1 and in q it is p2. so, i divided p by pow(pf,p1) and multiplied by pow(pf,p2-1). i did this for every factor of q and calculated the maximum number formed out of them. sol link- https://codeforces.com/contest/1445/submission/97400076
•  » » » 3 years ago, # ^ |   0 Can you please explain me a little why you are at first removing all the factor of that prime and after that multiplying it by p2-1, What's the intuition behind that?
•  » » » » 3 years ago, # ^ |   0 let q be of the form 2^p1*3^q1... .then q will divide p only if p has atleast power of 2(same implies to other faCTORS) equal to p1 in it . therefore we reduce power of 2 (for example) in p to p1-1 and then the number so formed will not be divisible by q.so we reduce power of such prime factor in p such that it reduces value of p by least amount .
 » 3 years ago, # |   0 Am I the only person who felt Div2-D is easier than Div2-C ?
•  » » 3 years ago, # ^ |   0 Div2-D is easy once you see that property that the sum is same for all permutations. If not, then not. Additionally the statement was formulated in a way that it was easy to get it wrong concerning the ordering of the two halfs L and R.Div2-C on the other hand is fairly clear, all that division and things. What else can we do than a prime factorization of the only value where we can do it? Once realized that it is not so hard to use the fatorization to construct ans.
 » 3 years ago, # | ← Rev. 10 →   0 Can anybody please explain me the editorial of problem A. Div2 ? I am not getting it please!
•  » » 3 years ago, # ^ |   0 Consider the smallest value in a[], min(a[]). Which one of b[] values would be "the best" to pair with that?Well, in b[] there is a biggest value, max(b[]). If $max(b[])+min(a[]) > x$, then there is obviously no solution.Else it is optimal to "get rid of max(b[])" by using it. Then repeat with the next smallest value in a[]... same again, you will pair it with the then biggest value in b[].You end sort a[] ascending, and b[] descending, and checking all pairs a[i]+b[i].
 » 3 years ago, # |   +5 About 1444C - Team-Building, can anybody please explain me the sentence "If the path of the cycle in this component ends in the same side where it has started, then it has even length, and odd otherwise."? Thanks in advance.
 » 3 years ago, # |   +3 How to solve 1444B - Divide and Sum if $f(p,q)$ set to be $f(p,q) = \sum_{i = 1}^n x_i y_i$
•  » » 3 years ago, # ^ |   0 Now, I know how to do it in $O(n^2 \log n)$ time complex and $O(n^2)$ space complex if $f(p, q)$ is bilinear. Just note that if $x_i$ belong to the left side, then $y_i$ belong to the right side, and vice verse. So we only have to consider $f_k(p, q) = \sum_{i = 1}^k x_i y_i$ with $x_k$ in the left side and $y_k$ in the right side. And the answer with be $\displaystyle 2 \sum_{p, q} \sum_{k = 1}^ n f_k(p,q)$In order to compute $\displaystyle \sum_{p, q} f_k(p, q)$, we only have to computer sum of $k$-subarry of left and right side. donated by $(l_1, \cdots, l_k)$ and $(r_1, \cdots, r_k)$, and then $\displaystyle \sum_{p, q} f_k(p, q) = \sum_{i = 1}^k l_i \cdot r_i$. the origin time complex is $O(n^3)$, but there is a convolution in above computation, so we can solve this problem with time complex $O(n^2 \log n)$ with the help of NFT.
 » 3 years ago, # | ← Rev. 2 →   0 Please help, I want to know where and why O(r) code for nCr mod p fails!!! long long nCr(int n, int r){ if(r>n){ return 0; } int i= 0; int t =r; long long ans = 1; while(t--){ ans = ( (ans*(n-i))/(i+1) ); // ans is always less than p, so it won't overflow, right?? ans = ans%p; i++; } return ans%p; } 
•  » » 3 years ago, # ^ |   0 Division under mod does not work like that.For better implementation see https://cp-algorithms.com/combinatorics/binomial-coefficients.html
•  » » 3 years ago, # ^ |   0 since i + 1 may not divided by ans, since ans = ans % p
 » 3 years ago, # |   0 how to solve problem D if it was given that both p & q should be non-increasing order or non-decreasing order?
•  » » 3 years ago, # ^ |   +6 sort the origin array $a$ and then the answer for both case is $\displaystyle 2 \sum_{k = 1}^n \sum_{j = 2k}^{n + k} \binom{j - 1}{k - 1} \binom{2n - j}{n - k} a_j \quad - \quad 2 \sum_{k = 1}^n \sum_{i = k}^{2k - 1} \binom{i - 1}{k - 1} \binom{2n - i}{n - k} a_i$time complex $O(n^2)$
•  » » » 3 years ago, # ^ |   0 how did you find this
 » 3 years ago, # | ← Rev. 2 →   0 .
 » 3 years ago, # |   0 Can someone please explain why my div1-b solution is failing on test case — 8. Div1-B Solution
 » 3 years ago, # |   0 The answer for the test case (P=557307185726829409 && Q= 746530097)according to the judge is X=1 why cant we have X=674968226.