Unofficial Editorials November Cook Off

Hey @taran_1407 @vijju123 , I have applied same logic for problem SKIING. got NZEC 10 times in java. same solution accepted in cpp. even I have copied already accepted solution of java after contest got over . Got NZEC.

Link to my Solution : https://www.codechef.com/viewsolution/16319005

I don’t Expect this type of judging from Codechef. Used fast Scanner Class for java. Fast Printwriter. Got nothing. my complete logic was correct.

Also I don’t have enough karma to open question for this…
Disappointed.

My Approach for Hasan and boring classes :

As the string we have to find must be a palindrome the condition that must hold is T[i] == T[n-1-i] for all i. So for every such pair (i,n-1-i) there are two cases :

Case 1: S[i] == S[n-1-i]

In this case we can either change both the characters to some other character or do not change any of two character. There are 25 ways to change both the characters. There is 1 way to change none of them.

Case 2: S[i] != S[n-1-i]

In this case we can either change the ith character to (n-1-i)th character or change (n-1-i)th character to ith character or change both the characters to some character other than both of them. If we are chnaging only one of them then there are 2 ways and if we are changing both of them there 24 ways.

Some Mathematics before Moving Further

Suppose there are m balls and n bins. Assume bins are distinct. How to put m balls in n bins such that no bins contains more than k balls. There is easy way to deal with such type of problems. Formulate the question in terms of polynomial. So the polynomial for first bin is (x^0 + x^1 + x^2 + … + x^k). x^0 represents we put 0 balls in the bin,and in general x^k represents we put k balls in the bin.And it will be same polynomial for all bins because the max limit is same for all bins. So now our answer will be nothing but just the coefficient of x^m in polynomial generated by multiplication of polynomials of all bins. So answer is coefficient of x^m in (x^0 + x^1 + x^2 + x^3 + … x^k)^n. . Note this logic can even be extended if we have different max limits for each bin.

Solution to original Question

So as discussed above we will create a polynomial for all (i,n-1-i) pairs and then find coefficient of x^k where k is the hamming distance needed. As in our case k is the max hamming distance possible, actual answer is summation that is coeff(x^0) + coeff(x^1) + … + coeff(x^k). Now the polynomial for case 1 is (x^0 + 25*x^2) Here x^0 corresponds to changing 0 characters and x^2 is changing 2 characters and coefficient describes number of ways to do it. Similarly the polynomial for case 2 is (2x^1 + 24x^2).

So now suppose that there are a pairs of case1 and b pairs of case 2 then multiplied polynomial will be (x^0 + 25x^2)^a * (2x^1 + 24*x^2)^b. So now compute first part and second part individually using combinatorial expansion and then use FFT to derive the multiplied polynomial. Now find summation of coeff(x^0) + coeff(x^1) + … + coeff(x^k) in the multiplied polynomial obtained using FFT. That’s the answer.

Note you have to separately handle for the odd case. Because the just middle character, the polynomial will be (x^0 + 25 * x^1). So answer in this case will be ans(S’,k)1 + 25ans(S’,k-1). where S’ is the string obtained by removing the middle character.

Optimization

If we have to find a coefficient of a single power in multiplication of two polynomials A and B, it is just O(N). But here we have to find k such powers. So it is not feasible to do this way and because of this reason only we are using FFT. But wait we have to find summation of coefficients of first k powers then what we can to do just find the coefficient of x^k in the multiplication of (prefix sum polynomial of A) and B. That is define new polynomial A’ where coefficient of x^k is summation of coeff(x^0) + coeff(x^1) + … + coeff(x^k) in polynomial A. Now we have to find just coefficient of x^k in A’B. Remember to expand A upto x^k by appending zeros and then form A’. You can also use B’A instead of A’B because A’B = B’A.

Intuition behind optimization : Suppose I want to buy at most k apples in total from shop A and shop B. Then you can solve this by fixing the amount of apples to buy from shop B and then loop over that fixed amount. That is (0 apple from shop B,at most k apples from shop A), (1 apple from shop B, at most k-1 apples from shop A), … , (k apples from shop B,0 apples from shop A). So for finding coefficient for at-most in A we are using prefix sum polynomial A’.

6 Likes

@nik_84 . Most probably in your case NZEC is caused because of Stack Overflow Error. Try reading this on how to avoid it : https://discuss.codechef.com/questions/77955/java-stack-size-recursion-limit .

1 Like

Beautiful Logic…

There’s one more solution https://codechef.com/viewsolution/16322126

Failed because of this while same solution in c++ passed.

I was wondering on how people did it using FFT. Nice trick man!!

Before their recent memory changes, i.e. in the age of fairies, demons and dinosaurs (i.e. this Jan,2017), even a recursion depth of {10}^{6} was acceptable in JAVA. But now its something less than {10}^{5}

@aayushkapadia can you please explain “Some Mathematics before Moving Further” this part a little more? how did (x^0 + x^1 +…) comes ? i mean why does the numbers are in powers ?

and also how we are making sure of the fact that the resultant string is palindrome as if any pair with case2 left then the string wouldn’t be palindrome !
i know i am missing something please help .

@vijju123 I tried to run your code for an array of size 500X500 and populating it by randomly generated numbers. It does not fit in time limits. It runs well for size ~200X200. Please correct me if I made some error in your original code. Your code: https://ideone.com/mYM9sw
I think it is because of that extra factor in your time complexity.

Thats because the random test data has 0 in between. I have specifically instructed DFS function to instantly return if it encounters a “0” in array, as 0 means it is going out of bounds (I decalred array of larger sides, this means that if index goes above 1000 or less than 1, it will encounter 0 and instantly return).

If 0 is in array, then my DFS wont even count it visited, it will immediately return. This causes an infinite loop.

Though idk if it will run in time even after correcting this XD

My code’s complexity is O(N*M*X) where X is number of times I will have to repeat those tedious N*M operations of finding maxima, and doing DFS from them.

In worst case, I can visit only 4 more cells from current highest cell, then X= (N*M)/5. But I doubt you need the worst case to time my code out.

My code ran rather quickly- 0.16 seconds only!
(TBH, I submitted it just “for sake of it”. I knew it will TLE, but idk I decided to give a try before wasting more time on that Q :stuck_out_tongue: )

(Now now, dont be so cruel on this poor man. His approach is 90% correct :3)

Oh! I did not see that in your dfs. But it still won’t run without the zeros. If you do not traverse each to find the max element it will work fine I suppose.

@rumblefool Yes, if I store and sort the elements, instead of performing o many operations to find next maxima, it will fit in the time limit. Thanks for working on my solution :slight_smile:

That was just for killing time btw. I was getting bored and saw your comment and thought of testing your code for a larger input. Btw I also got AC solution in SEPT Long Challenge with an n^2 time complexity (n~10^5)that should ideally have failed. But the cases were not so robust after all. All in good spirit! :smiley:

Yup, thank you :slight_smile: . (BTW, I am glad theres no “HACKING PHASE” here like CF…:stuck_out_tongue: XD)

1 Like

FFT seems to apply everywhere in combinatorics. :slight_smile:

Haha… @vijju123

@pk301 You can read about how to solve counting problems using polynomials (generating functions) in this link : https://brilliant.org/discussions/thread/generating-functions-2/ .

For case 2 pairs we are changing 1 character or 2 characters to change it to case 1. And hence its polynomial only contains coefficient of x^1 and x^2.And coefficient describes how many ways are there to change it. Visit the link above to see some example mentioned.

1 Like

Always the unique solutions, with even better references. :slight_smile: @aayushkapadia

1 Like

Thanks @taran_1407.