Hello Codeforces!
I was solving this problem
http://mirror.codeforces.com/contest/810/problem/C
and it gives me a lot of WA, here is my code
but after that I got an AC, because i just implemented this "+Mod" in this line
-- Sum=(Sum-der)%Mod;
And finally I stay like this
-- Sum=(Sum+Mod-der)%Mod;
why does it happened?
this is because (sum — der) can be negative. So in programming languages, (-1) modulo 2 won't give you 1. It will give you negative remainder, i.e. -1 because -1 = 2 * 0 + -1 or -3 mod 2 = 2 * -1 + (-1). So your sum — der will give negative value, which theoretically is correct but gives WA. So, you need to make it positive by adding a multiple of Mod. The better way to do these given of computations is ((sum — der) % mod + mod) % mod.