### ajit's blog

By ajit, history, 3 years ago,

We hope that you have enjoyed this round! Here is the sketch of solutions for the problems.

### A. Array and Peaks

• Problem Setter: ajit
• Editorialist: ajit
Tutorial
Implementation in C++

### B. AND Sequences

• Problem Setter: ajit
• Editorialist: ajit
Tutorial
Implementation in C++

### C. Add One

Tutorial
Implementation in C++

### D. GCD and MST

• Problem Setter: ajit
• Editorialist: ajit
Tutorial
Implementation in C++

### E. Cost Equilibrium

Tutorial
Implementation in C++

### F. Swapping Problem

Tutorial
Implementation1 in C++
Implementation2 in C++
• +153

| Write comment?
 » 3 years ago, # |   +1 Auto comment: topic has been updated by ajit (previous revision, new revision, compare).
 » 3 years ago, # |   +41 Thank you for the contest, I really enjoyed it.
 » 3 years ago, # |   +22 In problem C, I faced lot of issues when I did not use fast input "ios_base::sync_with_stdio(false); cin.tie(NULL);" 112714905 — here I took input number as a string (to iterate over it's digits) and even when I used integers only (112715192) even then also, It got TLE verdict.Has it become mandatory to use FAST I/O to pass the verdict now?
•  » » 3 years ago, # ^ |   +37 I think it has already been a standard that you should always use fast IO.
 » 3 years ago, # | ← Rev. 4 →   +10 Solved C by direct simulation with pre-processingBasically just brute-force the numbers gotten when you add 1 to every digit starting from 0. The numbers can't be stored because they get large very quickly. To fix this, store the count of each digit in the current number in an array instead and update the array to the next number after applying the operation accordingly. The sum of digits of the current number(we only care about the sum) are stored in a vector for pre-processing. Then for each digit of n, apply the operation m times, which is done in O(1) because of pre-processing. Total time Complexity: O(t*number of digits in n)https://codeforces.com/contest/1513/submission/112728116Lol, the updating can be done with only one array by updating backwards. Was too braindead to realize...
•  » » 3 years ago, # ^ |   0 Hi, I tried a similar solution, and as far I understand, I shouldn't be getting a TLE, but I still get one on test case 3, could you kindly tell me what is missing in this? https://codeforces.com/contest/1513/submission/112745133
•  » » » 3 years ago, # ^ |   0 As I can see, you are are simulating the whole procedure in each test case. This would take at max (no. of test case)*(m) i.e., 4e10 (worst case) operations which would obviously give TLE. Instead store answer for each digit from 0....9 and for each value of m using simple DP, before the test cases and then access them in O(1) in each test case.
•  » » » » 3 years ago, # ^ |   0 Yeah, usually I consider each test case as an isolated environment. Thanks for the reply, I shall try it out.
•  » » » » » 3 years ago, # ^ |   0 Think, If you have digit D and you want to increment it let's say X times then how much length it will increase. You can store it in table[D][X](Try to Precompute this table). If you don't get the solution, then you can see mine.
 » 3 years ago, # |   +28 F can be solved using an $O(n^2)$: 112699481.
•  » » 3 years ago, # ^ |   0 Could you explain your approach ?
•  » » » 3 years ago, # ^ |   0 Well, do like the editorial solution, but in the last step just do a brute force loop and rely on auto-vectorization to cut your time down to just under TL.
•  » » 3 years ago, # ^ |   0 How did this pass the time limit?
 » 3 years ago, # |   0 In the explanation of Problem B, Why do bi needs to be super mask of b1?
