Given integers x1, x2, x3……xn, b, and m, we are supposed to find the result of ((x1*x2….xn)/b)mod(m).
Example 1: Suppose that we are required to find (55C5)%(1000000007) i.e ((55*54*53*52*51)/120)%1000000007
Naive Method :
- Simply calculate the product (55*54*53*52*51)= say x,
- Divide x by 120 and then take its modulus with 1000000007
Using Modular Multiplicative Inverse :
The above method will work only when x1, x2, x3….xn have small values.
Suppose we are required to find the result where x1, x2, ….xn fall in the range of ~1000000(10^6). So we will have to exploit the rule of modular mathematics which says :
(a*b)mod(m)=(a(mod(m))*b(mod(m)))mod(m)
Note that the above formula is valid for modular multiplication. A similar formula for division does not exist.
i.e (a/b)mod(m) != a(mod(m))/b(mod(m))
- So we are required to find out the modular multiplicative inverse of b say i and then multiply ‘i’ with a .
- After this we will have to take the modulus of the result obtained.
i.e ((x1*x2….xn)/b)mod(m)=((x1*x2….xn)*i)mod(m)= ((x1)mod(m) * (x2)mod(m) *…. (xn)mod(m) * (i)mod(m))mod(m)
Note: To find modular multiplicative inverse we can use the Extended Euclidean algorithm or Fermat’s Little Theorem.
Example 2 : Let us suppose that we have to find (55555C5)%(1000000007) i.e ((55555*55554*55553*55552*55551)/120)%1000000007.
C++
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define int ll
typedef long double ld;
ll MOD = 998244353;
double eps = 1e-12;
#define mp make_pair
#define pb push_back
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int modular_inverse( int a, int m){
for ( int x = 1; x < m; x++)
if (((a%m) * (x%m)) % m == 1)
return x;
return 0;
}
int32_t main()
{
int ans = 1;
int naive_answer = ((55555 *55554 *55553 *55552 * 55551)/120)% 1000000007;
int i = modular_inverse(120, 10000007);
for ( int i = 0; i < 5; i++)
ans = (ans * (55555 - i)) % 1000000007;
ans = (ans * i) % 1000000007;
cout << "Answer using naive method: " << naive_answer << endl;
cout << "Answer using multiplicative"
<< " modular inverse concept: " << ans;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int modular_inverse( int a, int m)
{
for ( int x = 1 ; x < m; x++)
if (((a % m) * (x % m)) % m == 1 )
return x;
return 0 ;
}
public static void main(String[] args)
{
int ans = 1 ;
Long naive_answer = (55555L * 55554L * 55553L
* 55552L * 55551L / 120L)
% 1000000007L;
System.out.println( "Answer using naive method: "
+ naive_answer);
int i = modular_inverse( 120 , 10000007 );
for (i = 0 ; i < 5 ; i++)
ans = (ans * ( 55555 - i)) % 1000000007 ;
ans = (ans * i) % 1000000007 ;
System.out.println(
"Answer using multiplicative modular inverse concept: "
+ ans);
}
}
|
Python3
def modular_inverse(a, m):
for x in range ( 1 , m):
if (((a % m) * (x % m)) % m = = 1 ):
return x
return - 1
if __name__ = = '__main__' :
naive_answer = ((( 55555 * 55554 *
55553 * 55552 *
55551 ) / / 120 ) %
1000000007 )
ans = 1
i = modular_inverse( 120 , 10000007 )
for j in range ( 5 ):
ans = ((ans *
( 55555 - j)) %
1000000007 )
ans = (ans * i) % 1000000007
print ( "Answer using naive method:" ,
naive_answer)
print ( "Answer using multiplicative" +
"modular inverse concept:" , ans)
|
C#
using System;
using System.Numerics;
using System.Collections.Generic;
class GFG {
static int modular_inverse( int a, int m)
{
for ( int x = 1; x < m; x++)
if (((a % m) * (x % m)) % m == 1)
return x;
return 0;
}
public static void Main( string [] args)
{
int ans = 1;
unchecked
{
UInt64 naive_answer
= ((55555UL * 55554UL * 55553UL * 55552UL
* 55551UL)
/ 120UL)
% 1000000007UL;
Console.WriteLine( "Answer using naive method: "
+ naive_answer);
}
int i = modular_inverse(120, 10000007);
for (i = 0; i < 5; i++)
ans = (ans * (55555 - i)) % 1000000007;
ans = (ans * i) % 1000000007;
Console.WriteLine(
"Answer using multiplicative modular inverse concept: "
+ ans);
}
}
|
Javascript
function modular_inverse(a, m){
for (let x = 1; x < m; x++){
if (((a % m) * (x % m)) % m == 1){
return x;
}
}
}
let naive_answer =((55555 * 55554 * 55553 * 55552 * 55551) / 120) % 1000000007;
let ans = 1
let i = modular_inverse(120, 10000007)
for (let j = 0;j < 5; j++){
ans = ((ans * (55555 - j)) % 1000000007)
}
ans = (ans * i) % 1000000007
console.log( "Answer using naive method:" ,
naive_answer)
console.log( "Answer using multiplicative" +
"modular inverse concept:" , ans)
|
Different Languages give different result for the naive approach.
C++
Input :
Output:
Answer using naive method: 18446744073703577963
Answer using multiplicative modular inverse concept: 125376140
Python
Input:
Output:
Answer using naive method: 300820513
Answer using multiplicative modular inverse concept: 125376140
JavaScript:
Input:
Output:
Answer using naive method: 301201761
Answer using multiplicative modular inverse concept: 125376140
It is clear from the above example that the naive method will lead to an overflow of data resulting in an incorrect answer. Moreover, using modular inverse will give us the correct answer.
Without Using Modular Multiplicative Inverse :
But it is interesting to note that a slight change in code will discard the use of finding modular multiplicative inverse.
C++
#include <iostream>
using namespace std;
int main()
{
long int ans = 1;
long int mod = ( long int )1000000007 * 120;
for ( int i = 0; i < 5; i++)
ans = (ans * (55555 - i)) % mod;
ans = ans / 120;
cout << "Answer using shortcut: " << ans;
return 0;
}
|
Java
class GFG {
public static void main(String[] args)
{
long ans = 1 ;
long mod = ( long ) 1000000007 * 120 ;
for ( int i = 0 ; i < 5 ; i++)
ans = (ans * ( 55555 - i)) % mod;
ans = ans / 120 ;
System.out.println( "Answer using"
+ " shortcut: " + ans);
}
}
|
Python3
ans = 1
mod = 1000000007 * 120
for i in range ( 0 , 5 ) :
ans = (ans * ( 55555 - i)) % mod
ans = int (ans / 120 )
print ( "Answer using shortcut: " , ans)
|
C#
using System;
class GFG {
public static void Main()
{
long ans = 1;
long mod = ( long )1000000007 * 120;
for ( int i = 0; i < 5; i++)
ans = (ans * (55555 - i)) % mod;
ans = ans / 120;
Console.Write( "Answer using "
+ "shortcut: " + ans);
}
}
|
PHP
<?php
$ans = 1;
$mod = 1000000007 * 120;
for ( $i = 0; $i < 5; $i ++)
$ans = ( $ans * (55555 - $i )) %
$mod ;
$ans = $ans / 120;
echo "Answer using shortcut: " ,
$ans ;
?>
|
Javascript
<script>
var ans = 1;
var mod = 1000000007 * 120;
for ( var i = 0; i < 5; i++)
ans = (ans * (55555 - i)) % mod;
ans = ans / 120;
document.write( "Answer using" +
" shortcut: " + ans);
</script>
|
OutputAnswer using shortcut: 300820513
Why did it work?
This will work only in case when the denominator is a factor of numerator i.e. when a % b = 0 following the rule:
If b | a, then we can write (a/b) % p as (a % p*b)/b.
This rule proves useful for small values of b.
Let us consider a = x1*x2*x3…….xn
We have to find ans = (a/b)%1000000007
- Let result of a%(1000000007*b) be y. To avoid overflow, we use modular multiplicative property. This can be represented as
a = (1000000007*b)q + y where y < (1000000007*b) and q is an integer - Now dividing LHS and RHS by b, we get
y/b = a/b -(1000000007*b)*q/b
= a/b -1000000007*q < 1000000007 (From 1)
Therefore, y/b is equivalent to (a/b)mod(b*1000000007). 🙂
Please Login to comment...