# Compute (a*b)%c such that (a%c) * (b%c) can be beyond range

Given three numbers a, b and c such that a, b and c can be at most 1016. The task is to compute (a*b)%c

A simple solution of doing ( (a % c) * (b % c) ) % c would not work here. The problem here is that a and b can be large so when we calculate (a % c) * (b % c), it goes beyond the range that long long int can hold, hence overflow occurs. For example, If a = (1012+7), b = (1013+5), c = (1015+3).

Now long long int can hold upto 4 x 1018(approximately) and a*b is much larger than that.

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Instead of doing direct multiplication we can add find a + a + ……….(b times) and take modulus with c each time we add a so that overflow don’t take place. But this would be inefficient looking at constraint on a, b and c. We have to somehow calculate (∑ a) % c in optimized manner.

We can use divide and conquer to calculate it. The main idea is:

1. If b is even then a*b = (2*a) * (b/2)
2. If b is odd then a*b = a + (2*a)*((b-1)/2)

Below is the implementation of the algorithm:

## C++

 `// C++ program to Compute (a*b)%c  ` `// such that (a%c) * (b%c) can be ` `// beyond range ` `#include ` `using` `namespace` `std; ` `typedef` `long` `long` `int` `ll; ` ` `  `// returns (a*b)%c ` `ll mulmod(ll a,ll b,ll c) ` `{ ` `    ``// base case if b==0, return 0 ` `    ``if` `(b==0) ` `        ``return` `0; ` ` `  `    ``// Divide the problem into 2 parts ` `    ``ll s = mulmod(a, b/2, c); ` ` `  `    ``// If b is odd, return ` `    ``// (a+(2*a)*((b-1)/2))%c ` `    ``if` `(b%2==1) ` `        ``return` `(a%c+2*(s%c)) % c; ` ` `  `    ``// If b is odd, return ` `    ``// ((2*a)*(b/2))%c ` `    ``else` `        ``return` `(2*(s%c)) % c; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``ll a = 1000000000007, b = 10000000000005; ` `    ``ll c = 1000000000000003; ` `    ``printf``(``"%lldn"``, mulmod(a, b, c)); ` `    ``return` `0; ` `} `

## Java

 `// Java program to Compute (a*b)%c  ` `// such that (a%c) * (b%c) can be ` `// beyond range ` `// returns (a*b)%c ` `class` `GFG { ` ` `  `    ``static` `long` `mulmod(``long` `a, ``long` `b, ``long` `c) { ` `        ``// base case if b==0, return 0  ` `        ``if` `(b == ``0``) { ` `            ``return` `0``; ` `        ``} ` ` `  `        ``// Divide the problem into 2 parts  ` `        ``long` `s = mulmod(a, b / ``2``, c); ` ` `  `        ``// If b is odd, return  ` `        ``// (a+(2*a)*((b-1)/2))%c  ` `        ``if` `(b % ``2` `== ``1``) { ` `            ``return` `(a % c + ``2` `* (s % c)) % c; ` `        ``} ``// If b is odd, return  ` `        ``// ((2*a)*(b/2))%c  ` `        ``else` `{ ` `            ``return` `(``2` `* (s % c)) % c; ` `        ``} ` `    ``} ` ` `  `// Driver code  ` `    ``public` `static` `void` `main(String[] args) { ` `        ``long` `a = 1000000000007L, b = 10000000000005L; ` `        ``long` `c = 1000000000000003L; ` `        ``System.out.println((mulmod(a, b, c))); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992  `

## Python3

 `# Python3 program of above approach ` ` `  `# returns (a*b)%c ` `def` `mulmod(a, b, c): ` ` `  `    ``# base case if b==0, return 0 ` `    ``if``(b ``=``=` `0``): ` `        ``return` `0` ` `  `    ``# Divide the problem into 2 parts ` `    ``s ``=` `mulmod(a, b ``/``/` `2``, c) ` ` `  `    ``# If b is odd, return ` `    ``# (a+(2*a)*((b-1)/2))%c ` `    ``if``(b ``%` `2` `=``=` `1``): ` `        ``return` `(a ``%` `c ``+` `2` `*` `(s ``%` `c)) ``%` `c ` ` `  `    ``# If b is odd, return ` `    ``# ((2*a)*(b/2))%c ` `    ``else``: ` `        ``return` `(``2` `*` `(s ``%` `c)) ``%` `c ` ` `  `# Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``a ``=` `1000000000007` `    ``b ``=` `10000000000005` `    ``c ``=` `1000000000000003` `    ``print``(mulmod(a, b, c)) ` ` `  `# This code is contributed by ` `# Sanjit_Prasad `

## C#

 `// C# program to Compute (a*b)%c  ` `// such that (a%c) * (b%c) can be ` `// beyond range ` `using` `System; ` ` `  `// returns (a*b)%c ` `class` `GFG ` `{ ` `static` `long` `mulmod(``long` `a, ``long` `b, ``long` `c)  ` `{  ` `    ``// base case if b==0, return 0  ` `    ``if` `(b == 0)  ` `        ``return` `0;  ` ` `  `    ``// Divide the problem into 2 parts  ` `    ``long` `s = mulmod(a, b / 2, c);  ` ` `  `    ``// If b is odd, return  ` `    ``// (a+(2*a)*((b-1)/2))%c  ` `    ``if` `(b % 2 == 1)  ` `        ``return` `(a % c + 2 * (s % c)) % c;  ` ` `  `    ``// If b is odd, return  ` `    ``// ((2*a)*(b/2))%c  ` `    ``else` `        ``return` `(2 * (s % c)) % c;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``long` `a = 1000000000007, b = 10000000000005;  ` `    ``long` `c = 1000000000000003;  ` `    ``Console.WriteLine(mulmod(a, b, c));  ` `}  ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output :

`74970000000035`

See this for sample run.

Time Complexity: O(log b)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

7

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.