Sunteți pe pagina 1din 9

Modulo 10^9+7 (1000000007)

In most of the programming competitions, we are required to answer the


result in 10^9+7 modulo. The reason behind this is, if problem constraints
are large integers, 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: 
 
1-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.
2-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 the 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 a large number using modulo but at different
positions. 

unsigned long long factorial(int n) unsigned long long factorial(int n)


{ {
const unsigned int M = 1000000007; const unsigned int M = 1000000007;
unsigned long long f = 1;
unsigned long long f = 1;
for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++)
f = f * i; // WRONG APPROACH as f = (f*i) % M; // Now f never can
// f may exceed (2^64 - 1) // exceed 10^9+7
return f;
return f % M; }
}
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 a 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. 
 

int mod(int a, int m)


{
return (a%m + m) % m;
}

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 performing 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
Euclidean 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.

S-ar putea să vă placă și