Recently i went through few modulo and integer arithmetic section from a website
I am writing this as recap for myself but it might help beginners who are unfamiliar with Modular arithmetic
A few basic observations
If a mod b = 0, then a is divisible by b. This single condition appears in an enormous number of problems, from basic number theory to constraints checking.
Another common use is determining parity. If n mod 2 = 0, the number is even; if n mod 2 = 1, it is odd. Whenever a problem involves alternating behavior, parity, or splitting elements into two groups, modulo 2 is usually the first thing worth checking.
Modulo and cyclic behavior
One important property of modulo is that it naturally creates cycles. The values of n mod 5, for instance, repeat as 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ....
This is extremely useful in practice. Circular arrays, rotating turns, and repeating patterns can all be handled cleanly with modulo. If an array has size n and an index i goes out of bounds, using i mod n wraps it back into the valid range.
for a real life example we can take the days of week : they repeat every 7 days , so its exactly modular arithmetic (N%7 would give us the day even if the N value is more than 7)
Modulo properties and large numbers
Two properties of modulo are used constantly in competitive programming:
(a + b) mod m = ((a mod m) + (b mod m)) mod m (a × b) mod m = ((a mod m) × (b mod m)) mod m
These identities allow us to reduce numbers at every step instead of working with extremely large values. This is crucial when problems ask for answers modulo a large number such as 10^9 + 7. Multiplying first and taking modulo at the end often causes overflow, while applying modulo during intermediate steps keeps everything safe.
consider computing a product of two very large numbers modulo 10^9 + 7. Direct multiplication may overflow even 64-bit integers. By reducing both numbers modulo m first, then multiplying, and finally applying modulo again, the result remains correct while staying within safe limits.
For example
import math
tc=int(input())
mod=10**9+7
for i in range(tc):
m=int(input())
p=(m-1)**2
r=pow(2,p)
print(r%mod)
The above code return ML (Memory limit error due to overflow while doing the pow)
Which can be rectified using mod in intermediate steps:
tc = int(input())
mod = 10**9 + 7
for I in range(tc):
m = int(input())
p = (m - 1) * (m - 1)
print(pow(2, p, mod))
Negative numbers and modulo
modulo with negative numbers. Mathematically, -7 mod 3 equals 2, but programming languages handle this differently. Python returns 2, while C++ and Java return -1.
To avoid surprises, a commonly used safe expression is (a mod m + m) mod m. This guarantees a value in the range [0, m−1], regardless of the language.
Digit extraction using modulo
Modulo is also heavily used in digit manipulation problems. The last digit of a number can be obtained using n mod 10, and removing the last digit is done via integer division by 10.
For example, repeatedly applying these operations to 12345 extracts digits from right to left. This pattern appears in problems involving digit sums, palindromes;
Integer division vs modulo
Integer division and modulo are complementary. Integer division tells us how many full groups can be formed, while modulo tells us what remains. Many problems rely on using both together, such as grouping elements evenly or stripping digits from a number.
Understanding when to use each makes solutions much cleaner.
Common mistakes
A frequent mistake is confusing division with modulo, or mixing them incorrectly in expressions without proper parentheses. Another common issue is ignoring negative modulo behavior, which can silently break solutions in some languages. Finally, applying modulo only at the end of a long calculation often leads to overflow and incorrect results.
Some easy practice problems Happy Number Add digits Fizz Buzz Power of two
This post was written while revisiting the modulo and integer arithmetic sections on REPOVIVE . Thanks to Shayan for building and maintaining such a useful resource.







The above image is of the one who shared solutions via telegram and I think the codeforces ID is :!
