Modular Arithmetic for Beginners

Revision en1, by Spheniscine, 2019-12-27 14:50:55

Introduction

If you're new to the world of competitive programming, you may have noticed that some questions have this funny habit of asking you to calculate a huge number, then tell you that "because this number can be huge, please output it modulo $$$10^9 + 7$$$". Like, it's not enough that they ask you to calculate a number they know will overflow basic integer data types, but now you need to apply the modulo operation after that? Even worse are those that say you need to calculate a fraction $$$\frac pq$$$ and ask you to output $$$r$$$ where $$$r \cdot q = p \text{ mod } n$$$... not only do you have to calculate a fraction with huge numbers, how in the world are you going to find $$$r$$$?

Actually, the modulo is there to make the calculation easier, not harder. This may sound counterintuitive, but once you know how modular arithmetic works, you'll see that it doesn't work too differently from the math you learned in school.

Tags modulo, modular arithmetic

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en33 English Spheniscine 2020-10-24 18:50:23 20
en32 English Spheniscine 2020-04-18 06:33:00 319
en31 English Spheniscine 2020-02-12 04:43:04 101
en30 English Spheniscine 2020-02-12 04:39:24 214 Tiny change: 'efined as $1$](https://' -> 'efined as 1](https://'
en29 English Spheniscine 2020-01-16 12:01:55 73 Tiny change: ' produce a number between' -> ' produce an integer between'
en28 English Spheniscine 2020-01-16 11:40:32 252
en27 English Spheniscine 2020-01-07 15:14:50 14 Tiny change: '$0 \cdot x \text{ mo' -> '$0 \cdot x\ \text{ mo'
en26 English Spheniscine 2020-01-06 22:33:32 8
en25 English Spheniscine 2019-12-29 14:18:37 66
en24 English Spheniscine 2019-12-29 01:11:18 2 Tiny change: 'v p \pmod n$... not o' -> 'v p \pmod m$... not o'
en23 English Spheniscine 2019-12-29 01:10:14 218 Tiny change: ' \equiv p \text{ mod } n$... not o' -> ' \equiv p (\pmod n)$... not o'
en22 English Spheniscine 2019-12-28 13:56:37 90 Tiny change: 'ession $n mod m$ is kno' -> 'ession $n \text{ mod } m$ is kno'
en21 English Spheniscine 2019-12-28 05:46:39 3 Tiny change: 'minator isn't coprime t' -> 'minator is coprime t'
en20 English Spheniscine 2019-12-28 05:16:04 2 Tiny change: 'a fraction, we want a' -> 'a fraction; we want a'
en19 English Spheniscine 2019-12-28 05:15:21 181
en18 English Spheniscine 2019-12-28 04:34:30 32
en17 English Spheniscine 2019-12-28 04:30:00 7 Tiny change: 'r \cdot q = p \text{ ' -> 'r \cdot q \equiv p \text{ '
en16 English Spheniscine 2019-12-28 04:22:39 111
en15 English Spheniscine 2019-12-28 04:21:05 37
en14 English Spheniscine 2019-12-28 04:18:50 325
en13 English Spheniscine 2019-12-28 03:57:50 55
en12 English Spheniscine 2019-12-28 03:44:37 2 Tiny change: 'xt{ mod } 0 = 1$\n\nM' -> 'xt{ mod } m = 1$\n\nM'
en11 English Spheniscine 2019-12-28 03:44:15 174
en10 English Spheniscine 2019-12-27 17:41:43 36 Tiny change: 'orces.com/problemset/problem/935/D)' -> 'orces.com/contest/935/problem/D)'
en9 English Spheniscine 2019-12-27 17:37:21 8 Tiny change: 'division) in a way ' -> 'division) defined in a way '
en8 English Spheniscine 2019-12-27 17:26:44 344 Tiny change: 'actorials are too big ' -> 'actorials can be too big ' (published)
en7 English Spheniscine 2019-12-27 17:06:35 4705 Tiny change: 'method is often more popu' -> 'method is more popu'
en6 English Spheniscine 2019-12-27 15:56:03 270 Tiny change: 'd } m = 1$' -> 'd } m = 1$, therefore the number we need is $a^{m-2} \text{ mod } m$'
en5 English Spheniscine 2019-12-27 15:51:41 790 Tiny change: 'll see why. Soon you' -> 'll see why too. Soon you'
en4 English Spheniscine 2019-12-27 15:29:05 1601 Tiny change: 't{ mod } m' -> 't{ mod } m$'
en3 English Spheniscine 2019-12-27 15:04:10 518 Tiny change: ' b \text{ mod } m \' -> ' b \text{ mod } m \'
en2 English Spheniscine 2019-12-27 14:56:23 257
en1 English Spheniscine 2019-12-27 14:50:55 987 Initial revision (saved to drafts)