Hi,
There was a contest held named Code Stumble and for one of the question I was unable to get an accepted solution and I can’t understand why. I compared some of the AC submitted code and tried random test cases on both of them and found out that all the answers are same. Can anyone help me as to where possibly I could have gone wrong.
Link to the question : https://www.codechef.com/COMB2018/problems/COMB1/
My logic for the question was that Sum of odd numbers from 1 to 2n is (n - 1)squared:
eg. 1 + 3 + 5 = 9 which is 3 squared. 1 + 3 + 5 + 7 = 16 which is 4 squared.
For a series not starting at 1 simply subrtact the smaller square : (1 + 3) + 5 + 7 = 16, Subtrating bracketed terms is 16 - 4 or 4^2 - 2^2 = 12. Generally: a…b odd integers, Sum is ((b+1) / 2)^2 - ((a-1) / 2)^2, both a and b included in sum.
Where a and b were calculated as a=2l-1 and b=2r-1
Link to my Solution : https://www.codechef.com/viewsolution/22501845
Thank you in Advance.
It is just a simple AP, the answer is just r^2 - (l-1)^2 for each test case.
I get your point but I have also done the same thing though in a more complex manner. The thing which I can’t understand is that why am I getting WA for my solution.
@abhi2402 If I simplify my formula, it becomes exactly the same as yours.
Try declaring the variables outside the loop, declaring inside might exceed the memory limit constraint.
It is being caused due to the pow() function, I just tried out a random case-
1
786667886 986789345
For this case your code is giving WA and I checked that the pow() function is giving incorrect value, I have seen this error occur sometimes though don’t really know the actual reason behind it. Just remove the pow() function and it will get accepted.
1 Like
@abji2402 , thank you so much for finding out the error. It would be great if anyone could give the reason behind this.
@vijju123 @admin
I don’t think that this is the issue with the solution.