•  » » 3 years ago, # ^ |   0 I'll explain my thought process maybe it will help you. When $i=1$ only $B_1$ will be on the left side. And when $i=n-1$ only $B_n$ will be on the right side. Therefore it's clear that $B_1=B_n$ condition must hold.For $2 \leqslant i \leqslant n-2$ there will be some mid elements on the left and some will be on the right. But in every case their bit-wise AND should be equal to $B_1$ (or $B_n$ as they are equal). TO keep the bit-wise AND constant, $B_i$ needs to have all the set bits of $B_1$. If not the new AND will have some bits set to zero where it wasn't before. What they have on other bits doesn't matter because in $B_i$ and $B_n$ those bits are set to zero and $B_1$ is always on left and $B_n$ is always on right. Thus their AND will be zero anyway This means that $B_i$ has to be a super mask of $B_1$
•  » » » 3 years ago, # ^ |   +1 Sorry I still don't get it. I understand why $B_i$ has to be a super mask of $B_1$.But why choosing two of the minimum of the array $a$, and the rest of the array will automatically meet the "super mask" requirements?For example, a = [2,4,2]. min(a) = 2(and count(2) >=2), but 4 & 2 != 2
•  » » 3 years ago, # ^ |   +3 So that then only prefix and operation and suffix and operation for all i from 1 to n-1 will hold good , as long as those minimums are present at 1 and nth position
 » 3 years ago, # |   0 for question F:i guess this is correct: first : let vc[i] = {b[i] — a[i], i}; second: sort vc; ans = sum = segma(llabs(vc[i].first)); third: enumerate vc[0].second whith i from 1 to n, let ans = min(ans, sum — diff); last : enumerate vc[n — 1].second whith i from 1 to n, let ans = min(ans, sum — diff);it can pass all test, but i cannot prove it, who can give a prove?
 » 3 years ago, # |   0 Can someone tell me what is wrong in this submission for question B 112733641
•  » » 3 years ago, # ^ |   +5 The code if (v[i]&mn != mn) is equivalent to if (v[i] & (mn != mn)). Check C++ operator precedence .
•  » » 3 years ago, # ^ | ← Rev. 2 →   +1 try input 1 1 2 1&1&2 = 0, but the number of 0 is 0 so there are no valid permutation
 » 3 years ago, # |   +7 Great contest.
 » 3 years ago, # |   0 In Editorial of D — why is checking if the new_element has edges enough to determine if a cycle will be formed?
•  » » 3 years ago, # ^ | ← Rev. 2 →   +11 Hi, I have updated the solution slightly. Please have a look at it. We are spanning to left and right. Suppose we are currently spanning right and currently encountered a new_element which has an edge. First observation is that this edge is between new_element and an element with another index greater than it. Now what I meant is that, we will connect the edge between g and new_element and stop spanning in this direction (here right). This is because we just connected our current span group (current connected component) with some previous span group (another connected component) right? Any further entry we consider in this direction will form a cycle. You can have a look at the code for further understanding.
•  » » » 3 years ago, # ^ |   0 Ya ,now I get it. Thank you
•  » » » 2 years ago, # ^ |   0 Hi. I implemented this solution to end up getting WA. My approach is similar. Just going left and right to each index 'i' as long as I have multiples of a[i] (which would mean a[i] is the gcd) And for edge I am just taking min(a[i], p). Not able to find a case where it would fail. Could you check if you can figure out what's wrong. Also comp is the number of connected components. So at the end I am adding (comp — 1) * p to the ans. https://codeforces.com/problemset/submission/1513/137968697 I know this is an old problem but see if you can recall.
 » 3 years ago, # |   +3 For problem C I use A[i] to represent the occurrences times of number i. So we can construct anonther matrix B to represent the operation and we can multiply B m times to get the answer.My problem is that can I improve the complexity which is O(t * 10^3)[10 is the size of B] because B is a sparse matrix.The test code is https://codeforces.com/contest/1513/submission/112741598
•  » » 3 years ago, # ^ |   0 Probably a little late response — I did the same approach.Submission 115852637 is my Time Limit Exceeded on test 2.The simple fix was to first sort all the queries by $m$ increasingly, and update only by the power of difference (ie. keep the previous matrix power used in a variable and multiply by difference in $m$ between queries; in other words, if previous matrix was $P = M^{m_1}$ you just multiply it by $Q = P \cdot M^{m_2 - m_1}$). Lastly, just sort queries back to their original order and output the answer.Submission 115853823 is where I applied this optimization and it worked :D
 » 3 years ago, # |   0 Great contest with awesome problemsets.Problem C just refreshed my memory of dynamic programming.I solved problem C using two dimensional dp table using memoization approach.My submission 112676785
