AtCoder Beginner contest 177

question : https://atcoder.jp/contests/abc177/tasks/abc177_c
editorial : https://atcoder.jp/contests/abc177/editorial/97

 if (sum < 0) sum += mod;

what does this line of code do?

for negative sum you have to add mod first to ensure it doesn’t remain negative and then perform mod on it , Because mod works differently for negative and positive

2 Likes

Can you help me with this problem of the same contest.

1 Like

what if i use abs() ? :sweat_smile:

I will give you an example? As the definition of mod says it is the smallest number you should subtract from that number to make it divisible by number you are performing mod with , so ans for a simple 2 mod 3 will result out as 2 which means if we subtract 2 from 2 you make it divisible by 3 , now what if I perform -2 mod 3 this will result out as -2 , again see if you subtract -2 from -2 you will get it divisible by 3 But Actually our result has parted ways with convention we follow in question which require the ans in terms of modulo it should stay between 0 and MOD-1 , We should go for the value 1 instead if we subtract 1 from -2 we get -3 which is divisible by 3 so that is why we add mod for negative cases . ( -2 + 3) mod 3 = > 1 mod 3 => 1 (The answer we need )

3 Likes

Sorry , I have not upsolved it bro

1 Like

ohh…got it bro! and thank you so much :smile:

ohk
its fine bro :+1: