modulus and long doubt

Hey ,
Check this problem : CodeChef: Practical coding for everyone
sol 1: CodeChef: Practical coding for everyone
sol 2: CodeChef: Practical coding for everyone
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.