•  » » 3 years ago, # ^ |   0 can you explain the recurrence relation? Isn't dp[i][5]=dp[i-1][4] true? Why is it dp[i-1][6]? pls explain
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 dp[i][j] is defined as if we has number i and we apply exactly j moves than what is the total number of digits in the lastly generated digit.if i-1 is not 9 :than you can see that number of digits in(i-1)=number of digits in (i)so we can say that if i-1 is not 9 than applying one operation does not increase number of digitsthis implies dp[i-1][j]=dp[i][j-1]applying above thing dp[i-1][6]=dp[i][5] if i-1 is not 9and dp[i][5]=dp[i+1][6] if i is not 9so this clearly answers your query that dp[i][5] isn't dp[i-1][4] and dp[i][5]=dp[i-1][6] with constraints on values of i.
 » 3 years ago, # |   +51 My understanding for F:1. Plot all A_i and B_i on a number line.2. If A_i < B_i, write a right arrow A_i -> B_i.3. If A_i > B_i, write a left arrow B_i <- A_i.4. The answer is original_answer — 2*(the maximum length of overlapping two opposite direction arrows).(Continue to the editorial)By the way, thanks for the great contest!
 » 3 years ago, # | ← Rev. 2 →   -13 ...
•  » » 23 months ago, # ^ |   0 do anything a2oj or cf problemset but do it rating wise
 » 3 years ago, # |   +18 In problem E, how to prove that array will be balanced when this condition holds true: All the source vertices are before the sink vertices in the permutation
•  » » 3 years ago, # ^ |   +3 I would also like to see prove that there aren't any other conditions.
•  » » » 3 years ago, # ^ |   0
•  » » 3 years ago, # ^ | ← Rev. 3 →   +10 I accidently found the proof when I was trying to prove it wrong. So here it is: Lets denote $U_i$ as some value needed at some sink and $V_j$ as some extra value we can take from some source. Lets say positions of sinks are $x_1, x_2, ..., x_m$ and positions of sources are $y_1, y_2, ..., y_n$. Lets say $y_j > x_i$ for $1<=i<=m$ and $1<=j<=n$. Lets denote $w_{ij}$ as some value we take from some source $i$ and give it to some sink $j$. So, $cost = y_1*(w_{11}+w_{12}+...+w_{1m}) - x_1*w_{11} - x_2*w_{12} - ... - x_m*w_{1m} +$ $y_2*(w_{21}+w_{22}+...+w_{2m}) - x_1*w_{21} - x_2*w_{22} - ... - x_m*w_{2m} +$ $.$ $.$ $.$ $y_n*(w_{n1}+w_{n2}+...+w_{nm}) - x_1*w_{n1} - x_2*w_{n2} - ... - x_m*w_{nm}$ Now $V_a = w_{a1}+w_{a2}+...+w_{am}$,So $cost = y_1*V_1 - x_1*w_{11} - x_2*w_{12} - ... - x_m*w_{1m} +$ $y_2*V_2 - x_1*w_{21} - x_2*w_{22} - ... - x_m*w_{2m} +$ $.$ $.$ $.$ $y_n*V_n - x_1*w_{n1} - x_2*w_{n2} - ... - x_m*w_{nm}$ And $U_b = w_{1b}+w_{2b}+...+w_{nb}$,So finally, $cost = y_1*V_1 + y_2*V_2 + ... + y_n*V_n - x_1*U_1 - x_2*U_2 - ... - x_m*U_m$ Now, we can see that cost doesn't depend on $w_{ij}$ for any $i$ and $j$. So cost is constant no matter how we transfer values from sources to sinks. Hence proved.
•  » » » 3 years ago, # ^ |   0 Thanks for the detailed proof! However, I think this covers only one part of the problem, i.e. proving that sinks-after-sources or sources-after-sinks will always have a unique cost. Can you formally prove that permutations other than sinks-after-sources/sources-after-sinks will never have a unique cost? I could only come up with a very informal, rather intuitive proof.I feel this should have been covered in the editorial itself, instead of plainly writing down the formulae. On another note, shout-out to liouzhou_101's amazing, detailed af editorial of Round #700's Problem D/B1.
•  » » » » 3 years ago, # ^ | ← Rev. 2 →   0 First we come up with a conjecture, then we try different examples so the conjecture is not wrong and then we move to the proof. So if you can think of an example to prove it wrong then you don't need to prove.
•  » » » 3 years ago, # ^ |   0 Very Nice Mathematical proof!!
 » 3 years ago, # |   +15 PermutationForces
 » 3 years ago, # | ← Rev. 2 →   0 Would someone mind to explain to me why my submission for C got TLE? Its time complexity is O(10 * m), so I thought it would pass under 1 second. My submission 112747375
