![]() |
VOOZH | about |
In mathematics, modular arithmetic refers to the arithmetic of integers that wraps around when a certain value is reached, called the modulus. This becomes particularly crucial when handling large numbers in competitive programming. This article "Modular Arithmetic for Competitive Programming" will explore modular arithmetic, its operations, the underlying concepts, and practical applications. By understanding and implementing modular arithmetic, programmers can effectively manage and manipulate large integers, enhancing their skills in competitive programming.
Table of Content
Modular arithmetic is a branch of arithmetic mathematics related to the "mod" functionality. It is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value, known as the modulus. In its most elementary form, it is arithmetic done with a count that resets itself to zero every time a certain whole number N greater than one, known as the modulus (mod), has been reached.
The implementation of modular arithmetic involves various operations such as addition, subtraction, multiplication, division, and exponentiation. Here are some rules for these operations:
The concept of modular arithmetic is to find the remainder of a number upon division by another number. For example, if we have "A mod B" and we increase 'A' by a multiple of 'B', we will end up in the same spot, i.e.,"A mod B = (A + K * B) mod B" for any integer 'K'.
To visualize the modulo operator, we can use circles. We write 0 at the top of a circle and continuing clockwise writing integers 1, 2, ... up to one less than the modulus. For example, a clock with the 12 replaced by a 0 would be the circle for a modulus of 12.
To find the result of "A mod B" we can follow these steps:
Here are some examples of modular arithmetic operations:
Below code performs modular addition, subtraction, multiplication, and division.
Modular Addition: 2 Modular Subtraction: 4 Modular Multiplication: 4 Modular Division: 4
Modular arithmetic is commanly used in competitive programming and coding contests that require us to calculate the mod of something. It is typically used in combinatorial and probability tasks, where you are asked to calculate a huge number, then told to output it modulo 10^9 + 7. Below are the more use cases of modular arithmetic in CP.
In combinatorial tasks, you are often asked to calculate a huge number, then told to output it modulo 10^9 + 7. This is because the number can be so large that it cannot be stored in a variable of any data type. By taking the mod of the number, we reduce its size to a manageable level.
Modular arithmetic is used in polynomial arithmetic to perform addition, subtraction, and multiplication of polynomials under a modulus
Many hashing algorithms use modular arithmetic to ensure that the hash values they produce fit into a certain range.
In probability tasks, you might need to calculate the probability of an event occurring. The probability can be a huge number, and you are often asked to output it modulo 10^9 + 7.
Modular arithmetic can be used to solve linear congruence, which are equations of the form ax ≡ b (mod m). These types of problems often appear in number theory and cryptography.
Problem | Practice |
|---|---|
Find (a^b)%m | |
Friends Pairing Problem | |
How Many X's? | |
Padovan Sequence | |
Matrix Exponentiation | |
Mr Modulo and Pairs | |
Challenge by Nikitasha | |
Rahul and The Lift | |
Find the pattern | |
nCr mod M | Part 1 |