Codeforces and Polygon may be unavailable from December 6, 19:00 (UTC) to December 6, 21:00 (UTC) due to technical maintenance. ×

Question about Modulus

Revision en1, by xrisk, 2015-08-23 16:42:53

In one problem I was doing, I was getting WA because of some modulo error. In particular, I had a cumulative frequency array which I had defined as:

cf[i]=cf[i-1]+f[i];where f[i] was always positive.

Later, there was a sequence of range queries to answer, so I did:

cout << cf[r]-cf[l-1] for each query; L, R were the range boundaries (both inclusive).

However, the problem stated to report the answer modulo 1000000007, so I changed my queries to:

cout << (cf[r]-cf[l-1]%MOD)%MOD;.

But on seeing the intended solution, I saw that the correct query was :

cout<<(cf[r]-cf[l-1]+MOD)%MOD;

I am unable to understand this, because what I knew was that (a-b)%m = (a%m-b%m)%m

Can please someone explain this to me?

Thanks!! Any help is appreciated.

Tags modulus, modular arithmetic

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English xrisk 2015-08-23 16:42:53 801 Initial revision (published)