•  » » 3 years ago, # ^ |   0 Indeed complexity is $O(10 * m)$. But there are $2 * 10^5$ test cases as well. So, final complexity is $O(10 * m) * 2 * 10 ^ 5$which will surely TLE
•  » » » 3 years ago, # ^ |   +5 My bad, I thought the sum of m over all test cases does not exceed 2e5. This time there is no this line, thank you for pointing it out xD
 » 3 years ago, # |   0 length would be the sum of i−9 operations and i−10 operations, why? Problem C
•  » » 3 years ago, # ^ |   +6 If you look carefully every single digit from "1 to 8" produce one new digit after 10 moves, expect "9" which produce two new digits. Thus, number of digits doubles up after 10 moves, if they don't have "9" in it... we need to add the digits contributed by "9", this is easy, number of 9 in number $x$ $=$ total no of digits in $x + 1$ — total no of digits in $x$$dp[i] = 2 * dp[i - 10] + dp[i - 9] - dp[i - 10]$which is equals to$dp[i] = dp[i - 10] + dp[i - 9]$
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 also can you please explain why dp[i] is taken as the length to the operation applied to number '10'?why are we considering only 10?I got it.Thanks :)
•  » » » » 4 months ago, # ^ |   0 10 refers to the i + m , if i+m < 10 ,then the length is 1
 » 3 years ago, # |   +8 Problem C can be solved Using a Simple 2D DPwhere DP[digit][times] indicate the length of digit after times number of operations and digit E (0, 9)and the answer for a query like 1057 16 would be dp[1][16] + dp[0][16] + dp[5][16] + dp[7][16] and take MODULO'S AS NEEDEDheres the code https://codeforces.com/contest/1513/submission/112697582
