@Test public void testPowMod() { assertEquals(0, Library.powMod(0, 0, 1)); assertEquals(0, Library.powMod(1, 0, 1)); assertEquals(0, Library.powMod(0, 1, 1)); assertEquals(0, Library.powMod(1, 1, 1)); assertEquals(0, Library.powMod(2, 3, 1)); assertEquals(1, Library.powMod(0, 0, 2)); assertEquals(1, Library.powMod(1, 0, 2)); assertEquals(1, Library.powMod(2, 0, 2)); assertEquals(0, Library.powMod(0, 1, 2)); assertEquals(1, Library.powMod(1, 1, 2)); assertEquals(0, Library.powMod(2, 1, 2)); assertEquals(0, Library.powMod(0, 2, 2)); assertEquals(1, Library.powMod(1, 2, 2)); assertEquals(0, Library.powMod(2, 2, 2)); assertEquals(1, Library.powMod(2, 2, 3)); assertEquals(4, Library.powMod(4, 3, 5)); assertEquals(3, Library.powMod(7, 7, 10)); assertEquals(326216098, Library.powMod(78051657, 234602, 456087413)); assertEquals(1488576545, Library.powMod(2147480000, 2147483645, 2147483647)); }
public static int powMod(int x, int y, int m) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Modular reciprocal not supported"); if (m <= 0) throw new IllegalArgumentException("Modulus must be positive"); if (m == 1) return 0; int z = 1; for (; y != 0; y >>>= 1) { if ((y & 1) != 0) z = (int)((long)z * x % m); x = (int)((long)x * x % m); } return z; }
Library { public static int powMod(int x, int y, int m) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Modular reciprocal not supported"); if (m <= 0) throw new IllegalArgumentException("Modulus must be positive"); if (m == 1) return 0; int z = 1; for (; y != 0; y >>>= 1) { if ((y & 1) != 0) z = (int)((long)z * x % m); x = (int)((long)x * x % m); } return z; } }
Library { public static int powMod(int x, int y, int m) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Modular reciprocal not supported"); if (m <= 0) throw new IllegalArgumentException("Modulus must be positive"); if (m == 1) return 0; int z = 1; for (; y != 0; y >>>= 1) { if ((y & 1) != 0) z = (int)((long)z * x % m); x = (int)((long)x * x % m); } return z; }  }
Library { public static int powMod(int x, int y, int m) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Modular reciprocal not supported"); if (m <= 0) throw new IllegalArgumentException("Modulus must be positive"); if (m == 1) return 0; int z = 1; for (; y != 0; y >>>= 1) { if ((y & 1) != 0) z = (int)((long)z * x % m); x = (int)((long)x * x % m); } return z; }  static String reverse(String s); static boolean isPalindrome(String s); static boolean isPalindrome(int x); static int sqrt(int x); static long sqrt(long x); static BigInteger sqrt(BigInteger x); static boolean isSquare(int x); static int pow(int x, int y); static int powMod(int x, int y, int m); static int reciprocalMod(int x, int m); static BigInteger factorial(int n); static BigInteger binomial(int n, int k); static int gcd(int x, int y); static boolean isPrime(int x); static boolean[] listPrimality(int n); static int[] listPrimes(int n); static int[] listSmallestPrimeFactors(int n); static int totient(int n); static int[] listTotients(int n); static boolean nextPermutation(int[] arr); }
Library { public static int powMod(int x, int y, int m) { if (x < 0) throw new IllegalArgumentException("Negative base not supported"); if (y < 0) throw new IllegalArgumentException("Modular reciprocal not supported"); if (m <= 0) throw new IllegalArgumentException("Modulus must be positive"); if (m == 1) return 0; int z = 1; for (; y != 0; y >>>= 1) { if ((y & 1) != 0) z = (int)((long)z * x % m); x = (int)((long)x * x % m); } return z; }  static String reverse(String s); static boolean isPalindrome(String s); static boolean isPalindrome(int x); static int sqrt(int x); static long sqrt(long x); static BigInteger sqrt(BigInteger x); static boolean isSquare(int x); static int pow(int x, int y); static int powMod(int x, int y, int m); static int reciprocalMod(int x, int m); static BigInteger factorial(int n); static BigInteger binomial(int n, int k); static int gcd(int x, int y); static boolean isPrime(int x); static boolean[] listPrimality(int n); static int[] listPrimes(int n); static int[] listSmallestPrimeFactors(int n); static int totient(int n); static int[] listTotients(int n); static boolean nextPermutation(int[] arr);  }