Exponenciación modular#5198

Exponenciación modular (Editorial)

To find the value of x^n~mod~m use the formula:

x^n~mod~m = \begin{cases} (x^2)^{n/2}~mod~m, if~n~is~even,\\ (x \cdot x^{n-1})~mod~m, if~n~is~odd, \\ 1, n = 0 \end{cases}

Let’s consider the iterative exponentiation algorithm.

Any integer can be represented in binary form:

n = b_k 2^k + ... + b_1 2^1 + b_0 2^0

For example:

13 = 1101_2 = 8 + 4 + 1

Using this representation, the power x^{13} can be rewritten as a product of powers of two:

x^{13} = x^8 \cdot x^4 \cdot x^1

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:

x^1, x^2, x^4, x^8, x^{16}, ...

The algorithm for computing x^n scans the bits of the number from the least significant to the most significant, and at each iteration:

13 = 1101_2

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)) % m

The 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))