feat: upate mod inv
This commit is contained in:
@@ -3,22 +3,6 @@ package me.hatter.math;
|
||||
import java.math.BigInteger;
|
||||
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 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) {
|
||||
BigInteger x = BigInteger.ONE;
|
||||
BigInteger y = BigInteger.ZERO;
|
||||
|
||||
Reference in New Issue
Block a user