feat: mod inv

This commit is contained in:
2025-07-18 00:11:25 +08:00
parent f3b35ccc43
commit 89a258359f

View File

@@ -26,13 +26,16 @@ public class ModularInv {
for (long j = 1; j < 10000; j++) { for (long j = 1; j < 10000; j++) {
BigInteger a = BigInteger.valueOf(i); BigInteger a = BigInteger.valueOf(i);
BigInteger p = BigInteger.valueOf(j); BigInteger p = BigInteger.valueOf(j);
// gcd(a, p) = 1
if (!a.gcd(p).equals(BigInteger.ONE)) { if (!a.gcd(p).equals(BigInteger.ONE)) {
continue; continue;
} }
// a^-1 mod p
BigInteger x = a.modInverse(p); BigInteger x = a.modInverse(p);
BigInteger y = modularInv(a, p); BigInteger y = modularInv(a, p);
System.out.println(Arrays.asList(a, p, a.modInverse(p), modularInv(a, p))); BigInteger z = a.modPow(Phi.phi(p).subtract(BigInteger.ONE), p);
if (!y.equals(x)) { System.out.println(Arrays.asList(a + "^-1 mod " + p, x, y, z));
if (!y.equals(x) || !y.equals(z)) {
throw new RuntimeException(); throw new RuntimeException();
} }
} }