Modular Arithmetic and Number Theory Algorithms

Modular arithmetic and number theory algorithms are essential tools in competitive programming, particularly when dealing with problems involving integers and remainders. These algorithms help perform calculations efficiently and accurately, making them invaluable for solving complex programming challenges.

Modular Arithmetic

Modular arithmetic involves performing operations on remainders. It is widely used when dealing with cyclical or repeated phenomena, such as time, angles, and number sequences. The main idea behind modular arithmetic is that two numbers are considered equivalent modulo m if they have the same remainder when divided by m.

Addition and Subtraction

When adding or subtracting numbers modulo m, you simply perform the operation as usual and then take the remainder modulo m. For example, (a + b) % m is equivalent to (a % m + b % m) % m. Similarly, (a - b) % m is equivalent to (a % m - b % m + m) % m to ensure a positive remainder.

Multiplication

Multiplication modulo m can be computed similarly using the property that (a * b) % m is equivalent to (a % m * b % m) % m. However, because the intermediate products can be large, it is often necessary to keep the values within bounds. You can achieve this by taking the modulo at each step, i.e., (a % m * b % m) % m, to avoid overflow.

Division

Division modulo m can be more complex and requires the use of modular inverses. To compute a / b modulo m, you need to find the modular inverse of b. The modular inverse of b is a number c such that (b * c) % m = 1. Then, you can multiply a by the modular inverse of b to get a * c modulo m, which is equivalent to a / b modulo m.

Number Theory Algorithms

Number theory algorithms are based on principles and properties of integers, primes, and factors. They can help solve problems in competitive programming that require handling numerical computations efficiently.

GCD and LCM

The greatest common divisor (GCD) of two integers is the largest positive integer that divides both of them without leaving a remainder. The GCD can be computed using the Euclidean algorithm, which repeatedly subtracts the smaller number from the larger number until the smaller number becomes zero. The final non-zero remainder is the GCD.

The least common multiple (LCM) of two integers is the smallest positive integer that is divisible by both of them. The LCM can be computed using the formula LCM(a, b) = a * b / GCD(a, b).

Sieve of Eratosthenes

The sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime, starting from 2. The unmarked numbers remaining after the iterations are prime. The sieve can be used to generate prime numbers quickly for various number theory problems.

Prime Factorization

Prime factorization is the process of finding the prime divisors of a number. It is often used for problems involving factors or divisors, as well as finding the number of divisors or calculating the sum of divisors. Prime factorization can be achieved using trial division or more advanced algorithms like Pollard's rho algorithm or the quadratic sieve algorithm.

Conclusion

Modular arithmetic and number theory algorithms are fundamental to competitive programming using C++. They provide efficient methods to solve problems involving integers, remainders, primes, and factors. Understanding and implementing these algorithms will significantly enhance your problem-solving skills in the world of competitive programming.


noob to master © copyleft