Given four integers A, B, K, M. The task is to find (A + iB)K % M which is a complex number too. A + iB represents a complex number. Examples:
Input : A = 2, B = 3, K = 4, M = 5 Output: 1 + i*0 Input : A = 7, B = 3, K = 10, M = 97 Output: 25 + i*29
Prerequisite: Modular Exponentiation Approach: An efficient approach is similar to the modular exponentiation of a single number. Here, instead of a single we have two number A, B. So, pass a pair of integers as a parameter to the function instead of a single number. Below is the implementation of the above approach :Â
C++
#include <bits/stdc++.h> using namespace std; Â
// Function to multiply two complex numbers modulo M pair< int , int > Multiply (pair< int , int > p, pair< int , int > q,                                                     int M) {     // Multiplication of two complex numbers is     // (a + ib)(c + id) = (ac - bd) + i(ad + bc)          int x = ((p.first * q.first) % M - (p.second *                                     q.second) % M + M) % M;          int y = ((p.first * q.second) % M + (p.second *                                           q.first) % M) %M; Â
    // Return the multiplied value     return {x, y}; } Â
Â
// Function to calculate the complex modular exponentiation pair< int , int > compPow(pair< int , int > complex, int k, int M) {     // Here, res is initialised to (1 + i0)     pair< int , int > res = { 1, 0 };          while (k > 0)     {         // If k is odd         if (k & 1)         {             // Multiply 'complex' with 'res'             res = Multiply(res, complex, M);         }                  // Make complex as complex*complex         complex = Multiply(complex, complex, M);                  // Make k as k/2         k = k >> 1;     }          //Return the required answer     return res; } Â
// Driver code int main() { Â
    int A = 7, B = 3, k = 10, M = 97;          // Function call     pair< int , int > ans = compPow({A, B}, k, M);          cout << ans.first << " + i" << ans.second;            return 0; } |
Java
// Java implementation of the approach import java.util.*; Â
class GFG { static class pair { Â Â Â Â int first, second; Â Â Â Â public pair( int first, int second) Â Â Â Â { Â Â Â Â Â Â Â Â this .first = first; Â Â Â Â Â Â Â Â this .second = second; Â Â Â Â } } Â
// Function to multiply two complex numbers modulo M static pair Multiply (pair p, pair q, int M) {     // Multiplication of two complex numbers is     // (a + ib)(c + id) = (ac - bd) + i(ad + bc)          int x = ((p.first * q.first) % M -              (p.second * q.second) % M + M) % M;          int y = ((p.first * q.second) % M +              (p.second * q.first) % M) % M; Â
    // Return the multiplied value     return new pair(x, y); } Â
Â
// Function to calculate the // complex modular exponentiation static pair compPow(pair complex, int k, int M) {     // Here, res is initialised to (1 + i0)     pair res = new pair( 1 , 0 );          while (k > 0 )     {         // If k is odd         if (k % 2 == 1 )         {             // Multiply 'complex' with 'res'             res = Multiply(res, complex, M);         }                  // Make complex as complex*complex         complex = Multiply(complex, complex, M);                  // Make k as k/2         k = k >> 1 ;     }          // Return the required answer     return res; } Â
// Driver code public static void main(String[] args) {     int A = 7 , B = 3 , k = 10 , M = 97 ;          // Function call     pair ans = compPow( new pair(A, B), k, M);          System.out.println(ans.first + " + i" +                        ans.second); } } Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach Â
# Function to multiply two complex numbers modulo M def Multiply (p, q, M):          # Multiplication of two complex numbers is     # (a + ib)(c + id) = (ac - bd) + i(ad + bc)     x = ((p[ 0 ] * q[ 0 ]) % M - \          (p[ 1 ] * q[ 1 ]) % M + M) % M          y = ((p[ 0 ] * q[ 1 ]) % M + \          (p[ 1 ] * q[ 0 ]) % M) % M Â
    # Return the multiplied value     return [x, y] Â
# Function to calculate the # complex modular exponentiation def compPow( complex , k, M):          # Here, res is initialised to (1 + i0)     res = [ 1 , 0 ]          while (k > 0 ):                  # If k is odd         if (k & 1 ):                          # Multiply 'complex' with 'res'             res = Multiply(res, complex , M)                  # Make complex as complex*complex         complex = Multiply( complex , complex , M)                  # Make k as k/2         k = k >> 1          # Return the required answer     return res Â
# Driver code if __name__ = = '__main__' :     A = 7     B = 3     k = 10     M = 97          # Function call     ans = compPow([A, B], k, M)          print (ans[ 0 ], "+ i" , end = "")     print (ans[ 1 ])      # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the approach using System; Â Â Â Â Â class GFG { public class pair { Â Â Â Â public int first, second; Â Â Â Â public pair( int first, int second) Â Â Â Â { Â Â Â Â Â Â Â Â this .first = first; Â Â Â Â Â Â Â Â this .second = second; Â Â Â Â } } Â
// Function to multiply two complex numbers modulo M static pair Multiply (pair p, pair q, int M) {     // Multiplication of two complex numbers is     // (a + ib)(c + id) = (ac - bd) + i(ad + bc)          int x = ((p.first * q.first) % M -              (p.second * q.second) % M + M) % M;          int y = ((p.first * q.second) % M +              (p.second * q.first) % M) % M; Â
    // Return the multiplied value     return new pair(x, y); } Â
Â
// Function to calculate the // complex modular exponentiation static pair compPow(pair complex, int k, int M) {     // Here, res is initialised to (1 + i0)     pair res = new pair(1, 0 );          while (k > 0)     {         // If k is odd         if (k % 2 == 1)         {             // Multiply 'complex' with 'res'             res = Multiply(res, complex, M);         }                  // Make complex as complex*complex         complex = Multiply(complex, complex, M);                  // Make k as k/2         k = k >> 1;     }          // Return the required answer     return res; } Â
// Driver code public static void Main(String[] args) {     int A = 7, B = 3, k = 10, M = 97;          // Function call     pair ans = compPow( new pair(A, B), k, M);          Console.WriteLine(ans.first + " + i" +                       ans.second); } } Â
// This code is contributed by 29AjayKumar |
Javascript
function pair(first, second) { Â Â Â Â this .first = first; Â Â Â Â this .second = second; } Â
function multiply(p, q, M) {    // Multiplication of two complex numbers is     // (a + ib)(c + id) = (ac - bd) + i(ad + bc)     let x = ((p.first * q.first) % M - (p.second * q.second) % M + M) % M;     let y = ((p.first * q.second) % M + (p.second * q.first) % M) % M;     return new pair(x, y); } Â
function compPow(complex, k, M) { Â Â Â Â let res = new pair(1, 0); Â Â Â Â while (k > 0) { Â Â Â Â Â Â Â Â if (k % 2 === 1) { Â Â Â Â Â Â Â Â Â Â Â Â res = multiply(res, complex, M); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â complex = multiply(complex, complex, M); Â Â Â Â Â Â Â Â k = k >> 1; Â Â Â Â } Â Â Â Â return res; } Â
let A = 7, B = 3, k = 10, M = 97; let ans = compPow( new pair(A, B), k, M); console.log(ans.first + " + i" + ans.second); Â
// This code is contributed by abn95knd1. |
25 + i29
Time complexity: O(log k).
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!