Find the value of XXXX…..(N times) % M where N is large

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].

Examples:

Input: X = 7, N = 7, M = 50
Output: 27
7777777 % 50 = 27



Input: X = 1, N = 10, M = 9
Output: 1
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:

  1. If N is even -> XXX…(N times) = (XXX…(N/2 times) * 10N/2) + XXX..(N/2 times).
  2. 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Iterative function to calculate
// (x ^ y) % p in O(log y)
int power(int x, unsigned int y, int p)
{
  
    // Initialize result
    int res = 1;
  
    // Update x if it is >= p
    x = x % p;
  
    while (y > 0) {
  
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
  
        // y must be even now
        // y = y / 2
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
  
// Function to return XXX.....(N times) % M
int findModuloByM(int X, int N, int M)
{
  
    // Return the mod by M of smaller numbers
    if (N < 6) {
  
        // Creating a string of N X's
        string temp(N, (char)(48 + X));
  
        // Converting the string to int
        // and calculating the modulo
        int res = stoi(temp) % M;
  
        return res;
    }
  
    // Checking the parity of N
    if (N % 2 == 0) {
  
        // Dividing the number into equal half
        int half = findModuloByM(X, N / 2, M) % M;
  
        // Utilizing the formula for even N
        int res = (half * power(10, N / 2, M)
                   + half)
                  % M;
  
        return res;
    }
    else {
        // Dividing the number into equal half
        int half = findModuloByM(X, N / 2, M) % M;
  
        // Utilizing the formula for odd N
        int res = (half * power(10, N / 2 + 1, M)
                   + half * 10 + X)
                  % M;
  
        return res;
    }
}
  
// Driver code
int main()
{
    int X = 6, N = 14, M = 9;
  
    // Print XXX...(N times) % M
    cout << findModuloByM(X, N, M);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach 
  
# Iterative function to calculate 
# (x ^ y) % p in O(log y) 
def power(x, y, p) : 
  
    # Initialize result 
    res = 1
  
    # Update x if it is >= p 
    x = x % p; 
  
    while (y > 0) :
  
        # If y is odd, multiply x with result 
        if (y and 1) :
            res = (res * x) % p; 
  
        # y must be even now 
        # y = y // 2 
        y = y >> 1
        x = (x * x) % p; 
          
    return res; 
  
# Function to return XXX.....(N times) % M 
def findModuloByM(X, N, M) : 
  
    # Return the mod by M of smaller numbers 
    if (N < 6) :
  
        # Creating a string of N X's 
        temp = chr(48 + X) * N
  
        # Converting the string to int 
        # and calculating the modulo 
        res = int(temp) % M; 
  
        return res; 
  
    # Checking the parity of N 
    if (N % 2 == 0) :
  
        # Dividing the number into equal half 
        half = findModuloByM(X, N // 2, M) % M; 
  
        # Utilizing the formula for even N 
        res = (half * power(10, N // 2,
                                M) + half) % M; 
  
        return res; 
  
    else :
          
        # Dividing the number into equal half 
        half = findModuloByM(X, N // 2, M) % M; 
  
        # Utilizing the formula for odd N 
        res = (half * power(10, N // 2 + 1, M) +
               half * 10 + X) % M; 
  
        return res; 
          
# Driver code 
if __name__ == "__main__" :
  
    X = 6; N = 14; M = 9
  
    # Print XXX...(N times) % M 
    print(findModuloByM(X, N, M)); 
  
# This code is contributed by Ryuga

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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