Блог пользователя lusho

Автор lusho, история, 8 лет назад, По-английски

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?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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.