Find the value of XXXX…..(N times) % M where N is large
Last Updated :
10 Jul, 2022
Given three integers X, N and M. The task is to find XXX…(N times) % M where X can be any digit from 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:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, unsigned int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int findModuloByM( int X, int N, int M)
{
if (N < 6) {
string temp(N, ( char )(48 + X));
int res = stoi(temp) % M;
return res;
}
if (N % 2 == 0) {
int half = findModuloByM(X, N / 2, M) % M;
int res = (half * power(10, N / 2, M)
+ half)
% M;
return res;
}
else {
int half = findModuloByM(X, N / 2, M) % M;
int res = (half * power(10, N / 2 + 1, M)
+ half * 10 + X)
% M;
return res;
}
}
int main()
{
int X = 6, N = 14, M = 9;
cout << findModuloByM(X, N, M);
return 0;
}
|
Java
class GFG
{
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int findModuloByM( int X, int N, int M)
{
if (N < 6 )
{
String temp= "" ;
for ( int i = 0 ; i< N ; i++)
temp = temp + ( char )(X + 48 );
int res = Integer.parseInt(temp) % M;
return res;
}
if (N % 2 == 0 )
{
int half = findModuloByM(X, N / 2 , M) % M;
int res = (half * power( 10 , N / 2 , M)
+ half)
% M;
return res;
}
else
{
int half = findModuloByM(X, N / 2 , M) % M;
int res = (half * power( 10 , N / 2 + 1 , M)
+ half * 10 + X)
% M;
return res;
}
}
public static void main (String[] args)
{
int X = 6 , N = 14 , M = 9 ;
System.out.println(findModuloByM(X, N, M));
}
}
|
Python3
def power(x, y, p) :
res = 1 ;
x = x % p;
while (y > 0 ) :
if (y and 1 ) :
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
return res;
def findModuloByM(X, N, M) :
if (N < 6 ) :
temp = chr ( 48 + X) * N
res = int (temp) % M;
return res;
if (N % 2 = = 0 ) :
half = findModuloByM(X, N / / 2 , M) % M;
res = (half * power( 10 , N / / 2 ,
M) + half) % M;
return res;
else :
half = findModuloByM(X, N / / 2 , M) % M;
res = (half * power( 10 , N / / 2 + 1 , M) +
half * 10 + X) % M;
return res;
if __name__ = = "__main__" :
X = 6 ; N = 14 ; M = 9 ;
print (findModuloByM(X, N, M));
|
C#
using System;
class GFG
{
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int findModuloByM( int X, int N, int M)
{
if (N < 6)
{
string temp= "" ;
for ( int i = 0; i< N ; i++)
temp = temp + ( char )(X + 48);
int res = Convert.ToInt32(temp) % M;
return res;
}
if (N % 2 == 0)
{
int half = findModuloByM(X, N / 2, M) % M;
int res = (half * power(10, N / 2, M)
+ half)
% M;
return res;
}
else
{
int half = findModuloByM(X, N / 2, M) % M;
int res = (half * power(10, N / 2 + 1, M)
+ half * 10 + X)
% M;
return res;
}
}
public static void Main ()
{
int X = 6, N = 14, M = 9;
Console.WriteLine(findModuloByM(X, N, M));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y &1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function findModuloByM( $X , $N , $M )
{
if ( $N < 6)
{
$temp = chr (48 + $X ) * $N ;
$res = intval ( $temp ) % $M ;
return $res ;
}
if ( $N % 2 == 0)
{
$half = findModuloByM( $X , (int)( $N / 2), $M ) % $M ;
$res = ( $half * power(10,(int)( $N / 2),
$M ) + $half ) % $M ;
return $res ;
}
else
{
$half = findModuloByM( $X , (int)( $N / 2), $M ) % $M ;
$res = ( $half * power(10, (int)( $N / 2) + 1, $M ) +
$half * 10 + $X ) % $M ;
return $res ;
}
}
$X = 6;
$N = 14;
$M = 9;
print (findModuloByM( $X , $N , $M ));
?>
|
Javascript
<script>
function power(x, y, p)
{
var res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function findModuloByM(X, N, M)
{
if (N < 6)
{
var temp = "" ;
for ( var i = 1; i < N; i++)
{
temp += String.fromCharCode(48 + X);
}
var res = parseInt(temp) % M;
return res;
}
if (N % 2 == 0)
{
var half = findModuloByM(X, N / 2, M) % M;
var res = (half *
power(10, N / 2, M) + half) % M;
return res;
}
else
{
var half = findModuloByM(X, N / 2, M) % M;
var res = (half * power(10, N / 2 + 1, M) +
half * 10 + X) % M;
return res;
}
}
var X = 6, N = 14, M = 9;
document.write( findModuloByM(X, N, M));
</script>
|
Time Complexity : O(log(N))
Space Complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...