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:
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Multiply large integers under large modulo
- Find N % 4 (Remainder with 4) for a large value of N
- Times required by Simple interest for the Principal to become Y times itself
- Find the value of N XOR'ed to itself K times
- Number of times Maximum and minimum value updated during traversal of array
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Find Last Digit of a^b for Large Numbers
- Find (a^b)%m where 'a' is very large
- Program to find remainder when large number is divided by 11
- Find (a^b)%m where 'b' is very large
- Program to find remainder when large number is divided by r
- Find smallest possible Number from a given large Number with same count of digits
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Find the maximum possible value of the minimum value of modified array
- Find the K-th minimum element from an array concatenated M times
- Find the number of different numbers in the array after applying the given operation q times
- Find Nth smallest number that is divisible by 100 exactly K times
- Find the height of a right-angled triangle whose area is X times its base
- Find the elements appearing even number of times in an Array
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.