Skip to content

Commit 12af084

Browse files
committed
Rewrite CRT
1 parent 37924ec commit 12af084

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

content/number-theory/CRT.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Author: Simon Lindholm
3+
* Date: 2019-05-22
4+
* License: CC0
5+
* Description: Chinese Remainder Theorem.
6+
*
7+
* \texttt{crt(a, m, b, n)} computes $x$ such that $x\equiv a \pmod m$, $x\equiv b \pmod n$.
8+
* If $|a| < m$ and $|b| < n$, $x$ will obey $0 \le x < \text{lcm}(m, n)$.
9+
* Assumes $mn < 2^{62}$.
10+
* Status: Works
11+
* Time: $\log(n)$
12+
*/
13+
#pragma once
14+
15+
#include "euclid.h"
16+
17+
ll crt(ll a, ll m, ll b, ll n) {
18+
if (n > m) swap(a, b), swap(m, n);
19+
ll x, y, g = euclid(m, n, x, y);
20+
assert((a - b) % g == 0); // else no solution
21+
x = (b - a) % n * x % n / g * m + a;
22+
return x < 0 ? x + m*n/g : x;
23+
}

content/number-theory/chapter.tex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ \section{Primality}
1616
\section{Divisibility}
1717
\kactlimport{euclid.h}
1818
\kactlimport{Euclid.java}
19+
\kactlimport{CRT.h}
1920

2021
\subsection{Bézout's identity}
2122
For $a \neq $, $b \neq 0$, then $d=gcd(a,b)$ is the smallest positive integer for which there are integer solutions to
@@ -29,9 +30,6 @@ \section{Fractions}
2930
\kactlimport{ContinuedFractions.h}
3031
\kactlimport{FracBinarySearch.h}
3132

32-
\section{Chinese remainder theorem}
33-
\kactlimport{chinese.h}
34-
3533
\section{Pythagorean Triples}
3634
The Pythagorean triples are uniquely generated by
3735
\[ a=k\cdot (m^{2}-n^{2}),\ \,b=k\cdot (2mn),\ \,c=k\cdot (m^{2}+n^{2}), \]

content/number-theory/chinese.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)