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.