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 integers under large modulo
- Multiply Large Numbers represented as Strings
- Modulo of a large Binary String
- Number of factors of very large number N modulo M where M is any prime number
- Divide large number represented as string
- Square of large number represented as String
- Writing power function for large numbers
- 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
- Check if a large number is divisible by a number which is a power of 2
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Given two numbers as strings, find if one is a power of other
- Sum of two numbers modulo M
- Sum of the natural numbers (up to N) whose modulo with K yield R
- LCM of N numbers modulo M
- Subtract Two Numbers represented as Linked Lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.