1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include <algorithm>
#include <functional>
#include <numeric> // for std::gcd()
#include "maths/numbertheory.h"
namespace regina {
long reducedMod(long k, long modBase) {
if (modBase <= 0)
throw InvalidArgument(
"reducedMod() requires modBase to be strictly positive");
long ans = k % modBase;
if (ans < 0) {
if ((ans + modBase) <= (-ans))
return (ans + modBase);
} else if (-(ans - modBase) < ans)
return (ans - modBase);
return ans;
}
long gcd(long a, long b) {
while (a != b && b != 0) {
long tmp = a;
a = b;
b = tmp % b;
}
return (a >= 0 ? a : -a);
}
namespace {
long gcdWithCoeffsInternal(long a, long b, long& u, long& v) {
// PRE: a and b are non-negative.
// First get the trivial cases out of the way.
if (b == 0 || a == b) {
u = 1; v = 0;
return a;
}
if (a == 0) {
u = 0; v = 1;
return b;
}
u = 1;
v = 0;
long uu = 0;
long vv = 1;
while (b != 0) {
// At each stage:
// a != b and a != 0;
// u*(a_orig) + v*(b_orig) = a;
// uu*(a_orig) + vv*(b_orig) = b;
// u*vv - uu*v = ±1;
// (u,v), (uu,vv), (u,uu), (v,vv) are all coprime pairs with
// opposite signs (we treat 0 as negative for this purpose).
//
// Moreover, if we treat magnitude as distance from 1/2 (so that
// ... > |-1| > |0| == |1| < |2| < ...), then at every stage
// we have |u| ≤ |uu| and |v| ≤ |vv|.
long q = a / b;
// (u,uu) <- (uu, u - q*uu)
long tmp = u;
u = uu;
uu = tmp - (q * uu);
// (v,vv) <- (vv, v - q*vv)
tmp = v;
v = vv;
vv = tmp - (q * vv);
// (a,b) <- (b, a % b)
tmp = a;
a = b;
b = tmp % b;
}
// At this point:
// a = gcd = u*(a_orig) + v*(b_orig);
// (uu, vv) = ±(b_orig, -a_orig)/gcd.
//
// This means we have one of the following two scenarios:
//
// 1: (uu, vv) = (-b_orig, a_orig)/gcd.
// The magnitude result above then gives
// -a_orig/gcd < v ≤ 0 < u ≤ b_orig/gcd + 1,
// and the relation u*a_orig + v*b_orig = gcd then forces
// -a_orig/gcd < v ≤ 0 < u ≤ b_orig/gcd.
//
// 2: (uu, vv) = (b_orig, -a_orig)/gcd.
// The magnitude result above then gives
// -b_orig/gcd < u ≤ 0 < v ≤ a_orig/gcd + 1,
// and the relation u*a_orig + v*b_orig = gcd then forces
// -b_orig/gcd < u ≤ 0 < v ≤ a_orig/gcd.
//
// Our final aim is -a_orig/gcd < v ≤ 0 < u ≤ b_orig/gcd, which
// is easy from here:
if (u <= 0) {
u += uu; // adds (b_orig / gcd)
v += vv; // subtracts (a_orig / gcd)
}
return a;
}
}
std::tuple<long, long, long> gcdWithCoeffs(long a, long b) {
long signA = (a > 0 ? 1 : a == 0 ? 0 : -1);
long signB = (b > 0 ? 1 : b == 0 ? 0 : -1);
std::tuple<long, long, long> ans;
std::get<0>(ans) = gcdWithCoeffsInternal(a >= 0 ? a : -a,
b >= 0 ? b : -b, std::get<1>(ans), std::get<2>(ans));
std::get<1>(ans) *= signA;
std::get<2>(ans) *= signB;
return ans;
}
long lcm(long a, long b) {
if (a == 0 || b == 0)
return 0;
long tmp = std::gcd(a, b);
tmp = (a / tmp) * b;
return (tmp >= 0 ? tmp : -tmp);
}
long modularInverse(long n, long k) {
if (n <= 0)
throw InvalidArgument(
"modularInverse(n, k) requires n to be strictly positive");
if (n == 1)
return 0;
// Compute (d, u, v).
auto ans = gcdWithCoeffs(n, k % n);
// GCD should equal 1, so u*n + k*v = 1.
if (std::get<0>(ans) != 1)
throw InvalidArgument(
"modularInverse(n, k) requires n and k to be coprime");
// Inverse is v; note that -n < (+/-)v <= 0.
// Since n >= 2 now and (n,k) = 1, we know v != 0.
return (std::get<2>(ans) > 0 ? std::get<2>(ans) : std::get<2>(ans) + n);
}
} // namespace regina
|