To find the value of x^n~mod~m use the formula:
Let’s consider the iterative exponentiation algorithm.
Any integer can be represented in binary form:
For example:
Using this representation, the power x^{13} can be rewritten as a product of powers of two:
To compute the power, it is necessary to multiply only those powers for which the corresponding bit in the binary representation is .
To construct any power x^n, it is sufficient to have only powers of the form:
The algorithm for computing x^n scans the bits of the number from the least significant to the most significant, and at each iteration:
If the current bit of the number is , multiply the result by the current power ;
Square the base ;
Shift the exponent one bit to the right (divide by ).
n | bit | degree x | res |
13 | 1 | x^1 | x^1 |
6 | 0 | x^2 | x^1 |
3 | 1 | x^4 | x^5 |
1 | 1 | x^8 | x^{13} |
Algorithm implementation - recursion
The powmod function computes the value of x^n~mod~m.
long long powmod(long long x, long long n, long long m)
{
if (n == 0) return 1;
if (n % 2 == 0) return powmod((x * x) % m, n / 2, m);
return (x * powmod(x, n - 1, m)) % m;
}The main part of the program. Read the input data.
scanf("%lld %lld %lld", &x, &n, &m);Compute the value of x^n~mod~m.
res = powmod(x, n, m);Print the answer.
printf("%lld\n", res);Algorithm implementation - iteration
The powmod function computes the value of x^n~mod~m.
long long powmod(long long x, long long n, long long m)
{
long long res = 1;
while (n > 0)
{
if (n & 1) res = (res * x) % m;
x = (x * x) % m;
n >>= 1;
}
return res;
}The main part of the program. Read the input data.
scanf("%lld %lld %lld", &x, &n, &m);Compute the value of x^n~mod~m.
res = powmod(x, n, m);Print the answer.
printf("%lld\n", res);Java implementation
import java.util.*;
public class Main
{
static long PowMod(long x, long n, long m)
{
if (n == 0) return 1;
if (n % 2 == 0) return PowMod((x * x) % m, n / 2, m);
return (x * PowMod(x, n - 1, m)) % m;
}
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long x = con.nextLong();
long n = con.nextLong();
long m = con.nextLong();
long res = PowMod(x, n, m);
System.out.println(res);
con.close();
}
}Java implementation — BigInteger
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
BigInteger a = con.nextBigInteger();
BigInteger b = con.nextBigInteger();
BigInteger m = con.nextBigInteger();
System.out.println(a.modPow(b, m));
con.close();
}
}Python implementation — pow
Read the input data.
x, n, m = map(int, input().split())Compute and print the answer.
print(pow(x, n, m))Python implementation — recursion
The myPow function computes the value of x^n~mod~m.
def myPow(x, n, m):
if (n == 0): return 1
if (n % 2 == 0): return myPow((x * x) % m, n / 2, m)
return (x * myPow(x, n - 1, m)) % mThe main part of the program. Read the input data.
x, n, m = map(int, input().split())Compute and print the answer.
print(myPow(x, n, m))