Not Frequent
0/5

# Modular Arithmetic

Authors: Darren Yao, Michael Cao, Andi Qu, Benjamin Qi, Andrew Wang, Kevin Sheng

Working with remainders from division.

### Prerequisites

Resources
IUSACO

very brief, module is based off this

David Altizio

plenty of examples from math contests

CPH
PAPS
CF

some practice probs

MONT

## Introduction

In modular arithmetic, instead of working with integers themselves, we work with their remainders when divided by $m$. We call this taking modulo $m$. For example, if we take $m = 23$, then instead of working with $x = 247$, we use $x \bmod 23 = 17$. Usually, $m$ will be a large prime, given in the problem; the two most common values are $10^9 + 7$ and $998\,244\,353=119\cdot 2^{23}+1$. Modular arithmetic is used to avoid dealing with numbers that overflow built-in data types, because we can take remainders, according to the following formulas:

$(a+b) \bmod m = (a \bmod m + b \bmod m) \bmod m$
$(a-b) \bmod m = (a \bmod m - b \bmod m) \bmod m$
$(a \cdot b) \pmod{m} = ((a \bmod m) \cdot (b \bmod m)) \bmod m$
$a^b \bmod {m} = (a \bmod m)^b \bmod m$

## Modular Exponentiation

Focus Problem – read through this problem before continuing!

### Resources

Resources
cp-algo

Binary exponentiation can be used to efficently compute $x ^ n \mod m$. To do this, let's break down $x ^ n$ into binary components. For example, $5 ^ {10}$ = $5 ^ {1010_2}$ = $5 ^ 8 \cdot 5 ^ 2$. Then, if we know $x ^ y$ for all $y$ which are powers of two ($x ^ 1$, $x ^ 2$, $x ^ 4$, $\dots$ , $x ^ {2^{\lfloor{\log_2n} \rfloor}}$, we can compute $x ^ n$ in $\mathcal{O}(\log n)$.

To deal with $m$, observe that modulo doesn't affect multiplications, so we can directly implement the above "binary exponentiation" algorithm while adding a line to take results $\pmod m$.

Solution

## Modular Inverse

The modular inverse is the equivalent of the reciprocal in real-number arithmetic; to divide $a$ by $b$, multiply $a$ by the modular inverse of $b$. We'll only consider prime moduli $p$ here.

For example, the inverse of $2$ modulo $p=10^9+7$ is $i=\frac{p+1}{2}=5\cdot 10^8+4$. This means that for any integer $x$,

$(2x)\cdot i\equiv x\cdot (2i)\equiv x\pmod{10^9+7}.$

For example, $10i\equiv 5\pmod{10^9+7}$.

Resources
cp-algo

Various ways to take modular inverse, we'll only discuss the second here

### With Exponentiation

Fermat's Little Theorem (not to be confused with Fermat's Last Theorem) states that all integers $a$ not divisible by $p$ satisfy $a^{p - 1} \equiv 1 \pmod{p}$. Consequently, $a^{p-2} \cdot a \equiv 1 \pmod{p}$. Therefore, $a^{p - 2}$ is a modular inverse of $a$ modulo $p$.

C++

const int MOD = 1e9 + 7;
int main() {	ll x = binpow(2, MOD - 2, MOD);	cout << x << "\n";  // 500000004	assert(2 * x % MOD == 1);}

Java

public class Main {	public static final int MOD = (int) Math.pow(10, 9) + 7;	public static void main(String[] args) throws IOException {		long x = binpow(2, MOD - 2, MOD);		System.out.println(x);  // 500000004		assert(2 * x % MOD == 1);	}}

Python

MOD = 10 ** 9 + 7
x = pow(2, MOD - 2, MOD);print(x)  # 500000004assert(2 * x % MOD == 1);

Because it takes $\mathcal{O}(\log p)$ time to compute a modular inverse modulo $p$, frequent use of division inside a loop can significantly increase the running time of a program. If the modular inverse of the same number(s) is/are being used many times, it is a good idea to precalculate it.

Also, one must always ensure that they do not attempt to divide by 0. Be aware that after applying modulo, a nonzero number can become zero, so be very careful when dividing by non-constant values.

### Optional: Another Way to Compute Modular Inverses

We can also use the extended Euclidean algorithm. See the module in the Advanced section.

## Templates

Resources
Benq
Benq

feasible to type up during a contest

Using BenQ's template, both of these do the same thing:

#include <bits/stdc++.h>using namespace std;
const int MOD = 1e9 + 7;
Code Snippet: ModInt (Click to expand)
int main() {	{		int a = 1e8, b = 1e8, c = 1e8;

## Problems

StatusSourceProblem NameDifficultyTags
CSESEasy
Show TagsModular Arithmetic
CFEasy
Show TagsModular Arithmetic
CSESNormal
Show TagsModular Arithmetic
DMOJHard
Show TagsModular Arithmetic

### Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!