•  » » 3 years ago, # ^ |   +1 That is one of the cleanest dp approach one can think of. Definitely beats the editorial.
•  » » 3 years ago, # ^ |   +2 Way better than the editorial which has no explanation.
•  » » 3 years ago, # ^ |   +1 your dp approach is easy to visualize as compared to the editorial's thanks for sharing :)
•  » » 3 years ago, # ^ |   0 can you explain the recurrence relation? Isn't dp[i][5]=dp[i-1][4] true? Why is it dp[i-1][6]? pls explain
•  » » » 3 years ago, # ^ | ← Rev. 3 →   0 Please provide some information as to where you got that dp.Its not in my code
•  » » 3 years ago, # ^ |   +6 Can be converted to 1D dp as each digit can be brought up to 10. So, just calculate for dp[10][times]
•  » » » 3 years ago, # ^ |   0 Can you explain how the dp relation i-9 and i-10 for dp[i] as in editorial.
•  » » 3 years ago, # ^ |   0 Hey, your solution is great. Can you elaborate more on the last else statement when we are dividing $N$ in $0$ and $1$ . Is it due to the fact that we are calculating dp from small digits to the large, As at that time we would have already calculated dp for smaller no. ? Like if we say we take $N = 11$ or more.. then its not gonna divide in 1 and 0. I think, got the intiution. But can you confirm the above logic ?
•  » » » 3 years ago, # ^ |   0 thats because of this if n==8 then in next step it only becomes 9 hence dp[n][times] = dp[n+1][times-1]but for n==9 it becomes 10 (1 and 0) hence dp[1][times-1] + dp[0][times-1]observe how after 9 dfs now solves for dfs(1, times-1) and dfs(0, times-1) so dfs(n, times) n would only be from 0 to 9 and would never exceed 9 and hence no need to solve for n==10 and n==11 as these are non existent.cheers
•  » » 3 years ago, # ^ |   0 absolute beauty , same approach just missed % mod to add , thanks dude!!!
•  » » 3 years ago, # ^ |   +1 broooo that was neat!!!
 » 3 years ago, # | ← Rev. 2 →   +1 My idea for problem B: So we have to find some elements that we can put at start & end. We will check something for all elements bit by bit. Let's say we have a set which contains potential candidates for start & end and we have to remove some candidates. And we are checking for $kth$ bit. There are three cases possible: 1. All elements have $kth$ bit set. 2. All elements have $kth$ bit unset. 3. Some elements have $kth$ bit set and some have unset. Now for case 1 & 2, we have to do nothing. And for case 3, we can see that we can't let the first element of permutation have $kth$ bit set and similarly for last element. So we will remove all the candidates which have $kth$ bit set. We will do this for all the bits. Note that we have all the elements from $1$ to $n$ in our set initially. After doing this for every bit, lets say size of our set is $x$. So, for choosing and permuting first & last element we have C(x,2)*2 ways. And permuting the remaining elements will give us (n-2)! ways. So total ways: C(x,2)*2*(n-2)! My submission
 » 3 years ago, # |   +1 it lookes like codechef helped our problem maker. lol
 » 3 years ago, # | ← Rev. 2 →   0 Ignore.
 » 3 years ago, # |   0 In problem E "ways filling (n−src−snk) identical values in (src+snk+1) places)" should be equal to Binomail(src+snk+1,n-src-snk). But it is getting me wrong answer but when i use Binomial(n,src+snk) my got accepted. Can anybody explain?
 » 3 years ago, # |   0 The complexity of E should be O(nlogn) because of when we count the frequencies of every number cost us nlogn times(using the STL map or just sort).
•  » » 3 years ago, # ^ |   0 Unordered map works in O(N)
•  » » » 3 years ago, # ^ |   0 Oh you're right, thx for reply
 » 3 years ago, # |   0 Guys, What does super mask means??
•  » » 3 years ago, # ^ |   +4 Assume that there are two numbers A and B such that B is a submask of A. Then in such case A is a supermask of B. Generally speaking, a supermask of a number is a number of which current one is a submask.
•  » » » 3 years ago, # ^ |   0 Thank you!! I got it
 » 3 years ago, # | ← Rev. 2 →   0 simple solution for problem Dthis is what i have done Maintain a list of edges with there weightfirst push n-1 edges between neighboring nodes (For Ex) a1 a2 s3 a4 (given array) then push (1,2,p) (2,3,p) (3,4,p) in our edges, where first 2 numbers are indices and last one is weight the observation is GCD(in a range) == min (in a range) if an only if min divides every other value in the rangeFor example say one of the subarray is this 9 6 3 3 9 6 array 1 2 3 4 5 6 ind then min is 3 and it divides every element so push(3,1,3) ## here 3 is the index of element 3 and 1 is index of 9 and last 3 is (gcd weight) and push(3,2,3) (3,4,3) (3,5,3) (3,6,3) what my code would do is push them in 2 iterations in forward iteration push (3,4,3) (3,5,3) and (3,6,3) and in backward iteration push the restOriginally u had n-1 edges this forward and backward iteration may push 2*(n-1) edges more so total 3*(n-1) edges
 » 3 years ago, # |   0 I can't understand why the number of edges considered in problem D would be O(N) ? We are also rejecting the edges forming a cycle which takes O(1) time with DSU. Can somebody please help me understand.
