Given three integers X, N and M. The task is to find XXX…(N times) % M where X can be any digitfrom the range [1, 9].
Input: X = 7, N = 7, M = 50
7777777 % 50 = 27
Input: X = 1, N = 10, M = 9
1111111111 % 9 = 1
Approach: The problem can be solved using Divide and Conquer technique. The modulo of smaller numbers like X, XX, XXX etc. can be easily calculated. But problem arises for larger numbers. Hence, the number can be split as follows:
- If N is even -> XXX…(N times) = (XXX…(N/2 times) * 10N/2) + XXX..(N/2 times).
- If N is odd -> XXX…(N times) = (XXX…(N/2 times) * 10(N/2)+1) + (XXX…(N/2 times) * 10) + X.
By using the above formula, the number is divided into smaller parts whose modular operation can be easily found. Using the property (a + b) % m = (a % m + b % m) % m, a recursive divide and conquer solution is made to find the modulo of larger number using results of smaller numbers.
Below is the implementation of the above approach:
- Find (a^b)%m where 'b' is very large
- Find (a^b)%m where 'a' is very large
- Find N % 4 (Remainder with 4) for a large value of N
- Find Last Digit of a^b for Large Numbers
- Program to find remainder when large number is divided by r
- Program to find remainder when large number is divided by 11
- Multiply large integers under large modulo
- Find the K-th minimum element from an array concatenated M times
- LCM of two large numbers
- GCD of two numbers when one of them can be very large
- Sum of two large numbers
- Series summation if T(n) is given and n is very large
- Recursive sum of digit in n^x, where n and x are very large
- Difference of two large numbers
- Divisibility by 12 for a large number
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.
Improved By : Ryuga