General form:
(a1 * a2 * a3 ... * an) % m = [(a1 % m) * (a2 % m) * ... * (a3 % m) ] % m
Apply above formula, we have:
(2^64 * x) % m = (((2^64) % m) * (x % m)) % m
For the first part: 2^64 mod m
. I can make more general case: 2^t mod m
. I have this pseudocode. In will run in N(log t)
times. This pseudocode just for t and m are normal integer. Base on range of t and m, you can fix inside function calculation to use BigInteger at suitable point.
long solve(long t, long m) {
if (t == 0) return 1 % m;
if (t == 1) return t % m;
long res = solve(t/2, m);
res = (res * res) % m;
if (t % 2 == 1) res = (res * 2) % m;
return res;
}
Thanks for OldCurmudgeon. Above code can be one simple line:
BigInteger res = (new BigInteger("2")).
modPow(new BigInteger("64"), new BigInteger("" + m));
Here is the implementation of modPow
. This implementation uses different approach. Algorithm starts from m: will break m in to m = 2^k*q
. Then will find modulo of 2^k and q then use Chinese Reminder theorem
combines result.
public BigInteger modPow(BigInteger exponent, BigInteger m) {
if (m.signum <= 0)
throw new ArithmeticException("BigInteger: modulus not positive");
// Trivial cases
if (exponent.signum == 0)
return (m.equals(ONE) ? ZERO : ONE);
if (this.equals(ONE))
return (m.equals(ONE) ? ZERO : ONE);
if (this.equals(ZERO) && exponent.signum >= 0)
return ZERO;
if (this.equals(negConst[1]) && (!exponent.testBit(0)))
return (m.equals(ONE) ? ZERO : ONE);
boolean invertResult;
if ((invertResult = (exponent.signum < 0)))
exponent = exponent.negate();
BigInteger base = (this.signum < 0 || this.compareTo(m) >= 0
? this.mod(m) : this);
BigInteger result;
if (m.testBit(0)) { // odd modulus
result = base.oddModPow(exponent, m);
} else {
/*
* Even modulus. Tear it into an "odd part" (m1) and power of two
* (m2), exponentiate mod m1, manually exponentiate mod m2, and
* use Chinese Remainder Theorem to combine results.
*/
// Tear m apart into odd part (m1) and power of 2 (m2)
int p = m.getLowestSetBit(); // Max pow of 2 that divides m
BigInteger m1 = m.shiftRight(p); // m/2**p
BigInteger m2 = ONE.shiftLeft(p); // 2**p
// Calculate new base from m1
BigInteger base2 = (this.signum < 0 || this.compareTo(m1) >= 0
? this.mod(m1) : this);
// Caculate (base ** exponent) mod m1.
BigInteger a1 = (m1.equals(ONE) ? ZERO :
base2.oddModPow(exponent, m1));
// Calculate (this ** exponent) mod m2
BigInteger a2 = base.modPow2(exponent, p);
// Combine results using Chinese Remainder Theorem
BigInteger y1 = m2.modInverse(m1);
BigInteger y2 = m1.modInverse(m2);
if (m.mag.length < MAX_MAG_LENGTH / 2) {
result = a1.multiply(m2).multiply(y1).add(a2.multiply(m1).multiply(y2)).mod(m);
} else {
MutableBigInteger t1 = new MutableBigInteger();
new MutableBigInteger(a1.multiply(m2)).multiply(new MutableBigInteger(y1), t1);
MutableBigInteger t2 = new MutableBigInteger();
new MutableBigInteger(a2.multiply(m1)).multiply(new MutableBigInteger(y2), t2);
t1.add(t2);
MutableBigInteger q = new MutableBigInteger();
result = t1.divide(new MutableBigInteger(m), q).toBigInteger();
}
}
return (invertResult ? result.modInverse(m) : result);
}
For the second part: you can easily use BigInteger
or simply normal calculation, depend on range of x and m.