copy paste Hey guys!! Finally after 10 tiring days the A̶u̶g̶u̶s̶t̶ November long concludes. Lets share our approaches for the problems here while editorials for problems come out (at this moment none n̶o̶t̶ ̶a̶l̶l̶ of them are out)? Got any interesting approach which you cant wait to share? The wait is over :) So, which was your favourite problem? I had best time solving ̶I̶n̶t̶e̶r̶a̶c̶t̶i̶v̶e̶ ̶M̶a̶t̶r̶i̶x̶ ̶:̶)̶.̶ MAGICHF2 ̶T̶h̶e̶ ̶p̶r̶i̶n̶c̶e̶s̶s̶ ̶g̶a̶v̶e̶ ̶m̶e̶ ̶a̶ ̶r̶u̶n̶ ̶f̶o̶r̶ ̶m̶y̶ ̶s̶c̶o̶r̶e̶ ̶x̶D̶ ̶̶ ̶s̶o̶ ̶I̶ ̶l̶e̶t̶ ̶h̶e̶r̶ ̶s̶t̶a̶y̶ ̶w̶i̶t̶h̶ ̶d̶r̶a̶g̶o̶n̶s̶ ̶a̶n̶d̶ ̶b̶e̶ ̶b̶y̶ ̶h̶e̶r̶s̶e̶l̶f̶ ̶t̶h̶e̶n̶ :p But on a serious note, anyone who was able to MAXDTREE s̶a̶v̶e̶ ̶t̶h̶e̶ ̶p̶r̶i̶n̶c̶e̶s̶s̶ ̶(̶o̶r̶ ̶p̶r̶i̶n̶c̶e̶.̶.̶.̶? :p). I kept on getting TLE W̶A̶ for larger TCs, so I appreciate any approaches for it :) P.S.  If someone can write something on interpolation (s)he is most welcomed. I invite @gorre_morre and @algmyr for this task. (:( Tagging here don't send any notification.) I derived some required equations for CHEFEQUA, didn't check the correctness of those equations because I knew even I these are correct then time left for the contest in insufficient to implement those. Let the discussion begin! /copy paste asked 12 Nov '18, 15:53

My Solution (100pts) Since the sequence $C$ is not really restricted to have only $N$ elements, I made a generating function for it. What you get is: $ \displaystyle C_0 + C_1x + C_2x^2 \ldots = \sum_{i=0}^{N1} B_i(1 + A_ix + A_i^2x^2 + \ldots) = \sum_{i=0}^{N1} \frac{B_i}{1A_ix}$ $ \displaystyle \text{Now let } Q(x) = \prod_{i=0}^{N1}(1A_ix)$ If I multiply $Q$ to both sides of this, on the right I get a polynomial of degree $N1$ i.e. $N$ coeffients. Since the first $N$ coefficients of the product depend only on the first $N$ coefficients of the 2 polynomials being multiplied, I can write the right side as $\displaystyle \frac{P(x)}{Q(x)}$ where $P(x) =$ The first $N$ coefficients of $ \displaystyle (C_0 + C_1x + C_2x^2 \ldots + C_{N1}x^{N1})Q(x)$ $\displaystyle \therefore \frac{P(x)}{(1A_1x)(1A_2x)\ldots(1A_{N1}x)} = \frac{B_1}{1A_1x} + \frac{B_2}{1A_2x} \ldots \frac{B_{N1}}{1A_{N1}x} $ This is a partial fraction decomposition and the solution is given by $\displaystyle B_i = A_i\frac{P\left(\frac{1}{A_i}\right)}{Q'\left(\frac{1}{A_i}\right)}$ where $Q'$ is the derivative of $Q$ (cover up method). If we further let $ P^*(x) = x^{N1}P\left(\frac{1}{x}\right)$ i.e. the polynomial we get by reversing the coefficient list of $P$ and similarly $Q^*(x) = x^{N1}Q'(\frac{1}{x})$ we get $\displaystyle B_i = A_i\frac{P^*(A_i)}{Q^*(A_i)}$ We can find out the values of $P^*$ and $Q^*$ at any $N$ points in $O(N\log^2 N)$ using the algorithm described here. An example in case anyone doesn't get it. I'll be using the sample test case for this. $Q(x) = (1x)(12x)(13x) = 16x+11x^26x^3$ $P(x) = \text{First 3 coefficients of } (3+6x+14x^2)(16x+11x^26x^3) = 3  12x + 11x^2$ $P^* (x) = 1112x+3x^2$ $Q'(x) = 6 + 22x  18x^2$ $Q^* (x) = 18  22x + 6x^2$ $\therefore B_0, B_1, B_2 = \text{value of } \displaystyle x\frac{1112x+3x^2}{1822x+6x^2} \text{ at } x=1,2,3 \text{ respectively.}$ answered 13 Nov '18, 11:18
Why you made this as wiki ??
(13 Nov '18, 18:05)
I didn't intentionally make this a wiki. I thought some moderator did that. Strange. Changed it back now. Thanks @aryanc403! Edit: That didn't increase my reputation :
(13 Nov '18, 19:29)
All upvotes done on wiki post didn't add. But new upvotes will increase reputation.
(13 Nov '18, 20:32)
So this is another bug in Discuss then.
(14 Nov '18, 00:27)
Nope, it is not a bug.
(14 Nov '18, 00:39)
Oh. But this still needs to be fixed. Why should upvotes done on wiki post not add to the karma after unwikifying the post?
(14 Nov '18, 00:51)
showing 5 of 6
show all

About CHEFEQUA, I think that @psaini72 solution is great. But here is what I did: When I first saw the problem I recognized that the matrix in the problem is the transpose of something called a Vandermonde matrix, usually denoted $V$. This is a well known type of matrix and from some googling I found On the Inversion of Vandermonde Matrices pretty much telling me how to solve this problem. Note that the inversion formula for $V^T$ contains $V$, which will essentially be just a multipoint evaluation of a polynomial at points $A_0$,$A_1$,...,$A_{N1}$. So all I needed to do now was to implement a multipoint evaluator for polynomials. My favorite source for FFT algorithms are these lecture notes. It shows how to reduce multipoint evaluation to polynomial modulu at a cost of $O(N \log^2 N)$. After looking around I also found these lecture notes on how to implement polynomial modulu in $O(N \log N)$ using convolution based on FFT or NTT. Later on I even found that someone had already implemented this specific algorithm in python. This implementation is probably pretty slow and there are some things that could be done differently so I made my own implementation in C++, still it should be pretty easy to rewrite the python code into a somewhat quick fully working C++ code if you just want to solve the problem. Finally after getting accepted I noticed that I only got a running time of $2.3$ s while some people got around $1.2$ s, so I tried to speed up the algorithm. After playing around with the algorithm for a bit I noted that what slowed everything down was that I used 4 mod operations in the deepest part of my NTT. After some thinking/rewriting I was able to get it down to using a single mod operation, speeding up the algorithm significantly. Currently I'm pretty happy with my implementation, it is both pretty clean, readable and especially the NTT is noticeable quicker than most implementations I've seen. All in all I got it running in under $<1.2$ s. I could probably even get it under $1$ s by reusing some calculations. answered 14 Nov '18, 06:58

"HMAPPY1" first find 2 max size substring and store their first and last pointer and when shift queries come you just move 4 points and for length query you just need to check size of both substring both query thake O(1) time https://www.codechef.com/viewsolution/21397995 "GMEDIAN" Its simple observation you for making odd length subset you have 2^(n1) possible way i proved that with equation (l+r) C (l) way to choose elements in array at (l+1) position where (l+r+1) = n Now for even length we need to check frequency of number and after some observations you can find solutions with O(n²) so total time complexity O(n²) https://www.codechef.com/viewsolution/21483295 "MAGICHF2" If N is even than we need to divide N like after devide N is two part one part still can divide by 2 But for odd we need to add one into it And after logN/K iteration you can find its probability. https://www.codechef.com/viewsolution/21521761 "CHEF and Eq " First i tried with Gaussian elimination in O( n³ ) but after reading some papers i can make in O(n²) Its known as vandermonde matrix. And i read about FFT i think that it will be solutions of it May be using MOD root of unity. I can't solve it for 100 point. O( N²) for more information https://discuss.codechef.com/questions/80978/sata05editorial " MaxDigit " For subtask 1 we just need to know how many paths are passing from node 1 in s using dfs an after than remove node from distance 3 and ( Children of root C 2 ) from ans... I didn't paste partially point solutions you can check For prime tree and Chef and recipe i used brute force approach for partially point I got TLE in BinXor answered 12 Nov '18, 21:29

I am eagerly waiting for Editorial of CHEFEQUA , RECIPIES, and MAXDTREE. I was only able to grab partial points. RECIPES: These type of Halfsecond question are really interesting. (CPCOMP) Last time. One can definitely relate merging of elements. @bciobanu or @gorre_morre anyone else who could enlighten about variants of these kinds of problems would be help to all. I am really eager to know different approach to solve such merging question. CHEFEQUA: A MathMamoth I would say. One could easily figure out 20 point solution using Vandermonde Matrix to solve Linear System of Equation. After some more efforts one could relate it with FFT and Interpolation. The giant Task lies in connecting it question. I spent a lot of time going through different articles and Tutorial about FFT and Interpolation. Unfortunately, I couldn't figure out the solution for question. @taran_1407 expecting a gem from you again. MAXDTREE: One can check the length of patterns with occurrence of 2 using program to generate series and easily deduce that pattern for 2 exist for all, but length 3. I am totally blank about that 100pt approach. Waiting for editorials. An awesome contest, I would say. Learnt a lot ( though couldn't implement everything :P ). answered 13 Nov '18, 00:25
I disagree that CHEFEQUA was a math mammoth especially compared to some other math based questions that I have seen in previous long contests. Maybe you should look at my answer on this thread.
(13 Nov '18, 11:30)

RECIPESI used Gauss Elimination + Bitmasks. My Soln CHEFEQUAI used $O(n^3)$ to find the inverse of a matrix and fetch 5pts. My Soln GMEDIANMy approach was to count Bad Sequences. Only matrices with even length($2*n$) and different element on $n$ and $n+1$ position are bad. My Solns $O(n*logn)$ $O(n^2*logn)$ . I recommend reading $O(n^2*logn)$ first. MAXDTREEI first made a hypothesis that all no which contains only 2 as digits are present. Running it offline gave me 222. Rest all no up to 2222222222 (10 digits) were present in this recurrence. BINSTRMy code is of 415 lines uncommented. I will keep job of explaining soln of this for editorials and other community members. Chef Vijju Corner answered 13 Nov '18, 01:12
BINSTR > query_sort_by_L + right_to_left_pass + min_seg_tree + min_trie_for_each_length + jump_pointers_for_each_string
(13 Nov '18, 05:46)
My soln is an online soln. But because it is very long. I have to reread It so I skipped that. :P
(13 Nov '18, 06:16)

1. For HMAPPY1: I used Segment Tree. Though, it can be done with Bruteforce on Strings. Here is my Code 2. For GMEDIAN: I used combinatorics approach as the naive submission had complexity O(2^n) which can be seen as a pattern. Here is my Code. 3. For MAGICHF2: This was a simple problem. All you needed is a correct direction of moves and evade some edge cases Here is my Code. 4. For BINSTR: I used Trie with Intervals for this problem. The main issue with this problem is different lengths with larger differences between max length and second max length and the larger number of zeros to be appended which I saved beforehand to prevent again and again traversal. Here is my Code. If you feel like I should comment the code, or you have any queries in the code, you are free to ping me here :) answered 12 Nov '18, 17:01
1
even i used combinatorics for GMEDIAN but getting TLE: Can someone help....?
(12 Nov '18, 17:14)
@tushar2899 try to use some naive code as tester for your code. If your code was on the right direction it should not have failed for task 1 atleast. Check your approach and use some naive code for checking your direction of the code. I have included a naive checker below in my code :)
(12 Nov '18, 17:21)

Hey Guys , Can anyone help me with BinaryStrings , I tried making a TRIE and taking maximum of the input length of the string and padding all others with same 0s to make the strings equal , any query string with length greater than this , I took a substring of the the appropriate length (max length that is ), i searched in TRIE as usual and each node with indices which pass through that number were stored , I calculated the one in range linearly , It passed most of the cases but TLED in(5,7,8,14)My solution :https://www.codechef.com/viewsolution/21596350 Can someone please suggest why this is so are the input strings too big such that you cant pad everyother string or is it due to the way i search in Ingervals Side Note : Also there was Sigsegv before for the cases not TLED , did it exceed memory Requirement in anyway ?Any help is appreciated! Thankyou :D:D answered 13 Nov '18, 00:44
@insanity_123 Kindly read the reply I gave to @arpit_2k on this thread below. If you are padding zeros then you are also having bug similar him.
(13 Nov '18, 21:52)
Thanks a ton ! :D I already read your reply though XD
(14 Nov '18, 21:34)

Can anyone tell what I could do to improve my solution to BINSTR (https://www.codechef.com/viewsolution/21589740). I've implemented a radix tree and for input strings of different lengths, I'm padding the shorter strings with 0's initially. My solution gives TLE on tasls 5, 7, 8 but runs in <= 0.08 seconds on all the other. answered 13 Nov '18, 18:38
2
@arpit_2k your solution will not work for following testcase. n=10^{5} q=10^{5} length of First String = 10^{5} length of all other strings = 9 So your code will try to make Radix tree as follows: It will append zeros to make the length of strings = 10^{5} Now this makes the total Number of character to placed in Radix tree = 10^{10} 10^{10} Characters nearly takes 40GB memory. The TLE verdict you got, is actually because of the large number of nodes that are required to create. If you run this code locally on such large input file, The OS will Kill the process because so much Memory cannot be allocated.
(13 Nov '18, 19:30)
Oh. I did not think of that.I guess I'll slightly modify my approach and try to compress those strings or some other modification.
(13 Nov '18, 20:54)

For CHEFEQUA2, the algorithm for inversion of a system with power equations is given in http://www4.ncsu.edu/~kaltofen/bibliography/88/KaLa88.pdf . All that is required is a fast multipoint evaluation algorithm using FFT. answered 17 Nov '18, 12:58

What can be even faster way for this problem "HMAPPY1"
answered 12 Nov '18, 16:23
2
0.14 sec, same with my solution. I don't remember why I wrote so much code, but still fastest Python solution belongs to me :P https://www.codechef.com/viewsolution/21381609
(12 Nov '18, 17:08)
HMAPPY1 can also be solved like this https://www.youtube.com/watch?v=T47EFVOJbgc
(12 Nov '18, 17:43)
1
(12 Nov '18, 18:09)
nice solution joffan, you're even not using something like deque
(12 Nov '18, 19:19)

Please suggest some test case for which my HMAPPY1 solution should fail, because I am only able to pass few test cases, and others are showing wrong answer.I tried all the possible combination of arrays but still many test cases showing wrong answer. answered 12 Nov '18, 17:43
This may help you https://www.youtube.com/watch?v=T47EFVOJbgc
(12 Nov '18, 17:52)

https://www.codechef.com/viewsolution/21550313 My solution to HMAPPY1... segment tree not used.. is based completely on analysis answered 12 Nov '18, 17:57

The test cases for Binary Strings were very weak, I have seen many PYPY solutions where just brute force was applied and they got AC. It just kills the main motive behind the problem and a language advantage is the last thing I want to see on Codechef. I myself was not able to solve the problem and seeing so many people get AC using only brute force is saddening :( answered 13 Nov '18, 00:06

How the heck do you solve RECIPES? The only solution I know is checking if there's a subset XOR = 0, and for this I check for each bitset if there's a subset not having it that XOR is equal to it. Performed Gaussian Elimination, however, there are N equations and K variables maximally, which bring a query's complexity to O(NK^2), and this of course TLE out even on subtask 2. So how did you guys Gauss? What were the equations, and what were the variables? answered 13 Nov '18, 17:21
For RECIPES there is a pretty joyful solution, whereas you would normally map bases to $2^0, 2^1, ..., 2^(10000)$, you now map them to random int [0, 2^64], and proceed normal xor, so probabilistic solution.
(13 Nov '18, 18:14)
Damn, this problem was great! Learnt a lot. Thanks to your tip I have gotten Accepted on it.
(13 Nov '18, 20:18)
You can do without random mapping. In that case, you will have to implement your own bitset class. My Soln
(14 Nov '18, 01:16)
1
How the heck is THAT fast enough? Doesn't make any sense to me.
(14 Nov '18, 09:48)
Haha, nice comment @ducpro :))
(15 Nov '18, 03:57)
No I'm serious. I originally implemented your idea with a complexity of O(Q * K * K * 64) and it already TLEd on subtask 2. That monstrosity of a complexity passing in 0.15 secs is just beyond my understanding.
(15 Nov '18, 08:30)
@ducpro The worst case which I can think of for my code.
(15 Nov '18, 10:36)
showing 5 of 7
show all

I used a double ended queue for solving HMAPPY1, by storing count of consecutive 1's and 0's. Like for case: 1 1 0 0 0 1 1 deque contains: 2 2 2. I found that max value after rotation depends on front and rear too. Link to my solution : https://www.codechef.com/viewsolution/21546305 answered 13 Nov '18, 17:32

Can somebody suggest test cases for GMEDIAN on which my code is failing? https://www.codechef.com/viewsolution/21621610 First I found the number of odd subsequeces by: 2^(N1) I have used following variables: l=length of contiguous subsequence containing elements less than that number r=length of contiguous subsequence containing elements more than that number d=length of duplicate string Then,I have choosen the duplicate subsequences containing of even length and then choosen equal elements from left and right side answered 14 Nov '18, 11:01

//can anyone explain me ? what is the concept behind goodmedian question.. how to solve it for 100 points ? answered 15 Nov '18, 00:37

I so wanted to get 5 points from CHEFEQUA. I understood it requires something like FFT but still tried hard with Gaussian elimination, then tested my solution with n=10 custom test and it failed. FFT solutions looks too scary but I guess it's time to familiarize with it.
Considering I didn't compete this time I'll let gorre_morre make comments on CHEFEQUA if he wishes to. :P
I know he had a blast with the problem. It boils down to speeding up polynomial calculations with NTT for quick convolutions and some cleverness, both of which are up his alley.
His last submission on the problem has rather clean code, so it's worth looking at: https://www.codechef.com/viewsolution/21514506
Gaussian elimination trick gives 5pts for CHEFEQUA. My Soln
CHEFEQUA was 2x multipoint evaluation i think, i guess in $O(NlogN^2)$ standard approach with modulos of polynomials with FFT division, though it was too hard to code for me. One multi point is for some kind of convolution with vector C (can google solving transpose vandermonde system), other one is in derivative of $(xx_1)(xx_2)..(xa_n)$, since those are actually denominators of Lagrangian basis polynomials. Pretty hard i would say, and doable in few hours if you have done fft divisions before and can google good. NTT*