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.
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:
- If b is even then a*b = (2*a) * (b/2)
- If b is odd then a*b = a + (2*a)*((b-1)/2)
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mulmod(ll a,ll b,ll c)
{
if (b==0)
return 0;
ll s = mulmod(a, b/2, c);
if (b%2==1)
return (a%c+2*(s%c)) % c;
else
return (2*(s%c)) % c;
}
int main()
{
ll a = 1000000000007, b = 10000000000005;
ll c = 1000000000000003;
printf ( "%lldn" , mulmod(a, b, c));
return 0;
}
|
Java
class GFG {
static long mulmod( long a, long b, long c) {
if (b == 0 ) {
return 0 ;
}
long s = mulmod(a, b / 2 , c);
if (b % 2 == 1 ) {
return (a % c + 2 * (s % c)) % c;
}
else {
return ( 2 * (s % c)) % c;
}
}
public static void main(String[] args) {
long a = 1000000000007L, b = 10000000000005L;
long c = 1000000000000003L;
System.out.println((mulmod(a, b, c)));
}
}
|
Python3
def mulmod(a, b, c):
if (b = = 0 ):
return 0
s = mulmod(a, b / / 2 , c)
if (b % 2 = = 1 ):
return (a % c + 2 * (s % c)) % c
else :
return ( 2 * (s % c)) % c
if __name__ = = '__main__' :
a = 1000000000007
b = 10000000000005
c = 1000000000000003
print (mulmod(a, b, c))
|
C#
using System;
class GFG
{
static long mulmod( long a, long b, long c)
{
if (b == 0)
return 0;
long s = mulmod(a, b / 2, c);
if (b % 2 == 1)
return (a % c + 2 * (s % c)) % c;
else
return (2 * (s % c)) % c;
}
public static void Main()
{
long a = 1000000000007, b = 10000000000005;
long c = 1000000000000003;
Console.WriteLine(mulmod(a, b, c));
}
}
|
Javascript
<script>
function mulmod(a , b , c) {
if (b == 0) {
return 0;
}
var s = mulmod(a, parseInt(b / 2), c);
if (b % 2 == 1) {
return (a % c + 2 * (s % c)) % c;
}
else {
return (2 * (s % c)) % c;
}
}
var a = 1000000000007, b = 10000000000005;
var c = 1000000000000003;
document.write((mulmod(a, b, c)));
</script>
|
PHP
<?php
function mulmod( $a , $b , $c )
{
if ( $b ==0)
return 0;
$s = mulmod( $a , $b /2, $c );
if ( $b % 2 == 1)
return ( $a % $c + 2 *
( $s % $c )) % $c ;
else
return (2 * ( $s % $c )) % $c ;
}
$a = 1000000000007;
$b = 10000000000005;
$c = 1000000000000003;
echo mulmod( $a , $b , $c );
?>
|
Output :
74970000000035
See this for sample run.
Time Complexity: O(log b)
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Approach#2: Using modulo properties
this approach is to use the properties of modulo arithmetic. Specifically, we can use the fact that (ab)%c = ((a%c)(b%c))%c. This means that we can first take the modulus of a and b, and then calculate their product.
Algorithm
Start by defining the function multiply_modulo with three input parameters: a, b, and c.
Calculate the remainder of a when divided by c using the modulo operator and assign it to a.
Calculate the remainder of b when divided by c using the modulo operator and assign it to b.
Multiply a and b.
Calculate the remainder of the product obtained in step 4 when divided by c using the modulo operator and assign it to product.
Return product as the output of the function.
C++
#include <iostream>
#include <sstream>
#include <string>
std::string multiplyModulo( const std::string& aStr,
const std::string& bStr,
const std::string& cStr)
{
std::stringstream aStream(aStr), bStream(bStr),
cStream(cStr);
long long a, b, c;
aStream >> a;
bStream >> b;
cStream >> c;
a = (a % c + c) % c;
b = (b % c + c) % c;
long long product = (a * b);
product = (product % c + c) % c;
return std::to_string(product);
}
int main()
{
std::string a = "1000000000007" ;
std::string b = "10000000000005" ;
std::string c = "1000000000000003" ;
std::string result = multiplyModulo(a, b, c);
std::cout << "Result: " << result << std::endl;
return 0;
}
|
Java
import java.math.BigInteger;
public class Main {
static BigInteger multiplyModulo(BigInteger a, BigInteger b, BigInteger c) {
a = a.mod(c);
b = b.mod(c);
BigInteger product = a.multiply(b).mod(c);
return product;
}
public static void main(String[] args) {
BigInteger a = new BigInteger( "1000000000007" );
BigInteger b = new BigInteger( "10000000000005" );
BigInteger c = new BigInteger( "1000000000000003" );
System.out.println(multiplyModulo(a, b, c));
}
}
|
Python3
def multiply_modulo(a, b, c):
a = a % c
b = b % c
product = (a * b) % c
return product
a = 1000000000007
b = 10000000000005
c = 1000000000000003
print (multiply_modulo(a, b, c))
|
C#
using System;
class GFG
{
static long MultiplyModulo( long a, long b, long c)
{
if (b == 0)
return 0;
long s = MultiplyModulo(a, b / 2, c);
if (b % 2 == 1)
return (a % c + 2 * (s % c)) % c;
else
return (2 * (s % c)) % c;
}
public static void Main()
{
long a = 1000000000007, b = 10000000000005;
long c = 1000000000000003;
Console.WriteLine(MultiplyModulo(a, b, c));
}
}
|
Javascript
function multiply_modulo(a, b, c) {
a = BigInt(a) % BigInt(c);
b = BigInt(b) % BigInt(c);
let product = (a * b) % BigInt(c);
return product;
}
let a = BigInt( "1000000000007" );
let b = BigInt( "10000000000005" );
let c = BigInt( "1000000000000003" );
console.log(multiply_modulo(a, b, c).toString());
|
Output
Result: 972978359410049
Time complexity: O(log(a)+log(b))
Auxiliary Space is O(1).
Last Updated :
16 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...