Modular Exponentiation (Power in Modular Arithmetic) Given three integers x, n, and M, compute (x^n) % M (remainder when x raised to the power n is divided by M).Examples : Input: x = 3, n = 2, M = 4Output: 1Explanation: 32 % 4 = 9 % 4 = 1.Input: x = 2, n = 6, M = 10Output: 4Explanation: 26 % 10 = 64 % 10 = 4.Table of Content[Naive Approach] Repeated
6 min read
Modular multiplicative inverse Given two integers A and M, find the modular multiplicative inverse of A under modulo M.The modular multiplicative inverse is an integer X such that:A X â¡ 1 (mod M) Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 m
15+ min read
Modular Multiplication Given three integers a, b, and M, where M is the modulus. Compute the result of the modular multiplication of a and b under modulo M.((aÃb) mod M)Examples:Input: a = 5, b = 3, M = 11Output: 4Explanation: a à b = 5 à 3 = 15, 15 % 11 = 4, so the result is 4.Input: a = 12, b = 15, M = 7Output: 5Explana
6 min read
Modular Division Given three positive integers a, b, and M, the objective is to find (a/b) % M i.e., find the value of (a à b-1 ) % M, where b-1 is the modular inverse of b modulo M.Examples: Input: a = 10, b = 2, M = 13Output: 5Explanation: The modular inverse of 2 modulo 13 is 7, so (10 / 2) % 13 = (10 à 7) % 13 =
13 min read