Given two numbers sa and sb represented as strings, find ab % MOD where MOD is 1e9 + 7. The numbers a and b can contain upto 106 digits.
Input : sa = 2, sb = 3
Output : 8
Input : sa = 10000000000000000000000000000000000000000000
sb = 10000000000000000000000000000000000000000000
Output : 494234546
As a and b very large (may contain upto 10^6 digits each). So what we can do, we apply Fermat’s little theorem and property of modulo to reduce a and b.
As we know,
(ab) % MOD = ((a % MOD)b) % MOD
How to reduce b, We have already discuss in Find (a^b)%m where ‘b’ is very large
Now finally we have both a and b are in range of 1<=a, b<=10^9+7. Hence we can now use our modular exponentiation to calculate required answer.
- Multiply Large Numbers represented as Strings
- Writing power function for large numbers
- Multiply large integers under large modulo
- Given two numbers as strings, find if one is a power of other
- Modulo of a large Binary String
- Divide large number represented as string
- Square of large number represented as String
- Highest power of 2 that divides a number represented in binary
- Smallest number greater than n that can be represented as a sum of distinct power of k
- LCM of N numbers modulo M
- Sum of two numbers modulo M
- Number of factors of very large number N modulo M where M is any prime number
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Add two numbers represented by two arrays
- Add two numbers represented by linked lists | Set 2
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.