In most of the programming competitions, problems are required to answer the result in 10^9+7 modulo. The reason behind this is to have problems for large integers so that only efficient algorithms can solve them in allowed limited time.
What is modulo operation:
The remainder obtained after the division operation on two operands is known as modulo operation. Operator for doing modulus operation is ‘%’. For ex: a % b = c which means, when a is divided by b it gives the remainder c, 7%2 = 1, 17%3 = 2.
Why do we need modulo:
- The reason of taking Mod is to prevent integer overflows. The largest integer data type in C/C++ is unsigned long long int which is of 64 bit and can handle integer from 0 to (2^64 – 1). But in some problems where the growth rate of output is very high, this high range of unsigned long long may be insufficient.
Suppose in a 64 bit variable ‘A’, 2^62 is stored and in another 64 bit variable ‘B’, 2^63 is stored. When we multiply A and B, the system does not give a runtime error or exception. It just does some bogus computation and stores the bogus result because the bit size of result comes after multiplication overflows.
- In some of the problems, to compute the result modulo inverse is needed and this number helps a lot because it is prime. Also this number should be large enough otherwise modular inverse techniques may fail in some situations.
Due to these reasons, problem setters require to give the answer as a result of modulo of some number N.
There are certain criteria on which value of N depends:
- It should just be large enough to fit in the largest integer data type i.e it makes sure that there is no overflow in result.
- It should be a prime number because if we take mod of a number by Prime the result is generally spaced i.e. the results are very different results in comparison to mod the number by non-prime, that is why primes are generally used for mod.
10^9+7 fulfills both the criteria. It is the first 10-digit prime number and fits in int data type as well. In fact, any prime number less than 2^30 will be fine in order to prevent possible overflows.
How modulo is used:
A few distributive properties of modulo are as follows:
- ( a + b) % c = ( ( a % c ) + ( b % c ) ) % c
- ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
- ( a – b) % c = ( ( a % c ) – ( b % c ) ) % c
( a / b ) % c = ( ( a % c ) / ( b % c ) ) % c
So, modulo is distributive over +, * and – but not over / [Please refer Modular Division for details]
NOTE: The result of ( a % b ) will always be less than b.
In case of computer programs, due to the size of variable limitations we perform modulo M at each intermediate stage so that range overflow never occurs.
Example: a = 145785635595363569532135132 b = 3151635135413512165131321321 c = 999874455222222200651351351 m = 1000000007 Print (a*b*c)%m. Method 1: First, multiply all the number and then take modulo: (a*b*c)%m = (459405448184212290893339835148809 515332440033400818566717735644307024625348601572) % 1000000007 a*b*c does not fit even in the unsigned long long int due to which system drop some of its most significant digits. Therefore, it gives the wrong answer. (a*b*c)%m = 798848767 Method 2: Take modulo at each intermediate steps: i = 1 i = (i*a) % m // i = 508086243 i = (i*b) % m // i = 144702857 i = (i*c) % m // i = 798848767 i = 798848767 Method 2 always gives the correct answer.
Function for finding factorial of large number using modulo but at different positions.
The same procedure can be followed for addition.
(a + b + c) % M is the same as ( ( ( a + b ) % M ) + c ) % M.
Perform % M every time a number is added so as to avoid overflow.
Note: In most of the programming languages (like in C/C++) when you perform modular operation with negative numbers it gives negative result like -5%3 = -2, but what the result comes after modular operation should be in the range 0 to n-1 means the -5%3 = 1. So for this convert it into positive modular equivalent.
But the rules are different for division. To perform division in modulo arithmetic, we need to first understand the concept of modulo multiplicative inverse.
Modulo multiplicative Inverse(MMI):
The multiplicative inverse of a number y is z iff (z * y) == 1.
Dividing a number x by another number y is same as multiplying x with the multiplicative inverse of y.
x / y == x * y^(-1) == x * z (where z is multiplicative inverse of y).
In normal arithmetic, the multiplicative inverse of y is a float value. Ex: Multiplicative inverse of 7 is 0.142…, of 3 is 0.333… .
In mathematics, modular multiplicative inverse of an integer ‘a’ is an integer ‘x’ such that the product ax is congruent to 1 with respect to the modulus m.
ax = 1( mod m)
The remainder after dividing ax by the integer m is 1.
Example: If M = 7, the MMI of 4 is 2 as ( 4 * 2 ) %7 ==1, If M = 11, the MMI of 7 is 8 as ( 7 * 8 )%11 ==1, If M = 13, the MMI of 7 is 2 as ( 7 * 2 ) % 13==1.
Observe that the MMI of a number may be different for different M.
So, if we are performi,ng modulo arithmetic in our program and we need the result of the operation x / y, we should NOT perform
z = (x/y) % M;
instead we should perform
y_inv = findMMI(y, M); z = (x * y_inv) % M;
Now one question remains.. How to find the MMI of a number n.
There exist two algorithms to find MMI of a number. First is the Extended Euclidian algorithm and the second using Fermat’s Little Theorem.
You can find both the methods in the given link:
Modular multiplicative inverse
Finding modular multiplicative inverses also has practical applications in the field of cryptography, i.e. public-key cryptography and the RSA Algorithm. A benefit for the computer implementation of these applications is that there exists a very fast algorithm (the extended Euclidean algorithm) that can be used for the calculation of modular multiplicative inverses.
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Fibonacci modulo p
- LCM of N numbers modulo M
- Compute n! under modulo p
- Sum of two numbers modulo M
- Maximum subarray sum modulo m
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Find sum of modulo K of first N natural number
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Equalizing array using increment under modulo 3
- Sort elements of array whose modulo with K yields P
- Expressing a fraction as a natural number under modulo 'm'
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Cyclic Redundancy Check and Modulo-2 Division
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Primitive root of a prime number n modulo n