•  » » 3 years ago, # ^ |   0 So are you referring to my code.
•  » » 3 years ago, # ^ |   +3 since total edges already there is n-1 then in 2 (forward and backward iterations i at most push (n-1)edges in each iteration)total 3*(n-1) which is O(n) edges for cases like 10 10 10 10 10 (array) (total edges will be 3*(5-1) i guess) u can simply print edg to verify now Coming to DSU Part Just Search Minimum Spanning Tree algorithm based on UnionFind on youtube.I hope this clears it.
 » 3 years ago, # |   -19 Simple Solution Problem BWhat is asked in the problem is this Count all permutation such that binary and of any prefix would match the binary and of remaining suffixFor Example Say array elements are A1 A2 A3 A4 A5 A6 A7 so lets say one of the prefix is A1 A2 A3 then A1 & A2 & A3 must equal A4 & A5 & A6 & A7 this must be true for all prefix and resulting suffix with length greater than 1 lets just call them prefAnd and SuffAnd since prefAnd == SuffAnd Therefore prefAnd & SuffAnd == prefAnd == SuffAnd since and of 2 same numbers is the same number but prefAnd & SuffAnd == FUllArrayAnd so just find the all array and; say this comes out to be Anow every permutation must begin and end with A since there can be prefix of size 1 and this must equal prefAnd == A similary there can be a prefix of size (n-1) leaving suffix of size 1 and this has and values as SuffAnd == Anow you can show that any arrangement in the middle cannot affect any prefAnd and SuffAndFor Example given array 1, 3, 5, 1 whole array and is 1 and hence it should always begin and end in 1 and middle part can be juggled as you wish giving (n-2)! for the mid Proof of why the prefAnd wouldn't change observe any prefAnd >= wholeArrayAnd (since a&b <= min(a,b) and just generalise this by taking more numbers) NOTE: WHOLE ARRAY AND IS THE SMALLEST NUMBER IN THE ARRAY (IF NOT PRINT(-1)) WE WILL USE THIS FACT LATER.observe only 1&1 yield rest (0&1 0&0 1&0) yield zero lets look at our example 001 (1) 011 (3) 101 (5) 001 (1) 001 (whole array and) (observe if ith bit is on here then it is on in every number) so all the numbers have atleat all the bits on as in (A or wholeArrayAnd)now suppose 1 3 1 5 is given we arrange 1 number as 1 1 _ _ _ now suppose we put 5 then 1&5 will always be (A == 1 == wholeArrayAnd) since both 1 and 5 will have all the bits open as in wholeArrayAnd the And will atleast be WholeArrayAnd or A so FirstElement & SecondElement >= WholeArrayAnd (Eq 1. )again since a&b <= min(a, b) so (FirstElement & SecondElement) <= min(FirstElement, SecondElement) = FirstElement = WholeArrayAnd (Eq. 2) (since FirstElement == WholeArrayAnd and WholeArrayAnd is the smallest)from equation 1 and 2 we have FirstElement & SecondElement = FirstElement so that does it for any 2 elements at the beginningfor 3 elements just replace the first 2 elements by their binary and which again is FirstElement as shown and case of 3 elements at the beginning will be the same as case of 2 elements.Here you go prefAnd would never change irrespective of arrangement in the middle.if Prefand doesnt change then suffAnd wouldn't change either as wholeArrayAnd is constant. so arrange at begging and end is C(A, 2)*2 (multiply by 2 since we are arranging indices and not element values) and middle permuation will be (n-2)! Answer is 2*(A,2)*(n-2)! % MOD;cheers
