lusho's blog

By lusho, history, 8 years ago, In English

Hello Codeforces!

I was solving this problem

http://codeforces.me/contest/810/problem/C

and it gives me a lot of WA, here is my code

27302116

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;

27302107

why does it happened?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it +5 Vote: I do not like it

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.