Need Help with modulo multiplication

I have been trying to solve a question where the values were like :

num = (num*x) % m and this kept me giving WA

and when i did this :

num = (1LL*num*x) % m … I got an AC.

The number x was already smaller than m and was well in range of 10^5. I cant figure out why is this happening.

Please help as this thing has wasted many hours of mine in different questions and i cant figure it out.

Assuming num and x are in int
When you are multiplying num and x it will give result in int. So, overflow has already occured. Taking its mod will give wrong value. In second case you are getting result in long long. It is able to store that value. So, its mod gave correct result.

2 Likes