•  » » 3 years ago, # ^ | ← Rev. 4 →   0 Here is my code Spoiler from collections import Counter LIM = 2*10**5 + 10 f = [1]*LIM MOD = 10**9 + 7 for i in range(1, LIM): f[i] = i * f[i-1] % MOD def solve(A, n): And = A[0] for it in A[1:]: And &= it C = Counter(A) c = C[And] if c<2: return 0 ans = c*(c-1) * f[n-2] % MOD return ans for case in range(int(input())): n = int(input()) A = list(map(int, input().split())) ans = solve(A, n) print(ans) 
•  » » 3 years ago, # ^ |   0 Don't know why you got so heavily downvoted. Your explanation was very clear!
•  » » » 3 years ago, # ^ |   0 world works differently i guess
•  » » » » 3 years ago, # ^ |   +3 this is better explanation than editorial...thank you r-tron18
 » 3 years ago, # |   0 Please help regarding my submission of problem c.. my submission is https://codeforces.com/contest/1513/submission/112704547 looking at the time complexity it looks fine to me... please explain if any changes are required
•  » » 3 years ago, # ^ |   0 ios_base::sync_with_stdio(); Maybe you mean: ios_base::sync_with_stdio(0); Actually the first one is as same as none. It got TLE because of slow IO.
 » 3 years ago, # |   +19 I did really bad during the contest.I got wrong attempts in every problem of the first three ones, and I did't solve D in the contest.
•  » » 3 years ago, # ^ |   0 PS: It's my hair which dropped during the contest.
 » 3 years ago, # |   +7 Problem D is just amazing. Tried something with 3 segment trees, binary search and lazy propagation but did not work out. The solution idea in the editorial is really cool. I wonder how you thought about such a problem idea xD.
•  » » 3 years ago, # ^ |   0 Hey can you elaborate on the editorial? I am still trying to upsolve this.
•  » » 3 years ago, # ^ | ← Rev. 2 →   +3 I had the same idea and managed to implement it during the contest (Instead of 3 segment trees, I used 2 sparse tables and 1 segment tree) CodeI felt really stupid after reading the editorial xD and felt I did a really big overkill
 » 3 years ago, # |   0 For $D$, why does the following approach give wrong answer 1. Join $i$ and $i - 1$ with edge weight of $p$ for all $2 \leq i \leq n$ 2. Pick up the minimum unvisited element, call it $minVal$ at $minIndex$. Start from this value and do dfs to the neighbours if and only if they are multiples of $minVal$. Connect $minIndex$ to this vertex, with edge weight $minVal$. Repeat for the next unvisited minimum Now, run "kruskal-algorithm" on this graph to find the minimum spanning tree. Here is my submission
 » 3 years ago, # |   +1 Such a pathetic way to write problem description for Problem B.
 » 3 years ago, # | ← Rev. 6 →   +14 I find a different way of problem C, link it to Pascal triangle, but unable to implement it, my submission failed testcase n = 98, m = 100, see the picture of my approach and the wa submission : https://codeforces.com/contest/1513/submission/112781701 . I would appreciate it if anyone could help!
 » 3 years ago, # | ← Rev. 2 →   0 Problem E was really fun to solve! Thank you.
 » 3 years ago, # |   0 In problem E, can somebody prove the 3 and 4 or explain why these conditions are sufficient ?
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 see this. You could have gone through the comment section before asking this.
 » 3 years ago, # |   0 For problem E, shouldn't it be $Binomial(n, src + snk)$ in the third to last line of the editorial? (Since we are using stars and bars basically?)
 » 3 years ago, # |   0 Can anyone please explain (cnt⋅(cnt−1)⋅(n−2)!)%(109+7). Why do we have to do this?
•  » » 3 years ago, # ^ |   0 It's the number of ways to choose 2 elements among those who have minimal value which need to be at the start and the end of the array multiplied by the permutation of the other $n - 2$ elements. In other terms, we're doing: $Binomial(cnt, 2) * 2 * (n - 2)!$, where $cnt$ is the frequency of the smallest element.
 » 3 years ago, # | ← Rev. 2 →   +8 nice contest.
 » 3 years ago, # |   0 in editorial of problem E how did we get C=(# ways filling (n−src−snk) identical values in (src+snk+1) places) = Binomial(n,src+src) could someone explain or elaborate?
