BIG_Answer % 100000007 Type problems

Hi guys , I want to know how to solve this type of problem. Suppose my result is going out of bound of integer and double then how to change my answer to Answer % 100000007.

Ex - (factorial(500)) % 1000000007

Most of the programming contest has this type of problem but I don’t know the concept :frowning: Please help.

1 Like


All such solution are based on few simple rules:

( a + b ) mod m = ( a mod n + b mod n ) mod n
( a * b ) mod m = ( a mod n * b mod n ) mod n

With negative numbers there is a problem - according to wikipedia this is 3, not -2, so before doing minus operation it is recommended to add n (which is removed after mod operation), abs() is not working !!!

( a - b ) mod m = ( a mod n + n - b mod n ) mod n

What most of contestants are confused about is:

int MOD = 1000_000_007;
int a = 1000_000;
int b = 1000_000;
int c = ( a * b ) % MOD;

and while c is lower than MOD and a nad b are ints, it seems ok to use ints, but it is not…

Overflow occurs for a * b command.

Example - notice the result is negative.

There are few tricks how to overcome this

  1. use 1L * a * b
  2. or to use bigger type, long in Java, long long in C/C++ not long !!!

Disadvantage of 1.) is, that you have to identify all places where you need to apply this. And also you one have to know very well what is he doing - see

It may seem like there is problem with multiply only and addition is ok, there is a catch also:

a + b = ( a + b ) mod M

the above is fine for a,b <= 109 which it typical in problem statements…

But there is a problem if one tries to do

a + b + c = ( a + b + c ) mod M // there is overflow again -

You may be interested also in this answer.

Few problems I searched quickly:

  1. LUCKY2

Note: see the 1000_000, this is valid 106 in Java >= 7 and I love it, for more examples see SO, it is also possible to write binary numbers as 0b1011 which is 11 in decimal…


Probably you meant 109+7…

Thanks @betlista but i am talking about (factorial(100)) % 100000007 where the result can’t be store even in double.

You can refer this blog by abhinav92003 on handling operators with mod. Also few concepts involving modular operations are well explained there.

Also @betlista has made a point where many contestants tend make a mistake.

1 Like

while factorial n! = n * (n-1) * ... 2 * 1 … it is very the same, you just need to us modulo for each multiplication, like this:

int res = 1;
for ( int i = 2; i <= n; ++i ) res = ( res * i ) % MOD;

are u trying to say that 7%7 ==2%7 +5%7

I mean in some place if there is addition or other type of expression like power operator than can we apply this technique please help…

Yes, power is the very the same as factorial x^n = n * n * ... * n I added rules above, from which this is derived…

Thanks @betlista . One more thing

what about (n!/m!)%c =? and n and m >100

this is more difficult, if you are interested in combinations (n!/(r! * (n-r)!), then there is excellent article about the topic, otherwise there is another article