@noobipy
Check for the number of different character appearing in your string (it can be easily incorporated with help of sets or hash maps). To further reduce your operations, if the character coming just after the character-which-you-are-altering, differs from it by a difference greater than your till best result, you can ignore the rest of the characters for that particular case.
But, this still gives TLE for the task 4 of subtask 3.
@kr002 that is the exact problem I am facing in handling those large numbers:(. What is your exact code complexity ? Well I can reduce mine to n^2 but only if those Big Ints operation take O(1) .
@Kr002 I have done the precomputation thing already to reduce my complexity but it still gives me TLE in second subtask in python.
And as far as wrong ans are concerned I don't think it is is due to overflow because if you are able to store nCr for all values then how can be there an overflow while calculating their powers as we then have to use mod at each step!!!
Don't store it in modulo with prime, python supports BigInts, store it as it is and then try submitting the code and let me know if you pass all the subtasks.
Your system might crashed (didn't responded) because may be you were trying to print that value.Do one thing make a factorial array in which F[i] = factorial of i. Store the factorials for all the numbers till 5000 and then see if your system crashes or not
@ghostrider would the inclusion exclusion principle work?I mean I know it will give the correct answer but it seems as if it would take a lot of time...For each element, I will have to add a total of min(i,n-i) nCr's where n is the number of elements and i is the i'th element's index in array.Would it work?