•  » » 3 years ago, # ^ |   0 See bars and stars:To place N identical values into K places (with potentially multiple items in each place), you think of it as partitioning the N items into K places using K-1 partitions. So think of placing N items along with (K-1) partitions down in a row, which is N+K-1 total things. You'll choose the spots of the K-1 partitions, and there is: Binomial(N+K-1, K-1) ways to do it.Then plug in N = n-src-snk, and K=src+snk+1
•  » » » 3 years ago, # ^ |   0 oookkk . yeah thanks for the clarification.
 » 3 years ago, # |   +13 Alternative solution for 1513F - Swapping Problem: let $i$, $j$ be the indices of the swapped elements. Fix the signs of $a_i - b_j$ and $a_j - b_i$. With a merge sort tree (or a BIT of vectors), you can find, for each choice of the signs and for each $i$, the valid $j$ and, among them, the optimal one (the one that maximizes $|a_i - b_i| + |a_j - b_j| - |a_i - b_j| - |a_j - b_i|$). Complexity: $O(n \log^2(n))$. 113102167
 » 3 years ago, # |   0 Problem C can probably be in O(logm) time. It is matrix powers.
 » 3 years ago, # |   +3 While trying to hack solutions in 1513F - Swapping Problem, I got a number of unexpected verdicts. From what I understand, it happens when the problem authors' solutions are inconsistent.Can problem setters please resolve that? Thanks in advance.
 » 3 years ago, # |   0 In problem E, in the first example with array $[1,2,3]$ the notes say the total cost must be 2, but consider the following transformations: $[1,2,3] \stackrel{3\rightarrow2,x=2}{\rightarrow} [1,4,1] \stackrel{2\rightarrow1,x=2}{\rightarrow} [3,2,1] \stackrel{1\rightarrow3,x=1}{\rightarrow} [2,2,2]$The total cost is $6$ ($2$ for each step) and as far as I can tell I followed the rules. Am I missing something?
 » 3 years ago, # |   0 My idea for D was exactly the same as Editorial's. Somehow I failed 3rd test case and couldn't figure out why.Please help. Code
•  » » 3 years ago, # ^ |   +3 Hey, did you find the bug ? I had the same idea for this and my implementation even resembles yours. My Submission
•  » » » 3 years ago, # ^ |   +3 Unfortunately, I couldn't figure it out. If you do, please let me know.
•  » » 3 years ago, # ^ |   +3 One of my friends was having the same difficulty and their output was same as yours. We found a case — 14 756 30 5 2 Correct output — 86The mistake was not visiting 30 again as it was already visited before by 5. However, I checked your latest code and it gives the right output for this case, but a mistake I found in it was that you were not marking done for the current element, what I mean is that you should do done[index] = true as well, since it can give you wrong output in case of elements having same value as an edge can be counted multiple times. I hope I explained well what I meant to say. Hope this helps!
 » 3 years ago, # |   0 i was just reading the editorial for problem C-Add one it was hard to understand but it is such a beautiful solution after u understand it i wanted to write it out so for all the numbers for 0 to 9 in n we can take it to 10 if `(10-s[i])
 » 4 months ago, # | ← Rev. 3 →   0 For F, there's a simple approach that's only around ~60 lines of code. I cannot verify if it actually works for all test data, but it gets accepted.Basically, only look at "extreme" indices: Look at the indices that are amongst the 2500 smallest/largest elements in $a$. Look at the indices that are amongst the 2500 smallest/largest elements in $b$. Look at the indices that are amongst the 2500 smallest/largest elements in the vector $abs(a[i] - b[i])$. Try swapping all pairs of "extreme" indices and see which one yields the best result.215878392
•  » » 2 months ago, # ^ |   0 Very late but here's your hack.
 » 3 months ago, # | ← Rev. 2 →   0 I am unable to understand whether D is a nice question or not. Nevertheless the authors done a brilliant job in abstracting the simple logic by using $gcd,min,range$.But the abstraction doesn't feel good and thats what makes to decide hard, whether its good or bad ?Note: The above comment is only my opinion on quality.By the way I'm newbie.