feat: upate mod inv

This commit is contained in:
2025-07-18 00:13:05 +08:00
parent 89a258359f
commit 6944d618d6

View File

@@ -3,22 +3,6 @@ package me.hatter.math;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Arrays; import java.util.Arrays;
// https://zhuanlan.zhihu.com/p/426499159
// def modular_inv(a, p):
// x, y, m = 1, 0, p
// while a > 1:
// q = a // m
// t = m
//
// m = np.mod(a, m)
// a = t
// t = y
//
// y, x = x - np.int64(q) * np.int64(y), t
//
// if x < 0:
// x = np.mod(x, p)
// return np.mod(x, p)
public class ModularInv { public class ModularInv {
public static void main(String[] args) { public static void main(String[] args) {
@@ -42,6 +26,22 @@ public class ModularInv {
} }
} }
// https://zhuanlan.zhihu.com/p/426499159
// def modular_inv(a, p):
// x, y, m = 1, 0, p
// while a > 1:
// q = a // m
// t = m
//
// m = np.mod(a, m)
// a = t
// t = y
//
// y, x = x - np.int64(q) * np.int64(y), t
//
// if x < 0:
// x = np.mod(x, p)
// return np.mod(x, p)
public static BigInteger modularInv(BigInteger a, BigInteger p) { public static BigInteger modularInv(BigInteger a, BigInteger p) {
BigInteger x = BigInteger.ONE; BigInteger x = BigInteger.ONE;
BigInteger y = BigInteger.ZERO; BigInteger y = BigInteger.ZERO;