modulus and long doubt

Hey ,
Check this problem : https://www.codechef.com/COKS2016/problems/BHMCHOC
sol 1: https://www.codechef.com/viewsolution/9682592
sol 2: https://www.codechef.com/viewsolution/9646388
I have no clue why sol 1 passed and sol 2 failed . How does (long) make a difference ?

The thing is when you are typecasting ((dp[j]+dp[j-1])%(mod)) as (long) and then storing it in dp[j], then the result stored in dp[j] has much more precisions. Therefore every time you do this ((dp[j]+dp[j-1])%(mod)) you have much more precisions to work with when you use dp[j] next time. Hence, the outputs are different for modulus operations. Hope this helps.

2 Likes

I think this may help.

7 Likes


I believe this is the reason.

3 Likes

@sanket407 the two solutions are completely equivalent. There is no problem with type conversion whatsoever. (long) does not make any difference because the value being explicitly cast to long is already of type long. Please submit your WA solution in the practice section, you will get AC (I just did). Probably there were faulty test cases in the contest which the setter fixed later.

Can somebody tell me why has my answer been downvoted? When you are downvoting, please atleast explain where is it wrong or uncomplete. Give them the opportunity to improve. This is very unfair.